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):
"""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."""
self._async_unsub_dispatcher_connect = None
self._last_used_websocket_event = None
self._name = name
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._state = None
self._system = system
@ -598,28 +609,29 @@ class SimpliSafeEntity(Entity):
async def async_update(self):
"""Update the entity."""
self.async_update_from_rest_api()
if self._respond_to_rest_api:
self.async_update_from_rest_api()
# Since the REST API triggers this coroutine, we don't want old websocket 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(
self._system.system_id
)
if self._respond_to_websocket_api:
# Since the REST API triggers this coroutine, we don't want old websocket
# 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(
self._system.system_id
)
if self._last_used_websocket_event == last_websocket_event:
return
if self._last_used_websocket_event == last_websocket_event:
return
self._last_used_websocket_event = last_websocket_event
self._attrs.update(
{
ATTR_LAST_EVENT_INFO: last_websocket_event.info,
ATTR_LAST_EVENT_SENSOR_NAME: last_websocket_event.sensor_name,
ATTR_LAST_EVENT_SENSOR_TYPE: last_websocket_event.sensor_type,
ATTR_LAST_EVENT_TIMESTAMP: last_websocket_event.timestamp,
}
)
self.async_update_from_websocket_event(last_websocket_event)
self._last_used_websocket_event = last_websocket_event
self._attrs.update(
{
ATTR_LAST_EVENT_INFO: last_websocket_event.info,
ATTR_LAST_EVENT_SENSOR_NAME: last_websocket_event.sensor_name,
ATTR_LAST_EVENT_SENSOR_TYPE: last_websocket_event.sensor_type,
ATTR_LAST_EVENT_TIMESTAMP: last_websocket_event.timestamp,
}
)
self.async_update_from_websocket_event(last_websocket_event)
@callback
def async_update_from_rest_api(self):