1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
|
"""Test for pyotgw/messageprocessor.py"""
import asyncio
import logging
import re
from unittest.mock import MagicMock, patch
import pytest
from pyotgw import vars as v
from tests.data import pygw_proto_messages
from tests.helpers import called_once
MATCH_PATTERN = r"^(T|B|R|A|E)([0-9A-F]{8})$"
@pytest.mark.asyncio
async def test_cleanup(pygw_message_processor):
"""Test MessageProcessor.cleanup()"""
assert pygw_message_processor._task
await pygw_message_processor.cleanup()
assert not pygw_message_processor._task
def test_connection_lost(pygw_message_processor):
"""Test MessageProcessor.connection_lost()"""
message = re.match(MATCH_PATTERN, "A01020304")
pygw_message_processor.submit_matched_message(message)
pygw_message_processor.submit_matched_message(message)
pygw_message_processor.submit_matched_message(message)
pygw_message_processor.connection_lost()
assert pygw_message_processor._msgq.empty()
def test_submit_matched_message(caplog, pygw_message_processor):
"""Tests MessageProcessor.submit_matched_message()"""
bad_match = re.match(MATCH_PATTERN, "E01020304")
good_match = re.match(MATCH_PATTERN, "A01020304")
pygw_message_processor.submit_matched_message(bad_match)
assert pygw_message_processor._msgq.empty()
with caplog.at_level(logging.DEBUG):
pygw_message_processor.submit_matched_message(good_match)
assert caplog.record_tuples == [
(
"pyotgw.messageprocessor",
logging.DEBUG,
"Added line to message queue. Queue size: 1",
),
]
assert pygw_message_processor._msgq.get_nowait() == (
"A",
v.READ_DATA,
b"\x02",
b"\x03",
b"\x04",
)
def test_dissect_msg(caplog, pygw_message_processor):
"""Test MessageProcessor._dissect_msg"""
pat = r"^(T|B|R|A|E)([0-9A-F]{8})$"
test_matches = (
re.match(pat, "A10203040"),
re.match(pat, "EEEEEEEEE"),
re.match(pat, "AEEEEEEEE"),
)
none_tuple = (None, None, None, None, None)
assert pygw_message_processor._dissect_msg(test_matches[0]) == (
"A",
v.WRITE_DATA,
b"\x20",
b"\x30",
b"\x40",
)
with caplog.at_level(logging.INFO):
assert pygw_message_processor._dissect_msg(test_matches[1]) == none_tuple
assert caplog.record_tuples == [
(
"pyotgw.messageprocessor",
logging.INFO,
"The OpenTherm Gateway received an erroneous message."
" This is not a bug in pyotgw. Ignoring: EEEEEEEE",
)
]
assert pygw_message_processor._dissect_msg(test_matches[2]) == none_tuple
def test_get_msgtype(pygw_message_processor):
"""Test MessageProcessor._get_msgtype()"""
assert pygw_message_processor._get_msgtype(int("11011111", 2)) == int("0101", 2)
assert pygw_message_processor._get_msgtype(int("01000001", 2)) == int("0100", 2)
@pytest.mark.asyncio
async def test_process_msgs(caplog, pygw_message_processor):
"""Test MessageProcessor._process_msgs()"""
test_case = (
"B",
v.READ_ACK,
b"\x23",
b"\x0A",
b"\x01",
)
with patch.object(
pygw_message_processor, "_process_msg"
) as process_msg, caplog.at_level(logging.DEBUG):
task = asyncio.create_task(pygw_message_processor._process_msgs())
pygw_message_processor._msgq.put_nowait(test_case)
await called_once(process_msg)
task.cancel()
with pytest.raises(asyncio.CancelledError):
await task
process_msg.assert_called_once_with(test_case)
assert caplog.record_tuples == [
("pyotgw.messageprocessor", logging.DEBUG, "Processing: B 04 23 0A 01")
]
@pytest.mark.asyncio
async def test_process_msg(pygw_message_processor):
"""Test MessageProcessor._process_msg()"""
# Test quirks
test_case = (
"B",
v.READ_ACK,
v.MSG_TROVRD,
b"\x10",
b"\x80",
)
with patch.object(
pygw_message_processor, "_quirk_trovrd", return_value=None
) as quirk_trovrd:
await pygw_message_processor._process_msg(test_case)
quirk_trovrd.assert_called_once_with(
v.BOILER,
"B",
b"\x10",
b"\x80",
)
async def empty_coroutine(status):
return
status_callback = MagicMock(side_effect=empty_coroutine)
pygw_message_processor.status_manager.subscribe(status_callback)
for test_case, expected_result in pygw_proto_messages:
pygw_message_processor.status_manager.reset()
await pygw_message_processor._process_msg(test_case)
if expected_result is not None:
await called_once(status_callback)
status_callback.assert_called_once_with(expected_result)
status_callback.reset_mock()
@pytest.mark.asyncio
async def test_get_dict_update_for_action():
"""Test MessageProcessor._get_dict_update_for_action"""
assert True # Fully tested in test_process_msg()
@pytest.mark.asyncio
async def test_quirk_trovrd(pygw_message_processor):
"""Test MessageProcessor._quirk_trovrd()"""
async def empty_coroutine(stat):
return
status_callback = MagicMock(side_effect=empty_coroutine)
pygw_message_processor.status_manager.subscribe(status_callback)
pygw_message_processor.status_manager.submit_partial_update(
v.OTGW,
{v.OTGW_THRM_DETECT: "I"},
)
await called_once(status_callback)
status_callback.reset_mock()
with patch.object(
pygw_message_processor.command_processor,
"issue_cmd",
return_value="O=c19.5",
):
await pygw_message_processor._quirk_trovrd(
v.THERMOSTAT,
"A",
b"\x15",
b"\x40",
)
await called_once(status_callback)
status_callback.assert_called_once_with(
{
v.BOILER: {},
v.OTGW: {v.OTGW_THRM_DETECT: "I"},
v.THERMOSTAT: {v.DATA_ROOM_SETPOINT_OVRD: 19.5},
}
)
with patch.object(
pygw_message_processor.command_processor,
"issue_cmd",
return_value="O=q---",
), patch.object(
pygw_message_processor.status_manager,
"submit_partial_update",
) as partial_update, patch.object(
pygw_message_processor.status_manager,
"delete_value",
) as delete_value:
await pygw_message_processor._quirk_trovrd(
v.THERMOSTAT,
"A",
b"\x15",
b"\x40",
)
partial_update.assert_not_called()
delete_value.assert_not_called()
assert (
v.DATA_ROOM_SETPOINT_OVRD
in pygw_message_processor.status_manager.status[v.THERMOSTAT]
)
status_callback.reset_mock()
await pygw_message_processor._quirk_trovrd(
v.THERMOSTAT,
"A",
b"\x00",
b"\x00",
)
await called_once(status_callback)
status_callback.assert_called_once_with(
{
v.BOILER: {},
v.OTGW: {v.OTGW_THRM_DETECT: "I"},
v.THERMOSTAT: {},
}
)
status_callback.reset_mock()
pygw_message_processor.status_manager.submit_partial_update(
v.OTGW, {v.OTGW_THRM_DETECT: "D"}
)
await called_once(status_callback)
status_callback.reset_mock()
await pygw_message_processor._quirk_trovrd(
v.THERMOSTAT,
"A",
b"\x15",
b"\x40",
)
await called_once(status_callback)
status_callback.assert_called_once_with(
{
v.BOILER: {},
v.OTGW: {v.OTGW_THRM_DETECT: "D"},
v.THERMOSTAT: {v.DATA_ROOM_SETPOINT_OVRD: 21.25},
}
)
status_callback.reset_mock()
pygw_message_processor.status_manager.submit_partial_update(
v.OTGW, {v.OTGW_THRM_DETECT: "I"}
)
await called_once(status_callback)
status_callback.reset_mock()
with patch.object(
pygw_message_processor.command_processor,
"issue_cmd",
return_value="O=N",
):
await pygw_message_processor._quirk_trovrd(
v.THERMOSTAT,
"A",
b"\x15",
b"\x40",
)
await called_once(status_callback)
status_callback.assert_called_once_with(
{
v.BOILER: {},
v.OTGW: {v.OTGW_THRM_DETECT: "I"},
v.THERMOSTAT: {},
}
)
@pytest.mark.asyncio
async def test_quirk_trset_s2m(pygw_message_processor):
"""Test MessageProcessor._quirk_trset_s2m()"""
async def empty_coroutine(stat):
return
with patch.object(
pygw_message_processor.status_manager,
"submit_partial_update"
) as partial_update:
await pygw_message_processor._quirk_trset_s2m(
v.THERMOSTAT,
b"\x01",
b"\x02",
)
await pygw_message_processor._quirk_trset_s2m(
v.BOILER,
b"\x14",
b"\x80"
)
partial_update.assert_called_once_with(
v.BOILER,
{v.DATA_ROOM_SETPOINT: 20.5}
)
def test_get_flag8(pygw_message_processor):
"""Test pygw._get_flag8()"""
test_cases = (
(
int("00000001", 2).to_bytes(1, "big"),
[1, 0, 0, 0, 0, 0, 0, 0],
),
(
int("00000010", 2).to_bytes(1, "big"),
[0, 1, 0, 0, 0, 0, 0, 0],
),
(
int("00000100", 2).to_bytes(1, "big"),
[0, 0, 1, 0, 0, 0, 0, 0],
),
(
int("00001000", 2).to_bytes(1, "big"),
[0, 0, 0, 1, 0, 0, 0, 0],
),
(
int("00010000", 2).to_bytes(1, "big"),
[0, 0, 0, 0, 1, 0, 0, 0],
),
(
int("00100000", 2).to_bytes(1, "big"),
[0, 0, 0, 0, 0, 1, 0, 0],
),
(
int("01000000", 2).to_bytes(1, "big"),
[0, 0, 0, 0, 0, 0, 1, 0],
),
(
int("10000000", 2).to_bytes(1, "big"),
[0, 0, 0, 0, 0, 0, 0, 1],
),
)
for case, res in test_cases:
assert pygw_message_processor._get_flag8(case) == res
def test_get_u8(pygw_message_processor):
"""Test pygw._get_u8()"""
test_cases = (
(
b"\x00",
0,
),
(
b"\xFF",
255,
),
)
for case, res in test_cases:
assert pygw_message_processor._get_u8(case) == res
def test_get_s8(pygw_message_processor):
"""Test pygw._get_s8()"""
test_cases = (
(
b"\x00",
0,
),
(
b"\xFF",
-1,
),
)
for case, res in test_cases:
assert pygw_message_processor._get_s8(case) == res
def test_get_f8_8(pygw_message_processor):
"""Test pygw._get_f8_8()"""
test_cases = (
(
(
b"\x00",
b"\x00",
),
0.0,
),
(
(
b"\xFF",
b"\x80",
),
-0.5,
),
)
for case, res in test_cases:
assert pygw_message_processor._get_f8_8(*case) == res
def test_get_u16(pygw_message_processor):
"""Test pygw._get_u16()"""
test_cases = (
(
(
b"\x00",
b"\x00",
),
0,
),
(
(
b"\xFF",
b"\xFF",
),
65535,
),
)
for case, res in test_cases:
assert pygw_message_processor._get_u16(*case) == res
def test_get_s16(pygw_message_processor):
"""Test pygw._get_s16()"""
test_cases = (
(
(
b"\x00",
b"\x00",
),
0,
),
(
(
b"\xFF",
b"\xFF",
),
-1,
),
)
for case, res in test_cases:
assert pygw_message_processor._get_s16(*case) == res
|