From f093438cdc663f3dd9c3d420171b34480aa6034a Mon Sep 17 00:00:00 2001 From: username081581 Date: Thu, 31 Aug 2023 10:35:49 +0200 Subject: [PATCH] Add tests --- hmip_cli.py | 7 ++-- tests/test_hmip_cli.py | 85 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 87 insertions(+), 5 deletions(-) diff --git a/hmip_cli.py b/hmip_cli.py index f3db23d5..3dcc2705 100644 --- a/hmip_cli.py +++ b/hmip_cli.py @@ -15,7 +15,7 @@ from homematicip.home import Home from homematicip.rule import * -logger = None +logger = logging.getLogger(__name__) def create_logger(level, file_name): @@ -893,7 +893,7 @@ def main(): def _get_target_channel_indices(device: BaseDevice, channels: list = None) -> list: - """Get list with adressed channels""" + """Get list with adressed channel indices. Is the channels list None, than the channel 1 is used.""" target_channels_indices = [] if channels: target_channels_indices = [*channels] @@ -915,7 +915,7 @@ def _get_target_channels(device: Device, channels: list = None): def _execute_action_for_device( device, cli_args, action: CliActions, function_name: str, *args ) -> None: - target_channels = _get_target_channels(device, *cli_args.channels) + target_channels = _get_target_channels(device, cli_args.channels) for fc in target_channels: _execute_cli_action( fc, @@ -923,7 +923,6 @@ def _execute_action_for_device( function_name, *args, ) - command_entered = True def _execute_cli_action( diff --git a/tests/test_hmip_cli.py b/tests/test_hmip_cli.py index 05324b79..5ec0e617 100644 --- a/tests/test_hmip_cli.py +++ b/tests/test_hmip_cli.py @@ -1,7 +1,21 @@ import json +import logging -from hmip_cli import getRssiBarString +from hmip_cli import ( + _channel_supports_action, + getRssiBarString, + _get_target_channel_indices, + _get_target_channels, + _execute_action_for_device, + _execute_cli_action, +) +from homematicip.base.enums import CliActions from homematicip.base.helpers import anonymizeConfig, handle_config +from homematicip.home import Home +from homematicip_demo.helper import ( + fake_home_download_configuration, + no_ssl_verification, +) def test_getRssiBarString(): @@ -51,3 +65,72 @@ def test_anonymizeConfig(): c = '{"id":"test"}' c = anonymizeConfig(c, "original", "REPLACED") assert c == '{"id":"test"}' + + +def test_get_target_channel_indices(fake_home: Home): + d = fake_home.search_device_by_id("3014F71100000000000DRBL4") + + assert _get_target_channel_indices(d, [1]) == [1] + assert _get_target_channel_indices(d, None) == [1] + assert _get_target_channel_indices(d, [1, 2, 3]) == [1, 2, 3] + + +def test_get_target_channels(fake_home: Home): + d = fake_home.search_device_by_id("3014F71100000000000DRBL4") + + result = _get_target_channels(d, None) + assert len(result) == 1 + assert result[0].index == 1 + + result = _get_target_channels(d, [1, 3]) + assert len(result) == 2 + assert result[0].index == 1 + assert result[1].index == 3 + + +def test_execute_action_for_device_shutter_level(fake_home: Home): + class Args: + def __init__(self) -> None: + self.channels: list = [1, 2, 3] + + args = Args() + d = fake_home.search_device_by_id("3014F71100000000000DRBL4") + + with no_ssl_verification(): + _execute_action_for_device( + d, args, CliActions.SET_SHUTTER_LEVEL, "set_shutter_level", 0.5 + ) + fake_home.get_current_state() + d = fake_home.search_device_by_id("3014F71100000000000DRBL4") + assert d.functionalChannels[1].shutterLevel == 0.5 + assert d.functionalChannels[2].shutterLevel == 0.5 + assert d.functionalChannels[3].shutterLevel == 0.5 + + +def test_execute_action_for_device_slats_level(fake_home: Home): + class Args: + def __init__(self) -> None: + self.channels: list = [1, 2, 3] + + args = Args() + d = fake_home.search_device_by_id("3014F71100000000000DRBL4") + + with no_ssl_verification(): + _execute_action_for_device( + d, args, CliActions.SET_SLATS_LEVEL, "set_slats_level", 0.5 + ) + fake_home.get_current_state() + d = fake_home.search_device_by_id("3014F71100000000000DRBL4") + assert d.functionalChannels[1].slatsLevel == 0.5 + assert d.functionalChannels[2].slatsLevel == 0.5 + assert d.functionalChannels[3].slatsLevel == 0.5 + + +def test_channel_supports_action(fake_home: Home): + d = fake_home.search_device_by_id("3014F71100000000000DRBL4") + assert False == _channel_supports_action( + d.functionalChannels[1], CliActions.SET_DIM_LEVEL + ) + assert True == _channel_supports_action( + d.functionalChannels[1], CliActions.SET_SHUTTER_LEVEL + )