Skip to content

Commit b487516

Browse files
bdracorytilahti
authored andcommitted
Fix modularize with strips (#326)
* Fix test_deprecated_type stalling * Fix strips with modularize * Fix test_deprecated_type stalling (#325)
1 parent 46fb718 commit b487516

File tree

4 files changed

+24
-22
lines changed

4 files changed

+24
-22
lines changed

kasa/protocol.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,13 @@ async def query(self, request: Union[str, Dict], retry_count: int = 3) -> Dict:
7070
async with self.query_lock:
7171
return await self._query(request, retry_count, timeout)
7272

73-
async def _connect(self, timeout: int) -> bool:
73+
async def _connect(self, timeout: int) -> None:
7474
"""Try to connect or reconnect to the device."""
7575
if self.writer:
76-
return True
77-
78-
with contextlib.suppress(Exception):
79-
self.reader = self.writer = None
80-
task = asyncio.open_connection(
81-
self.host, TPLinkSmartHomeProtocol.DEFAULT_PORT
82-
)
83-
self.reader, self.writer = await asyncio.wait_for(task, timeout=timeout)
84-
return True
85-
86-
return False
76+
return
77+
self.reader = self.writer = None
78+
task = asyncio.open_connection(self.host, TPLinkSmartHomeProtocol.DEFAULT_PORT)
79+
self.reader, self.writer = await asyncio.wait_for(task, timeout=timeout)
8780

8881
async def _execute_query(self, request: str) -> Dict:
8982
"""Execute a query on the device and wait for the response."""
@@ -123,12 +116,14 @@ def _reset(self) -> None:
123116
async def _query(self, request: str, retry_count: int, timeout: int) -> Dict:
124117
"""Try to query a device."""
125118
for retry in range(retry_count + 1):
126-
if not await self._connect(timeout):
119+
try:
120+
await self._connect(timeout)
121+
except Exception as ex:
127122
await self.close()
128123
if retry >= retry_count:
129124
_LOGGER.debug("Giving up on %s after %s retries", self.host, retry)
130125
raise SmartDeviceException(
131-
f"Unable to connect to the device: {self.host}"
126+
f"Unable to connect to the device: {self.host}: {ex}"
132127
)
133128
continue
134129

kasa/smartdevice.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,11 @@ async def update(self, update_children: bool = True):
314314
self._last_update = await self.protocol.query(req)
315315
self._sys_info = self._last_update["system"]["get_sysinfo"]
316316

317+
await self._modular_update(req)
318+
self._sys_info = self._last_update["system"]["get_sysinfo"]
319+
320+
async def _modular_update(self, req: dict) -> None:
321+
"""Execute an update query."""
317322
if self.has_emeter:
318323
_LOGGER.debug(
319324
"The device has emeter, querying its information along sysinfo"
@@ -326,10 +331,9 @@ async def update(self, update_children: bool = True):
326331
continue
327332
q = module.query()
328333
_LOGGER.debug("Adding query for %s: %s", module, q)
329-
req = merge(req, module.query())
334+
req = merge(req, q)
330335

331336
self._last_update = await self.protocol.query(req)
332-
self._sys_info = self._last_update["system"]["get_sysinfo"]
333337

334338
def update_from_discover_info(self, info):
335339
"""Update state from info from the discover call."""

kasa/smartstrip.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
from collections import defaultdict
44
from datetime import datetime, timedelta
55
from typing import Any, DefaultDict, Dict, Optional
6-
6+
import asyncio
77
from kasa.smartdevice import (
88
DeviceType,
99
EmeterStatus,
1010
SmartDevice,
1111
SmartDeviceException,
1212
requires_update,
13+
merge,
1314
)
1415
from kasa.smartplug import SmartPlug
1516

@@ -250,16 +251,16 @@ def __init__(self, host: str, parent: "SmartStrip", child_id: str) -> None:
250251
self._last_update = parent._last_update
251252
self._sys_info = parent._sys_info
252253
self._device_type = DeviceType.StripSocket
254+
self.modules = {}
255+
self.protocol = parent.protocol # Must use the same connection as the parent
256+
self.add_module("time", Time(self, "time"))
253257

254258
async def update(self, update_children: bool = True):
255259
"""Query the device to update the data.
256260
257261
Needed for properties that are decorated with `requires_update`.
258262
"""
259-
# TODO: it needs to be checked if this still works after modularization
260-
self._last_update = await self.parent.protocol.query(
261-
self._create_emeter_request()
262-
)
263+
await self._modular_update({})
263264

264265
def _create_emeter_request(self, year: int = None, month: int = None):
265266
"""Create a request for requesting all emeter statistics at once."""

kasa/tests/test_smartdevice.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ async def test_initial_update_no_emeter(dev, mocker):
3636
dev._last_update = None
3737
spy = mocker.spy(dev.protocol, "query")
3838
await dev.update()
39-
assert spy.call_count == 1
39+
# 2 calls are necessary as some devices crash on unexpected modules
40+
# See #105, #120, #161
41+
assert spy.call_count == 2
4042

4143

4244
async def test_query_helper(dev):

0 commit comments

Comments
 (0)