Skip to content

Commit 366df35

Browse files
committed
Moved logic of device update into MyQDevice class
1 parent c99927d commit 366df35

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

pymyq/account.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,25 +111,10 @@ async def _get_devices(self) -> None:
111111
f"Updating information for device with serial number {serial_number}"
112112
)
113113
myqdevice = self._devices[serial_number]
114+
myqdevice.update(
115+
device_json=device, state_update_timestmp=state_update_timestmp
116+
)
114117

115-
# When performing commands we might update the state temporary, need to ensure
116-
# that the state is not set back to something else if MyQ does not yet have updated
117-
# state
118-
last_update = myqdevice.device_json["state"].get("last_update")
119-
myqdevice.device_json = device
120-
121-
if (
122-
myqdevice.device_json["state"].get("last_update") is not None
123-
and myqdevice.device_json["state"].get("last_update")
124-
!= last_update
125-
):
126-
# MyQ has updated device state, reset ours ensuring we have the one from MyQ.
127-
myqdevice.state = None
128-
_LOGGER.debug(
129-
f"State for device {myqdevice.name} was updated to {myqdevice.state}"
130-
)
131-
132-
myqdevice.state_update = state_update_timestmp
133118
else:
134119
if device.get("device_family") == DEVICE_FAMILY_GARAGEDOOR:
135120
_LOGGER.debug(

pymyq/device.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,25 @@ async def turnoff(self, wait_for_state: bool = False) -> Union[asyncio.Task, boo
115115
async def turnon(self, wait_for_state: bool = False) -> Union[asyncio.Task, bool]:
116116
raise NotImplementedError
117117

118+
async def update_device(self, device_json: dict, state_update_timestmp: datetime):
119+
# When performing commands we might update the state temporary, need to ensure
120+
# that the state is not set back to something else if MyQ does not yet have updated
121+
# state
122+
last_update = self.device_json["state"].get("last_update")
123+
self.device_json = device_json
124+
125+
if (
126+
self.device_json["state"].get("last_update") is not None
127+
and self.device_json["state"].get("last_update") != last_update
128+
):
129+
# MyQ has updated device state, reset ours ensuring we have the one from MyQ.
130+
self._device_state = None
131+
_LOGGER.debug(
132+
"State for device %s was updated to %s", self.name, self.state
133+
)
134+
135+
self.state_update = state_update_timestmp
136+
118137
async def _send_state_command(self, url: str, command: str) -> None:
119138
"""Instruct the API to change the state of the device."""
120139
# If the user tries to open or close, say, a gateway, throw an exception:
@@ -172,6 +191,6 @@ async def wait_for_state(
172191
# Reset self.state ensuring it reflects actual device state. Only do this if state is still what it would
173192
# have been, this to ensure if something else had updated it to something else we don't override.
174193
if self._device_state == current_state:
175-
self.state = None
194+
self._device_state = None
176195

177196
return self.state in new_state

0 commit comments

Comments
 (0)