Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions kasa/smartdevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,20 +289,32 @@ async def get_sys_info(self) -> Dict[str, Any]:
"""Retrieve system information."""
return await self._query_helper("system", "get_sysinfo")

def _update_query(self, include_emeter=True) -> Dict[str, Dict[str, Any]]:
req: Dict[str, Dict[str, Any]] = {}
if include_emeter:
req.update(self._create_emeter_request())
req.update(self._create_request("system", "get_sysinfo"))
return req

async def _update_with_request(self, request):
self._last_update = await self.protocol.query(self.host, request)
self._sys_info = self._last_update["system"]["get_sysinfo"]

async def update(self):
"""Update some of the attributes.

Needed for methods that are decorated with `requires_update`.
"""
req = {}
req.update(self._create_request("system", "get_sysinfo"))
# Some devices without an emeter will crash if their emeter status
# is queried. Therefore, ensure the first update query is only for
# get_sysinfo.
if self._last_update is None:
initial_update_req = self._update_query(include_emeter=False)
await self._update_with_request(initial_update_req)
Copy link
Member

@rytilahti rytilahti Dec 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Save the response somewhere temporarily, that way there is no need to do the second query in the case if there's no emeter found.

Having two requests when the emeter is found is fine (as second one should've been made anyway).

Otherwise this looks good to me, but please add a unit test to check the update() to make sure no future refactoring will break this behavior!


# Check for emeter if we were never updated, or if the device has emeter
if self._last_update is None or self.has_emeter:
req.update(self._create_emeter_request())
self._last_update = await self.protocol.query(self.host, req)
# TODO: keep accessible for tests
self._sys_info = self._last_update["system"]["get_sysinfo"]
update_emeter = True if self.has_emeter else False
request = self._update_query(include_emeter=update_emeter)
await self._update_with_request(request)

@property # type: ignore
@requires_update
Expand Down