Skip to content

Commit 2d3043b

Browse files
authored
Fix status while disconnected, update tests accordingly (#96)
1 parent 4917552 commit 2d3043b

4 files changed

Lines changed: 30 additions & 31 deletions

File tree

pyotgw/connection.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,19 @@ async def _attempt_connect(self):
112112
self._retry_timeout = MIN_RETRY_TIMEOUT
113113
while transport is None:
114114
try:
115-
transport, protocol = await (
116-
serial_asyncio_fast.create_serial_connection(
117-
loop,
118-
partial(
119-
OpenThermProtocol,
120-
self._otgw.status,
121-
self.watchdog.inform,
122-
),
123-
self._port,
124-
write_timeout=0,
125-
**self._config,
126-
)
115+
(
116+
transport,
117+
protocol,
118+
) = await serial_asyncio_fast.create_serial_connection(
119+
loop,
120+
partial(
121+
OpenThermProtocol,
122+
self._otgw.status,
123+
self.watchdog.inform,
124+
),
125+
self._port,
126+
write_timeout=0,
127+
**self._config,
127128
)
128129
await asyncio.wait_for(
129130
protocol.init_and_wait_for_activity(),
@@ -169,6 +170,7 @@ async def _attempt_connect(self):
169170
async def _cleanup(self):
170171
"""Cleanup possible leftovers from old connections"""
171172
await self.watchdog.stop()
173+
self._otgw.status.reset()
172174
if self.protocol:
173175
await self.protocol.cleanup()
174176
if self._connecting_task is not None:

pyotgw/status.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def reset(self):
2525
while not self._updateq.empty():
2626
self._updateq.get_nowait()
2727
self._status = deepcopy(v.DEFAULT_STATUS)
28+
self._updateq.put_nowait(deepcopy(v.DEFAULT_STATUS))
2829

2930
@property
3031
def status(self):

tests/test_messageprocessor.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Test for pyotgw/messageprocessor.py"""
2+
23
import asyncio
34
import logging
45
import re
@@ -103,7 +104,7 @@ async def test_process_msgs(caplog, pygw_message_processor):
103104
"B",
104105
v.READ_ACK,
105106
b"\x23",
106-
b"\x0A",
107+
b"\x0a",
107108
b"\x01",
108109
)
109110
with patch.object(
@@ -155,6 +156,7 @@ async def empty_coroutine(status):
155156

156157
for test_case, expected_result in pygw_proto_messages:
157158
pygw_message_processor.status_manager.reset()
159+
pygw_message_processor.status_manager._updateq.get_nowait()
158160
await pygw_message_processor._process_msg(test_case)
159161
if expected_result is not None:
160162
await called_once(status_callback)
@@ -304,24 +306,16 @@ async def empty_coroutine(stat):
304306
return
305307

306308
with patch.object(
307-
pygw_message_processor.status_manager,
308-
"submit_partial_update"
309+
pygw_message_processor.status_manager, "submit_partial_update"
309310
) as partial_update:
310311
await pygw_message_processor._quirk_trset_s2m(
311312
v.THERMOSTAT,
312313
b"\x01",
313314
b"\x02",
314315
)
315-
await pygw_message_processor._quirk_trset_s2m(
316-
v.BOILER,
317-
b"\x14",
318-
b"\x80"
319-
)
316+
await pygw_message_processor._quirk_trset_s2m(v.BOILER, b"\x14", b"\x80")
320317

321-
partial_update.assert_called_once_with(
322-
v.BOILER,
323-
{v.DATA_ROOM_SETPOINT: 20.5}
324-
)
318+
partial_update.assert_called_once_with(v.BOILER, {v.DATA_ROOM_SETPOINT: 20.5})
325319

326320

327321
def test_get_flag8(pygw_message_processor):
@@ -373,7 +367,7 @@ def test_get_u8(pygw_message_processor):
373367
0,
374368
),
375369
(
376-
b"\xFF",
370+
b"\xff",
377371
255,
378372
),
379373
)
@@ -390,7 +384,7 @@ def test_get_s8(pygw_message_processor):
390384
0,
391385
),
392386
(
393-
b"\xFF",
387+
b"\xff",
394388
-1,
395389
),
396390
)
@@ -411,7 +405,7 @@ def test_get_f8_8(pygw_message_processor):
411405
),
412406
(
413407
(
414-
b"\xFF",
408+
b"\xff",
415409
b"\x80",
416410
),
417411
-0.5,
@@ -434,8 +428,8 @@ def test_get_u16(pygw_message_processor):
434428
),
435429
(
436430
(
437-
b"\xFF",
438-
b"\xFF",
431+
b"\xff",
432+
b"\xff",
439433
),
440434
65535,
441435
),
@@ -457,8 +451,8 @@ def test_get_s16(pygw_message_processor):
457451
),
458452
(
459453
(
460-
b"\xFF",
461-
b"\xFF",
454+
b"\xff",
455+
b"\xff",
462456
),
463457
-1,
464458
),

tests/test_status.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Tests for pyotgw/status.py"""
2+
23
import asyncio
34
import logging
45
from unittest.mock import MagicMock
@@ -21,6 +22,7 @@ def test_reset(pygw_status):
2122
pygw_status.reset()
2223

2324
assert pygw_status.status == v.DEFAULT_STATUS
25+
assert pygw_status._updateq.get_nowait() == v.DEFAULT_STATUS
2426
assert pygw_status._updateq.empty()
2527

2628

0 commit comments

Comments
 (0)