mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Guard against flaky SimpliSafe API calls (#59175)
This commit is contained in:
parent
72aaeda8a0
commit
8ebd47b430
@ -102,9 +102,11 @@ ATTR_TIMESTAMP = "timestamp"
|
|||||||
|
|
||||||
DEFAULT_ENTITY_MODEL = "alarm_control_panel"
|
DEFAULT_ENTITY_MODEL = "alarm_control_panel"
|
||||||
DEFAULT_ENTITY_NAME = "Alarm Control Panel"
|
DEFAULT_ENTITY_NAME = "Alarm Control Panel"
|
||||||
|
DEFAULT_REST_API_ERROR_COUNT = 2
|
||||||
DEFAULT_SCAN_INTERVAL = timedelta(seconds=30)
|
DEFAULT_SCAN_INTERVAL = timedelta(seconds=30)
|
||||||
DEFAULT_SOCKET_MIN_RETRY = 15
|
DEFAULT_SOCKET_MIN_RETRY = 15
|
||||||
|
|
||||||
|
|
||||||
DISPATCHER_TOPIC_WEBSOCKET_EVENT = "simplisafe_websocket_event_{0}"
|
DISPATCHER_TOPIC_WEBSOCKET_EVENT = "simplisafe_websocket_event_{0}"
|
||||||
|
|
||||||
EVENT_SIMPLISAFE_EVENT = "SIMPLISAFE_EVENT"
|
EVENT_SIMPLISAFE_EVENT = "SIMPLISAFE_EVENT"
|
||||||
@ -553,6 +555,8 @@ class SimpliSafeEntity(CoordinatorEntity):
|
|||||||
assert simplisafe.coordinator
|
assert simplisafe.coordinator
|
||||||
super().__init__(simplisafe.coordinator)
|
super().__init__(simplisafe.coordinator)
|
||||||
|
|
||||||
|
self._rest_api_errors = 0
|
||||||
|
|
||||||
if device:
|
if device:
|
||||||
model = device.type.name
|
model = device.type.name
|
||||||
device_name = device.name
|
device_name = device.name
|
||||||
@ -615,11 +619,24 @@ class SimpliSafeEntity(CoordinatorEntity):
|
|||||||
else:
|
else:
|
||||||
system_offline = False
|
system_offline = False
|
||||||
|
|
||||||
return super().available and self._online and not system_offline
|
return (
|
||||||
|
self._rest_api_errors < DEFAULT_REST_API_ERROR_COUNT
|
||||||
|
and self._online
|
||||||
|
and not system_offline
|
||||||
|
)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _handle_coordinator_update(self) -> None:
|
def _handle_coordinator_update(self) -> None:
|
||||||
"""Update the entity with new REST API data."""
|
"""Update the entity with new REST API data."""
|
||||||
|
# SimpliSafe can incorrectly return an error state when there isn't any
|
||||||
|
# error. This can lead to the system having an unknown state frequently.
|
||||||
|
# To protect against that, we measure how many "error states" we receive
|
||||||
|
# and only alter the state if we detect a few in a row:
|
||||||
|
if self.coordinator.last_update_success:
|
||||||
|
self._rest_api_errors = 0
|
||||||
|
else:
|
||||||
|
self._rest_api_errors += 1
|
||||||
|
|
||||||
self.async_update_from_rest_api()
|
self.async_update_from_rest_api()
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
@ -71,8 +71,6 @@ ATTR_RF_JAMMING = "rf_jamming"
|
|||||||
ATTR_WALL_POWER_LEVEL = "wall_power_level"
|
ATTR_WALL_POWER_LEVEL = "wall_power_level"
|
||||||
ATTR_WIFI_STRENGTH = "wifi_strength"
|
ATTR_WIFI_STRENGTH = "wifi_strength"
|
||||||
|
|
||||||
DEFAULT_ERRORS_TO_ACCOMMODATE = 2
|
|
||||||
|
|
||||||
VOLUME_STRING_MAP = {
|
VOLUME_STRING_MAP = {
|
||||||
VOLUME_HIGH: "high",
|
VOLUME_HIGH: "high",
|
||||||
VOLUME_LOW: "low",
|
VOLUME_LOW: "low",
|
||||||
@ -140,8 +138,6 @@ class SimpliSafeAlarm(SimpliSafeEntity, AlarmControlPanelEntity):
|
|||||||
additional_websocket_events=WEBSOCKET_EVENTS_TO_LISTEN_FOR,
|
additional_websocket_events=WEBSOCKET_EVENTS_TO_LISTEN_FOR,
|
||||||
)
|
)
|
||||||
|
|
||||||
self._errors = 0
|
|
||||||
|
|
||||||
if code := self._simplisafe.entry.options.get(CONF_CODE):
|
if code := self._simplisafe.entry.options.get(CONF_CODE):
|
||||||
if code.isdigit():
|
if code.isdigit():
|
||||||
self._attr_code_format = FORMAT_NUMBER
|
self._attr_code_format = FORMAT_NUMBER
|
||||||
@ -248,19 +244,6 @@ class SimpliSafeAlarm(SimpliSafeEntity, AlarmControlPanelEntity):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
# SimpliSafe can incorrectly return an error state when there isn't any
|
|
||||||
# error. This can lead to the system having an unknown state frequently.
|
|
||||||
# To protect against that, we measure how many "error states" we receive
|
|
||||||
# and only alter the state if we detect a few in a row:
|
|
||||||
if self._system.state == SystemStates.error:
|
|
||||||
if self._errors > DEFAULT_ERRORS_TO_ACCOMMODATE:
|
|
||||||
self._attr_state = None
|
|
||||||
else:
|
|
||||||
self._errors += 1
|
|
||||||
return
|
|
||||||
|
|
||||||
self._errors = 0
|
|
||||||
|
|
||||||
self._set_state_from_system_data()
|
self._set_state_from_system_data()
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
|
Loading…
x
Reference in New Issue
Block a user