Allow entities to opt out of REST and/or websocket API updates

This commit is contained in:
Aaron Bach
2020-02-07 10:16:14 -07:00
parent f53e10bdf7
commit 1989f2e00e

View File

@@ -517,12 +517,23 @@ class SimpliSafe:
class SimpliSafeEntity(Entity): class SimpliSafeEntity(Entity):
"""Define a base SimpliSafe entity.""" """Define a base SimpliSafe entity."""
def __init__(self, simplisafe, system, name, *, serial=None): def __init__(
self,
simplisafe,
system,
name,
*,
serial=None,
respond_to_rest_api=True,
respond_to_websocket_api=True,
):
"""Initialize.""" """Initialize."""
self._async_unsub_dispatcher_connect = None self._async_unsub_dispatcher_connect = None
self._last_used_websocket_event = None self._last_used_websocket_event = None
self._name = name self._name = name
self._online = True self._online = True
self._respond_to_rest_api = respond_to_rest_api
self._respond_to_websocket_api = respond_to_websocket_api
self._simplisafe = simplisafe self._simplisafe = simplisafe
self._state = None self._state = None
self._system = system self._system = system
@@ -598,15 +609,16 @@ class SimpliSafeEntity(Entity):
async def async_update(self): async def async_update(self):
"""Update the entity.""" """Update the entity."""
if self._respond_to_rest_api:
self.async_update_from_rest_api() self.async_update_from_rest_api()
# Since the REST API triggers this coroutine, we don't want old websocket events if self._respond_to_websocket_api:
# to unnecessarily overwrite things; so, we return if the last websocket event # Since the REST API triggers this coroutine, we don't want old websocket
# is one the entity has already responded to: # events to unnecessarily overwrite things; so, we return if the last
# websocket event is one the entity has already responded to:
last_websocket_event = self._simplisafe.websocket.last_events.get( last_websocket_event = self._simplisafe.websocket.last_events.get(
self._system.system_id self._system.system_id
) )
if self._last_used_websocket_event == last_websocket_event: if self._last_used_websocket_event == last_websocket_event:
return return