Make SimpliSafe entities unavailable when wifi is lost (#32154)

* Make SimpliSafe entities unavailable when wifi is lost

* Remove online status from REST API

* Comments

* Mispelling
This commit is contained in:
Aaron Bach 2020-02-25 20:03:41 -07:00 committed by GitHub
parent 4c33a9d732
commit b5c1afcb84
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 12 deletions

View file

@ -6,6 +6,8 @@ from simplipy import API
from simplipy.errors import InvalidCredentialsError, SimplipyError from simplipy.errors import InvalidCredentialsError, SimplipyError
from simplipy.websocket import ( from simplipy.websocket import (
EVENT_CAMERA_MOTION_DETECTED, EVENT_CAMERA_MOTION_DETECTED,
EVENT_CONNECTION_LOST,
EVENT_CONNECTION_RESTORED,
EVENT_DOORBELL_DETECTED, EVENT_DOORBELL_DETECTED,
EVENT_ENTRY_DETECTED, EVENT_ENTRY_DETECTED,
EVENT_LOCK_LOCKED, EVENT_LOCK_LOCKED,
@ -528,7 +530,10 @@ class SimpliSafeEntity(Entity):
self._online = True self._online = True
self._simplisafe = simplisafe self._simplisafe = simplisafe
self._system = system self._system = system
self.websocket_events_to_listen_for = [] self.websocket_events_to_listen_for = [
EVENT_CONNECTION_LOST,
EVENT_CONNECTION_RESTORED,
]
if serial: if serial:
self._serial = serial self._serial = serial
@ -655,13 +660,32 @@ class SimpliSafeEntity(Entity):
ATTR_LAST_EVENT_TIMESTAMP: last_websocket_event.timestamp, ATTR_LAST_EVENT_TIMESTAMP: last_websocket_event.timestamp,
} }
) )
self.async_update_from_websocket_event(last_websocket_event) self._async_internal_update_from_websocket_event(last_websocket_event)
@callback @callback
def async_update_from_rest_api(self): def async_update_from_rest_api(self):
"""Update the entity with the provided REST API data.""" """Update the entity with the provided REST API data."""
pass pass
@callback
def _async_internal_update_from_websocket_event(self, event):
"""Check for connection events and set offline appropriately.
Should not be called directly.
"""
if event.event_type == EVENT_CONNECTION_LOST:
self._online = False
elif event.event_type == EVENT_CONNECTION_RESTORED:
self._online = True
# It's uncertain whether SimpliSafe events will still propagate down the
# websocket when the base station is offline. Just in case, we guard against
# further action until connection is restored:
if not self._online:
return
self.async_update_from_websocket_event(event)
@callback @callback
def async_update_from_websocket_event(self, event): def async_update_from_websocket_event(self, event):
"""Update the entity with the provided websocket API data.""" """Update the entity with the provided websocket API data."""

View file

@ -190,11 +190,6 @@ class SimpliSafeAlarm(SimpliSafeEntity, AlarmControlPanel):
@callback @callback
def async_update_from_rest_api(self): def async_update_from_rest_api(self):
"""Update the entity with the provided REST API data.""" """Update the entity with the provided REST API data."""
if self._system.state == SystemStates.error:
self._online = False
return
self._online = True
if self._system.version == 3: if self._system.version == 3:
self._attrs.update( self._attrs.update(
{ {

View file

@ -70,11 +70,6 @@ class SimpliSafeLock(SimpliSafeEntity, LockDevice):
@callback @callback
def async_update_from_rest_api(self): def async_update_from_rest_api(self):
"""Update the entity with the provided REST API data.""" """Update the entity with the provided REST API data."""
if self._lock.offline or self._lock.disabled:
self._online = False
return
self._online = True
self._attrs.update( self._attrs.update(
{ {
ATTR_LOCK_LOW_BATTERY: self._lock.lock_low_battery, ATTR_LOCK_LOW_BATTERY: self._lock.lock_low_battery,