Skip to content

Commit

Permalink
Merge pull request #3 from rospogrigio/improved_status_check
Browse files Browse the repository at this point in the history
Improving status check
  • Loading branch information
rospogrigio authored Nov 27, 2021
2 parents a3fa8a8 + 3a6dd79 commit 8ec8ee8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
23 changes: 18 additions & 5 deletions custom_components/airbnk_mqtt/lock_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

_LOGGER = logging.getLogger(__name__)

MAX_NORECEIVE_TIME = 30

BLEOpTopic = "cmnd/%s/BLEOp"
BLERule1Topic = "cmnd/%s/Rule1"
BLEDetailsTopic = "cmnd/%s/BLEDetails2"
Expand Down Expand Up @@ -140,6 +142,13 @@ def device_info(self):
},
}

def check_availability(self):
curr_time = int(round(time.time()))
deltatime = curr_time - self.last_advert_time
# _LOGGER.debug("Last reply was %s secs ago", deltatime)
if deltatime >= MAX_NORECEIVE_TIME:
self.is_available = False

@property
def islocked(self) -> bool | None:
if self.curr_state == LOCK_STATE_LOCKED:
Expand Down Expand Up @@ -224,7 +233,7 @@ def parse_MQTT_message(self, msg):

deltatime = self.last_advert_time - time2
self._lockData[SENSOR_TYPE_LAST_ADVERT] = deltatime
if deltatime < 30:
if deltatime < MAX_NORECEIVE_TIME:
self.is_available = True
_LOGGER.debug("Time from last message: %s secs", str(deltatime))
elif time2 != 0:
Expand Down Expand Up @@ -264,8 +273,11 @@ def parse_MQTT_message(self, msg):
and msg_written_payload == self.frame2hex.upper()
):
self.frame2sent = True
_LOGGER.debug("OPERATING WAS %s", self.operating)
self.curr_state = LOCK_STATE_UNLOCKED if self.operating == 1 else 0
# _LOGGER.debug("OPERATING WAS %s", self.operating)
if self.operating == 1:
self.curr_state = LOCK_STATE_UNLOCKED
else:
self.curr_state = LOCK_STATE_LOCKED
self.operating = 0

for callback_func in self._callbacks:
Expand Down Expand Up @@ -553,10 +565,11 @@ def parse_MQTT_advert(self, mqtt_advert):
)

lockEvents = (bArr[18] << 24) | (bArr[19] << 16) | (bArr[20] << 8) | bArr[21]
new_state = (bArr[22] >> 4) & 3
self.opensClockwise = (bArr[22] & 0x80) != 0
if lockEvents != self.lockEvents:
if self.curr_state < LOCK_STATE_OPERATING or self.lockEvents != lockEvents:
self.lockEvents = lockEvents
self.curr_state = (bArr[22] >> 4) & 3
self.curr_state = new_state
if self.opensClockwise and self.curr_state is not LOCK_STATE_JAMMED:
self.curr_state = 1 - self.curr_state

Expand Down
1 change: 1 addition & 0 deletions custom_components/airbnk_mqtt/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class AirbnkBatterySensor(AirbnkSensor):
def state(self):
"""Return the state of the sensor."""
# print("VOLT: {} {}".format(self._monitored_attribute, self._device._lockData))
self._device.check_availability()
if self._monitored_attribute in self._device._lockData:
return self._device._lockData[self._monitored_attribute]
return None
Expand Down

0 comments on commit 8ec8ee8

Please sign in to comment.