From b5c1afcb848671a3c4cf66cf8f7ce1836316d16f Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Tue, 25 Feb 2020 20:03:41 -0700 Subject: [PATCH] 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 --- .../components/simplisafe/__init__.py | 28 +++++++++++++++++-- .../simplisafe/alarm_control_panel.py | 5 ---- homeassistant/components/simplisafe/lock.py | 5 ---- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/simplisafe/__init__.py b/homeassistant/components/simplisafe/__init__.py index f9c61b6add2..38a715d494a 100644 --- a/homeassistant/components/simplisafe/__init__.py +++ b/homeassistant/components/simplisafe/__init__.py @@ -6,6 +6,8 @@ from simplipy import API from simplipy.errors import InvalidCredentialsError, SimplipyError from simplipy.websocket import ( EVENT_CAMERA_MOTION_DETECTED, + EVENT_CONNECTION_LOST, + EVENT_CONNECTION_RESTORED, EVENT_DOORBELL_DETECTED, EVENT_ENTRY_DETECTED, EVENT_LOCK_LOCKED, @@ -528,7 +530,10 @@ class SimpliSafeEntity(Entity): self._online = True self._simplisafe = simplisafe self._system = system - self.websocket_events_to_listen_for = [] + self.websocket_events_to_listen_for = [ + EVENT_CONNECTION_LOST, + EVENT_CONNECTION_RESTORED, + ] if serial: self._serial = serial @@ -655,13 +660,32 @@ class SimpliSafeEntity(Entity): 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 def async_update_from_rest_api(self): """Update the entity with the provided REST API data.""" 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 def async_update_from_websocket_event(self, event): """Update the entity with the provided websocket API data.""" diff --git a/homeassistant/components/simplisafe/alarm_control_panel.py b/homeassistant/components/simplisafe/alarm_control_panel.py index c675f9c2748..9166c59bec0 100644 --- a/homeassistant/components/simplisafe/alarm_control_panel.py +++ b/homeassistant/components/simplisafe/alarm_control_panel.py @@ -190,11 +190,6 @@ class SimpliSafeAlarm(SimpliSafeEntity, AlarmControlPanel): @callback def async_update_from_rest_api(self): """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: self._attrs.update( { diff --git a/homeassistant/components/simplisafe/lock.py b/homeassistant/components/simplisafe/lock.py index 58448ec4599..fc98d67ccbf 100644 --- a/homeassistant/components/simplisafe/lock.py +++ b/homeassistant/components/simplisafe/lock.py @@ -70,11 +70,6 @@ class SimpliSafeLock(SimpliSafeEntity, LockDevice): @callback def async_update_from_rest_api(self): """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( { ATTR_LOCK_LOW_BATTERY: self._lock.lock_low_battery,