Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hahn-th committed Aug 31, 2023
1 parent 38abe42 commit f093438
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 5 deletions.
7 changes: 3 additions & 4 deletions hmip_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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]
Expand All @@ -915,15 +915,14 @@ 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,
action,
function_name,
*args,
)
command_entered = True


def _execute_cli_action(
Expand Down
85 changes: 84 additions & 1 deletion tests/test_hmip_cli.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down Expand Up @@ -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
)

0 comments on commit f093438

Please sign in to comment.