Skip to content

Commit 7486e45

Browse files
authored
Automatically set state after system change (#268)
1 parent c458a9b commit 7486e45

3 files changed

Lines changed: 12 additions & 20 deletions

File tree

simplipy/system/__init__.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,6 @@ class SystemStates(Enum):
6767
unknown = 99
6868

6969

70-
def coerce_state_from_raw_value(value: str) -> SystemStates:
71-
"""Return a proper state from a string input."""
72-
try:
73-
return SystemStates[convert_to_underscore(value)]
74-
except KeyError:
75-
LOGGER.error("Unknown raw system state: %s", value)
76-
return SystemStates.unknown
77-
78-
7970
def get_device_type_from_data(device_data: dict[str, Any]) -> DeviceTypes:
8071
"""Get the device type of a raw data payload."""
8172
try:
@@ -423,8 +414,12 @@ async def async_update(
423414
]
424415

425416
# Set the current state:
426-
self._state = coerce_state_from_raw_value(
427-
self._api.subscription_data[self._sid]["location"]["system"].get(
428-
"alarmState"
429-
)
417+
raw_state = self._api.subscription_data[self._sid]["location"]["system"].get(
418+
"alarmState"
430419
)
420+
421+
try:
422+
self._state = SystemStates[convert_to_underscore(raw_state)]
423+
except KeyError:
424+
LOGGER.error("Unknown raw system state: %s", raw_state)
425+
self._state = SystemStates.unknown

simplipy/system/v2.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
DEFAULT_MAX_USER_PINS,
1010
System,
1111
SystemStates,
12-
coerce_state_from_raw_value,
1312
get_device_type_from_data,
1413
)
1514

@@ -43,14 +42,13 @@ class SystemV2(System):
4342

4443
async def _async_set_state(self, value: SystemStates) -> None:
4544
"""Set the state of the system."""
46-
state_resp = await self._api.request(
45+
await self._api.request(
4746
"post",
4847
f"subscriptions/{self.system_id}/state",
4948
params={"state": value.name},
5049
)
5150

52-
if state_resp["success"]:
53-
self._state = coerce_state_from_raw_value(state_resp["requestedState"])
51+
self._state = value
5452

5553
async def _async_set_updated_pins(self, pins: dict) -> None:
5654
"""Post new PINs."""

simplipy/system/v3.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
DEFAULT_MAX_USER_PINS,
1818
System,
1919
SystemStates,
20-
coerce_state_from_raw_value,
2120
get_device_type_from_data,
2221
guard_from_missing_data,
2322
)
@@ -345,11 +344,11 @@ def _generate_camera_data(self) -> dict[str, dict]:
345344

346345
async def _async_set_state(self, value: SystemStates) -> None:
347346
"""Set the state of the system."""
348-
state_resp = await self._api.request(
347+
await self._api.request(
349348
"post", f"ss3/subscriptions/{self.system_id}/state/{value.name}"
350349
)
351350

352-
self._state = coerce_state_from_raw_value(state_resp["state"])
351+
self._state = value
353352
self._last_state_change_dt = datetime.utcnow()
354353

355354
async def _async_set_updated_pins(self, pins: dict) -> None:

0 commit comments

Comments
 (0)