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 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
|
# Copyright Tomer Figenblat.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Switcher integration TCP socket API module test cases."""
import os
from asyncio.streams import StreamReader, StreamWriter
from binascii import hexlify, unhexlify
from datetime import timedelta
from unittest import skipUnless
from unittest.mock import AsyncMock, Mock, patch
import pytest_asyncio
from assertpy import assert_that
from pytest import fixture, mark, raises
from aioswitcher.api import Command, SwitcherType1Api, SwitcherType2Api
from aioswitcher.api.messages import (
SwitcherBaseResponse,
SwitcherGetSchedulesResponse,
SwitcherLightStateResponse,
SwitcherLoginResponse,
SwitcherShutterStateResponse,
SwitcherStateResponse,
SwitcherThermostatStateResponse,
)
from aioswitcher.api.remotes import (
SwitcherBreezeCommand,
SwitcherBreezeRemote,
SwitcherBreezeRemoteManager,
)
from aioswitcher.device import (
DeviceState,
DeviceType,
ThermostatFanLevel,
ThermostatMode,
ThermostatSwing,
)
device_type_api1 = DeviceType.TOUCH
device_type_api2 = DeviceType.RUNNER
device_type_token_api2 = DeviceType.RUNNER_S11
device_index = 0
device_index2 = 1
device_id = "aaaaaa"
device_key = "18"
device_ip = "1.2.3.4"
token_empty = ""
token_not_empty = "zvVvd7JxtN7CgvkD1Psujw=="
pytestmark = mark.asyncio
faulty_dummy_response = skipUnless(
os.environ.get('CI'),
'this fails because "get_breeze_state.txt" dummy response is faulty, it is ON but fails temperature parsing'
)
@fixture
def writer_write():
return Mock()
@fixture
def reader_mock():
return AsyncMock(spec_set=StreamReader)
@fixture
def writer_mock(writer_write):
writer = AsyncMock(spec_set=StreamWriter)
writer.write = writer_write
return writer
@pytest_asyncio.fixture
async def connected_api_type1(reader_mock, writer_mock):
with patch("aioswitcher.api.open_connection", return_value=(reader_mock, writer_mock)):
api = SwitcherType1Api(device_type_api1, device_ip, device_id, device_key)
await api.connect()
yield api
await api.disconnect()
@pytest_asyncio.fixture
async def connected_api_type2(reader_mock, writer_mock):
with patch("aioswitcher.api.open_connection", return_value=(reader_mock, writer_mock)):
api = SwitcherType2Api(device_type_api2, device_ip, device_id, device_key, token_empty)
await api.connect()
yield api
await api.disconnect()
@pytest_asyncio.fixture
async def connected_api_token_type2(reader_mock, writer_mock):
with patch("aioswitcher.api.open_connection", return_value=(reader_mock, writer_mock)):
api = SwitcherType2Api(device_type_token_api2, device_ip, device_id, device_key, token_not_empty)
await api.connect()
yield api
await api.disconnect()
@patch("logging.Logger.info")
async def test_stopping_before_started_and_connected_should_write_to_the_info_output(mock_info):
api = SwitcherType1Api(device_type_api1, device_ip, device_id, device_key)
assert_that(api.connected).is_false()
await api.disconnect()
mock_info.assert_called_with("switcher device not connected")
async def test_api_as_a_context_manager(reader_mock, writer_mock):
with patch("aioswitcher.api.open_connection", return_value=(reader_mock, writer_mock)):
async with SwitcherType1Api(device_type_api1, device_ip, device_id, device_key) as api:
assert_that(api.connected).is_true()
async def test_api_with_token_needed_but_missing_should_raise_error():
with raises(RuntimeError, match="A token is needed but is missing"):
with patch("aioswitcher.api.open_connection", return_value=b''):
await SwitcherType2Api(device_type_token_api2, device_ip, device_id, device_key, token_empty)
async def test_login_function(reader_mock, writer_write, connected_api_type1, resource_path_root):
response_packet = _load_dummy_packet(resource_path_root, "login_response")
with patch.object(reader_mock, "read", return_value=response_packet):
response = await connected_api_type1._login()
writer_write.assert_called_once()
assert_that(response[1]).is_instance_of(SwitcherLoginResponse)
assert_that(response[1].unparsed_response).is_equal_to(response_packet)
async def test_login2_function(reader_mock, writer_write, connected_api_type2, resource_path_root):
response_packet = _load_dummy_packet(resource_path_root, "login2_response")
with patch.object(reader_mock, "read", return_value=response_packet):
response = await connected_api_type2._login()
writer_write.assert_called_once()
assert_that(response[1]).is_instance_of(SwitcherLoginResponse)
assert_that(response[1].unparsed_response).is_equal_to(response_packet)
async def test_login_token_function(reader_mock, writer_write, connected_api_token_type2, resource_path_root):
response_packet = _load_dummy_packet(resource_path_root, "login_response")
with patch.object(reader_mock, "read", return_value=response_packet):
response = await connected_api_token_type2._login()
assert_that(writer_write.call_count).is_equal_to(2)
assert_that(response[1]).is_instance_of(SwitcherLoginResponse)
assert_that(response[1].unparsed_response).is_equal_to(response_packet)
async def test_get_state_function_with_a_faulty_login_response_should_raise_error(reader_mock, writer_write, connected_api_type1):
with raises(RuntimeError, match="login request was not successful"):
with patch.object(reader_mock, "read", return_value=b''):
await connected_api_type1.get_state()
writer_write.assert_called_once()
async def test_get_state_function_with_a_faulty_get_state_response_should_raise_error(reader_mock, writer_write, connected_api_type1, resource_path_root):
login_response_packet = _load_dummy_packet(resource_path_root, "login_response")
with raises(RuntimeError, match="get state request was not successful"):
with patch.object(reader_mock, "read", side_effect=[login_response_packet, b'']):
await connected_api_type1.get_state()
assert_that(writer_write.call_count).is_equal_to(2)
async def test_get_state_function_with_valid_packets(reader_mock, writer_write, connected_api_type1, resource_path_root):
login_response_packet = _load_dummy_packet(resource_path_root, "login_response")
get_state_response_packet = _load_dummy_packet(resource_path_root, "get_state_response")
with patch.object(reader_mock, "read", side_effect=[login_response_packet, get_state_response_packet]):
response = await connected_api_type1.get_state()
assert_that(writer_write.call_count).is_equal_to(2)
assert_that(response).is_instance_of(SwitcherStateResponse)
assert_that(response.unparsed_response).is_equal_to(get_state_response_packet)
async def test_get_breeze_state_function_with_valid_packets(reader_mock, writer_write, connected_api_type2, resource_path_root):
login_response_packet = _load_dummy_packet(resource_path_root, "login2_response")
get_breeze_state_response_packet = _load_dummy_packet(resource_path_root, "get_breeze_state")
with patch.object(reader_mock, "read", side_effect=[login_response_packet, get_breeze_state_response_packet]):
response = await connected_api_type2.get_breeze_state()
assert_that(writer_write.call_count).is_equal_to(2)
assert_that(response).is_instance_of(SwitcherThermostatStateResponse)
assert_that(response.unparsed_response).is_equal_to(get_breeze_state_response_packet)
async def test_turn_on_function_with_valid_packets(reader_mock, writer_write, connected_api_type1, resource_path_root):
two_packets = _get_dummy_packets(resource_path_root, "login_response", "turn_on_response")
with patch.object(reader_mock, "read", side_effect=two_packets):
response = await connected_api_type1.control_device(Command.ON)
assert_that(writer_write.call_count).is_equal_to(2)
assert_that(response).is_instance_of(SwitcherBaseResponse)
assert_that(response.unparsed_response).is_equal_to(two_packets[-1])
async def test_get_breeze_state_function_with_a_faulty_login_response_should_raise_error(reader_mock, writer_write, connected_api_type2):
with raises(RuntimeError, match="login request was not successful"):
with patch.object(reader_mock, "read", return_value=b''):
await connected_api_type2.get_breeze_state()
writer_write.assert_called_once()
async def test_get_breeze_state_function_with_a_faulty_get_state_response_should_raise_error(reader_mock, writer_write, connected_api_type2, resource_path_root):
login_response_packet = _load_dummy_packet(resource_path_root, "login_response")
with raises(RuntimeError, match="get breeze state request was not successful"):
with patch.object(reader_mock, "read", side_effect=[login_response_packet, b'']):
await connected_api_type2.get_breeze_state()
assert_that(writer_write.call_count).is_equal_to(2)
async def test_control_breeze_device_function_with_valid_packets(reader_mock, writer_write, connected_api_type2, resource_path_root):
four_packets = _get_dummy_packets(resource_path_root, "login2_response", "get_breeze_state", "control_breeze_response", "control_breeze_swing_response")
with patch.object(reader_mock, "read", side_effect=four_packets):
remote = SwitcherBreezeRemoteManager().get_remote('ELEC7022')
response = await connected_api_type2.control_breeze_device(remote, DeviceState.ON, ThermostatMode.COOL, 24, ThermostatFanLevel.HIGH, ThermostatSwing.ON)
assert_that(writer_write.call_count).is_equal_to(4)
assert_that(response).is_instance_of(SwitcherBaseResponse)
assert_that(response.unparsed_response).is_equal_to(four_packets[-1])
async def test_control_breeze_device_update_state_with_valid_packets(reader_mock, writer_write, connected_api_type2, resource_path_root):
three_packets = _get_dummy_packets(resource_path_root, "login2_response", "get_breeze_state", "control_breeze_response")
with patch.object(reader_mock, "read", side_effect=three_packets):
remote = SwitcherBreezeRemoteManager().get_remote("ELEC7022")
response = await connected_api_type2.control_breeze_device(remote, DeviceState.ON, ThermostatMode.COOL, 24, ThermostatFanLevel.HIGH, ThermostatSwing.ON, True)
assert_that(writer_write.call_count).is_equal_to(3)
assert_that(response).is_instance_of(SwitcherBaseResponse)
assert_that(response.unparsed_response).is_equal_to(three_packets[-1])
async def test_breeze_remote_min_max_temp():
remote = SwitcherBreezeRemoteManager().get_remote('ELEC7001')
max_temp = remote.max_temperature
min_temp = remote.min_temperature
assert_that(min_temp).is_equal_to(16)
assert_that(min_temp).is_instance_of(int)
assert_that(max_temp).is_equal_to(30)
assert_that(max_temp).is_instance_of(int)
async def test_breeze_get_remote_id():
remote = SwitcherBreezeRemoteManager().get_remote('ELEC7001')
remote_id = remote.remote_id
assert_that(remote_id).is_equal_to("ELEC7001")
assert_that(remote_id).is_instance_of(str)
async def test_breeze_get_on_off_type():
remote = SwitcherBreezeRemoteManager().get_remote('ELEC7001')
on_off_type = remote.on_off_type
assert_that(on_off_type).is_equal_to(True)
assert_that(on_off_type).is_instance_of(bool)
async def test_control_breeze_function_with_a_faulty_get_state_response_should_raise_error(reader_mock, writer_write, connected_api_type2):
with raises(RuntimeError, match="login request was not successful"):
remote = SwitcherBreezeRemoteManager().get_remote('ELEC7022')
with patch.object(reader_mock, "read", return_value=b''):
await connected_api_type2.control_breeze_device(remote, DeviceState.ON, ThermostatMode.COOL, 24, ThermostatFanLevel.HIGH, ThermostatSwing.ON)
writer_write.assert_called_once()
async def test_get_breeze_command_function_with_low_temp(reader_mock, writer_write, connected_api_type2, resource_path_root):
four_packets = _get_dummy_packets(resource_path_root, "login2_response", "get_breeze_state", "control_breeze_response", "control_breeze_swing_response")
with patch.object(reader_mock, "read", side_effect=four_packets):
remote = SwitcherBreezeRemoteManager().get_remote('ELEC7022')
response = await connected_api_type2.control_breeze_device(remote, DeviceState.ON, ThermostatMode.COOL, 10, ThermostatFanLevel.HIGH, ThermostatSwing.ON)
assert_that(writer_write.call_count).is_equal_to(4)
assert_that(response).is_instance_of(SwitcherBaseResponse)
assert_that(response.unparsed_response).is_equal_to(four_packets[-1])
async def test_get_breeze_command_function_with_high_temp(reader_mock, writer_write, connected_api_type2, resource_path_root):
four_packets = _get_dummy_packets(resource_path_root, "login2_response", "get_breeze_state", "control_breeze_response", "control_breeze_swing_response")
with patch.object(reader_mock, "read", side_effect=four_packets):
remote = SwitcherBreezeRemoteManager().get_remote('ELEC7022')
response = await connected_api_type2.control_breeze_device(remote, DeviceState.ON, ThermostatMode.COOL, 100, ThermostatFanLevel.HIGH, ThermostatSwing.ON)
assert_that(writer_write.call_count).is_equal_to(4)
assert_that(response).is_instance_of(SwitcherBaseResponse)
assert_that(response.unparsed_response).is_equal_to(four_packets[-1])
async def test_breeze_get_command_function_with_non_supported_mode(resource_path_root):
# test invalid non existing mode (cool)
brm = SwitcherBreezeRemoteManager(str(resource_path_root) + "/breeze_data/irset_db_invalid_elec7022_data.json")
remote = brm.get_remote('ELEC7022')
with raises(RuntimeError, match=f"Invalid mode \"{ThermostatMode.COOL.display}\", available modes for this device are: {', '.join([x.display for x in remote.supported_modes])}"):
remote.build_command(DeviceState.ON, ThermostatMode.COOL, 20, ThermostatFanLevel.HIGH, ThermostatSwing.ON, DeviceState.OFF)
async def test_breeze_get_command_function_non_toggle_type_off_state(resource_path_root):
elec7022_turn_off_cmd = unhexlify((resource_path_root / ("breeze_data/" + "breeze_elec7022_turn_off_command" + ".txt")).read_text().replace('\n', '').encode())
remote = SwitcherBreezeRemoteManager().get_remote('ELEC7022')
command = remote.build_command(DeviceState.OFF, ThermostatMode.DRY, 20, ThermostatFanLevel.HIGH, ThermostatSwing.ON, DeviceState.OFF)
assert_that(command).is_instance_of(SwitcherBreezeCommand)
assert_that(command.command).is_equal_to(hexlify(elec7022_turn_off_cmd).decode())
async def test_breeze_get_command_function_toggle_type(resource_path_root):
elec7001_turn_off_cmd = unhexlify((resource_path_root / ("breeze_data/" + "breeze_elec7001_turn_off_command" + ".txt")).read_text().replace('\n', '').encode())
remote = SwitcherBreezeRemoteManager().get_remote('ELEC7001')
command = remote.build_command(DeviceState.OFF, ThermostatMode.DRY, 20, ThermostatFanLevel.HIGH, ThermostatSwing.ON, DeviceState.ON)
assert_that(command).is_instance_of(SwitcherBreezeCommand)
assert_that(command.command).is_equal_to(hexlify(elec7001_turn_off_cmd).decode())
async def test_breeze_get_command_function_should_raise_command_does_not_exist(resource_path_root):
elec7001_turn_off_cmd = unhexlify((resource_path_root / ("breeze_data/" + "breeze_elec7001_turn_off_command" + ".txt")).read_text().replace('\n', '').encode())
remote = SwitcherBreezeRemoteManager().get_remote('ELEC7001')
command = remote.build_command(DeviceState.OFF, ThermostatMode.DRY, 20, ThermostatFanLevel.HIGH, ThermostatSwing.ON, DeviceState.ON)
assert_that(command).is_instance_of(SwitcherBreezeCommand)
assert_that(command.command).is_equal_to(hexlify(elec7001_turn_off_cmd).decode())
async def test_breeze_remote_manager_get_from_local_database():
remote_manager = SwitcherBreezeRemoteManager()
remote_7022 = remote_manager.get_remote("ELEC7022")
assert_that(remote_7022).is_type_of(SwitcherBreezeRemote)
assert_that(remote_7022.remote_id).is_equal_to("ELEC7022")
async def test_breeze_build_swing_command():
remote_manager = SwitcherBreezeRemoteManager()
remote_7022 = remote_manager.get_remote("ELEC7022")
command = remote_7022.build_swing_command(swing=ThermostatSwing.ON)
assert_that(command.command).is_equal_to("000000004e4543587c32367c33327c31352c31357c31352c34307c31357c54303042457c33307c30317c414241425b33305d7c423234443642393445303146")
async def test_breeze_build_command_function_invalid_mode(resource_path_root):
brm = SwitcherBreezeRemoteManager(str(resource_path_root) + "/breeze_data/irset_db_invalid_elec7022_data.json")
remote = brm.get_remote('ELEC7022')
with raises(RuntimeError, match="Invalid mode \"cool\", available modes for this device are: auto, dry, fan"):
remote.build_command(DeviceState.ON, ThermostatMode.COOL, 20, ThermostatFanLevel.AUTO, ThermostatSwing.ON, DeviceState.OFF)
async def test_breeze_build_command_function_specific_case():
remote_manager = SwitcherBreezeRemoteManager()
remote_7001 = remote_manager.get_remote("ELEC7001")
command = remote_7001.build_command(DeviceState.OFF, ThermostatMode.COOL, 20, ThermostatFanLevel.HIGH, ThermostatSwing.ON, DeviceState.ON)
assert_that(command.command).is_equal_to("00000000524337327c32317c33327c32367c34437c39387c537c32327c30337c373237325b32325d7c39383841303030303830")
async def test_turn_on_with_timer_function_with_valid_packets(reader_mock, writer_write, resource_path_root, connected_api_type1):
two_packets = _get_dummy_packets(resource_path_root, "login_response", "turn_on_with_timer_response")
with patch.object(reader_mock, "read", side_effect=two_packets):
response = await connected_api_type1.control_device(Command.ON, 15)
assert_that(writer_write.call_count).is_equal_to(2)
assert_that(response).is_instance_of(SwitcherBaseResponse)
assert_that(response.unparsed_response).is_equal_to(two_packets[-1])
async def test_turn_off_function_with_valid_packets(reader_mock, writer_write, connected_api_type1, resource_path_root):
two_packets = _get_dummy_packets(resource_path_root, "login_response", "turn_off_response")
with patch.object(reader_mock, "read", side_effect=two_packets):
response = await connected_api_type1.control_device(Command.OFF)
assert_that(writer_write.call_count).is_equal_to(2)
assert_that(response).is_instance_of(SwitcherBaseResponse)
assert_that(response.unparsed_response).is_equal_to(two_packets[-1])
async def test_set_name_function_with_valid_packets(reader_mock, writer_write, connected_api_type1, resource_path_root):
two_packets = _get_dummy_packets(resource_path_root, "login_response", "set_name_response")
with patch.object(reader_mock, "read", side_effect=two_packets):
response = await connected_api_type1.set_device_name("my boiler")
assert_that(writer_write.call_count).is_equal_to(2)
assert_that(response).is_instance_of(SwitcherBaseResponse)
assert_that(response.unparsed_response).is_equal_to(two_packets[-1])
async def test_set_auto_shutdown_function_with_valid_packets(reader_mock, writer_write, connected_api_type1, resource_path_root):
two_packets = _get_dummy_packets(resource_path_root, "login_response", "set_auto_shutdown_response")
with patch.object(reader_mock, "read", side_effect=two_packets):
response = await connected_api_type1.set_auto_shutdown(timedelta(hours=2, minutes=30))
assert_that(writer_write.call_count).is_equal_to(2)
assert_that(response).is_instance_of(SwitcherBaseResponse)
assert_that(response.unparsed_response).is_equal_to(two_packets[-1])
async def test_get_schedules_function_with_valid_packets(reader_mock, writer_write, connected_api_type1, resource_path_root):
two_packets = _get_dummy_packets(resource_path_root, "login_response", "get_schedules_response")
with patch.object(reader_mock, "read", side_effect=two_packets):
response = await connected_api_type1.get_schedules()
assert_that(writer_write.call_count).is_equal_to(2)
assert_that(response).is_instance_of(SwitcherGetSchedulesResponse)
assert_that(response.unparsed_response).is_equal_to(two_packets[-1])
async def test_delete_schedule_function_with_valid_packets(reader_mock, writer_write, connected_api_type1, resource_path_root):
two_packets = _get_dummy_packets(resource_path_root, "login_response", "delete_schedule_response")
with patch.object(reader_mock, "read", side_effect=two_packets):
response = await connected_api_type1.delete_schedule("0")
assert_that(writer_write.call_count).is_equal_to(2)
assert_that(response).is_instance_of(SwitcherBaseResponse)
assert_that(response.unparsed_response).is_equal_to(two_packets[-1])
async def test_create_schedule_function_with_valid_packets(reader_mock, writer_write, connected_api_type1, resource_path_root):
two_packets = _get_dummy_packets(resource_path_root, "login_response", "create_schedule_response")
with patch.object(reader_mock, "read", side_effect=two_packets):
response = await connected_api_type1.create_schedule("18:00", "19:00")
assert_that(writer_write.call_count).is_equal_to(2)
assert_that(response).is_instance_of(SwitcherBaseResponse)
assert_that(response.unparsed_response).is_equal_to(two_packets[-1])
async def test_stop_shutter_device_function_with_valid_packets(reader_mock, writer_write, connected_api_type2, resource_path_root):
two_packets = _get_dummy_packets(resource_path_root, "login_response", "stop_shutter_response")
with patch.object(reader_mock, "read", side_effect=two_packets):
response = await connected_api_type2.stop_shutter(device_index)
assert_that(writer_write.call_count).is_equal_to(2)
assert_that(response).is_instance_of(SwitcherBaseResponse)
assert_that(response.unparsed_response).is_equal_to(two_packets[-1])
async def test_stop_shutter_token_device_function_with_valid_packets(reader_mock, writer_write, connected_api_token_type2, resource_path_root):
three_packets = _get_dummy_packets(resource_path_root, "login_response", "login2_response", "stop_shutter_response")
with patch.object(reader_mock, "read", side_effect=three_packets):
response = await connected_api_token_type2.stop_shutter(device_index)
assert_that(writer_write.call_count).is_equal_to(3)
assert_that(response).is_instance_of(SwitcherBaseResponse)
assert_that(response.unparsed_response).is_equal_to(three_packets[-1])
async def test_set_shutter_position_device_function_with_valid_packets(reader_mock, writer_write, connected_api_type2, resource_path_root):
two_packets = _get_dummy_packets(resource_path_root, "login_response", "set_shutter_position_response")
with patch.object(reader_mock, "read", side_effect=two_packets):
response = await connected_api_type2.set_position(50, device_index)
assert_that(writer_write.call_count).is_equal_to(2)
assert_that(response).is_instance_of(SwitcherBaseResponse)
assert_that(response.unparsed_response).is_equal_to(two_packets[-1])
async def test_set_shutter_position_token_device_function_with_valid_packets(reader_mock, writer_write, connected_api_token_type2, resource_path_root):
three_packets = _get_dummy_packets(resource_path_root, "login_response", "login2_response", "set_shutter_position_response")
with patch.object(reader_mock, "read", side_effect=three_packets):
response = await connected_api_token_type2.set_position(50, device_index)
assert_that(writer_write.call_count).is_equal_to(3)
assert_that(response).is_instance_of(SwitcherBaseResponse)
assert_that(response.unparsed_response).is_equal_to(three_packets[-1])
async def test_get_light_state_function_with_valid_packets(reader_mock, writer_write, connected_api_token_type2, resource_path_root):
three_packets = _get_dummy_packets(resource_path_root, "login_response", "login2_response", "get_light_state_response")
with patch.object(reader_mock, "read", side_effect=three_packets):
response = await connected_api_token_type2.get_light_state()
assert_that(writer_write.call_count).is_equal_to(3)
assert_that(response).is_instance_of(SwitcherLightStateResponse)
assert_that(response.unparsed_response).is_equal_to(three_packets[-1])
async def test_get_light_state_function_with_a_faulty_device_should_raise_error(reader_mock, writer_write, connected_api_type2, resource_path_root):
login_response_packet = _load_dummy_packet(resource_path_root, "login2_response")
get_state_response_packet = _load_dummy_packet(resource_path_root, "get_light_state_response")
with raises(RuntimeError, match="get light state request was not successful"):
with patch.object(reader_mock, "read", side_effect=[login_response_packet, get_state_response_packet]):
await connected_api_type2.get_light_state()
assert_that(writer_write.call_count).is_equal_to(2)
async def test_get_light_state_function_with_a_faulty_login_response_should_raise_error(reader_mock, writer_write, connected_api_type2):
with raises(RuntimeError, match="login request was not successful"):
with patch.object(reader_mock, "read", return_value=b''):
await connected_api_type2.get_light_state()
writer_write.assert_called_once()
async def test_get_light_state_function_with_a_faulty_get_state_response_should_raise_error(reader_mock, writer_write, connected_api_type2, resource_path_root):
login_response_packet = _load_dummy_packet(resource_path_root, "login_response")
with raises(RuntimeError, match="get light state request was not successful"):
with patch.object(reader_mock, "read", side_effect=[login_response_packet, b'']):
await connected_api_type2.get_light_state()
assert_that(writer_write.call_count).is_equal_to(2)
async def test_set_light_function_with_valid_packets(reader_mock, writer_write, connected_api_token_type2, resource_path_root):
three_packets = _get_dummy_packets(resource_path_root, "login_response", "login2_response", "set_light_response")
with patch.object(reader_mock, "read", side_effect=three_packets):
response = await connected_api_token_type2.set_light(DeviceState.ON, device_index)
assert_that(writer_write.call_count).is_equal_to(3)
assert_that(response).is_instance_of(SwitcherBaseResponse)
assert_that(response.unparsed_response).is_equal_to(three_packets[-1])
async def test_set_light_function_with_valid_packets_second_light(reader_mock, writer_write, connected_api_token_type2, resource_path_root):
three_packets = _get_dummy_packets(resource_path_root, "login_response", "login2_response", "set_light_response")
with patch.object(reader_mock, "read", side_effect=three_packets):
response = await connected_api_token_type2.set_light(DeviceState.ON, device_index2)
assert_that(writer_write.call_count).is_equal_to(3)
assert_that(response).is_instance_of(SwitcherBaseResponse)
assert_that(response.unparsed_response).is_equal_to(three_packets[-1])
async def test_get_shutter_state_function_with_valid_packets(reader_mock, writer_write, connected_api_type2, resource_path_root):
login_response_packet = _load_dummy_packet(resource_path_root, "login2_response")
get_state_response_packet = _load_dummy_packet(resource_path_root, "get_shutter_state_response")
with patch.object(reader_mock, "read", side_effect=[login_response_packet, get_state_response_packet]):
response = await connected_api_type2.get_shutter_state()
assert_that(writer_write.call_count).is_equal_to(2)
assert_that(response).is_instance_of(SwitcherShutterStateResponse)
assert_that(response.unparsed_response).is_equal_to(get_state_response_packet)
async def test_get_shutter_state_function_with_a_faulty_login_response_should_raise_error(reader_mock, writer_write, connected_api_type2):
with raises(RuntimeError, match="login request was not successful"):
with patch.object(reader_mock, "read", return_value=b''):
await connected_api_type2.get_shutter_state()
writer_write.assert_called_once()
async def test_get_shutter_state_function_with_a_faulty_get_state_response_should_raise_error(reader_mock, writer_write, connected_api_type2, resource_path_root):
login_response_packet = _load_dummy_packet(resource_path_root, "login_response")
with raises(RuntimeError, match="get shutter state request was not successful"):
with patch.object(reader_mock, "read", side_effect=[login_response_packet, b'']):
await connected_api_type2.get_shutter_state()
assert_that(writer_write.call_count).is_equal_to(2)
async def test_set_position_function_with_a_faulty_get_state_response_should_raise_error(reader_mock, writer_write, connected_api_type2):
with raises(RuntimeError, match="login request was not successful"):
with patch.object(reader_mock, "read", return_value=b''):
await connected_api_type2.set_position(50)
writer_write.assert_called_once()
async def test_stop_position_function_with_a_faulty_get_state_response_should_raise_error(reader_mock, writer_write, connected_api_type2):
with raises(RuntimeError, match="login request was not successful"):
with patch.object(reader_mock, "read", return_value=b''):
await connected_api_type2.stop_shutter(device_index)
writer_write.assert_called_once()
def _get_dummy_packets(resource_path_root, *packets):
return [_load_dummy_packet(resource_path_root, packet) for packet in packets]
def _load_dummy_packet(path, file_name):
return unhexlify((path / ("dummy_responses/" + file_name + ".txt")).read_text().replace('\n', '').encode())
|