From d5aaae7d9f8d0a7b45c2e04e7d822c58f3b32871 Mon Sep 17 00:00:00 2001 From: Thomas Hahn Date: Wed, 5 Jun 2024 18:55:46 +0200 Subject: [PATCH] Add set_switch_state action for group --- src/homematicip/action/group_actions.py | 13 ++++++++- tests/actions/test_group_actions.py | 36 +++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/homematicip/action/group_actions.py b/src/homematicip/action/group_actions.py index e2d06a0..bbeb626 100644 --- a/src/homematicip/action/group_actions.py +++ b/src/homematicip/action/group_actions.py @@ -4,6 +4,7 @@ from homematicip.connection.rest_connection import RestConnection from homematicip.model.enums import ProfileMode, ClimateControlMode from homematicip.model.hmip_base import HmipBaseModel +from homematicip.model.model_components import Group from homematicip.runner import Runner LOGGER = logging.getLogger(__name__) @@ -68,6 +69,17 @@ async def action_set_control_mode(rest_connection: RestConnection, group: HmipBa return await rest_connection.async_post("group/heating/setControlMode", data) +@Action.allowed_types("EXTENDED_LINKED_SWITCHING", "SWITCHING") +async def group_action_set_switch_state(rest_connection: RestConnection, group: Group, on): + """Set switching state of group + + :param rest_connection: RestConnection + :param group: The group to set the state + :param on: True switches on, False switches off + """ + data = {"groupId": group.id, "on": on} + return await rest_connection.async_post("group/switching/setState", data) + # # @Action.allowed_types("SWITCHING_PROFILE") # async def set_profile_mode(rest_connection: RestConnection, group: HmipBaseModel, profile_mode: ProfileMode): @@ -83,4 +95,3 @@ async def action_set_control_mode(rest_connection: RestConnection, group: HmipBa # } # return await rest_connection.async_post( # "group/switching/profile/setProfileMode", data) - diff --git a/tests/actions/test_group_actions.py b/tests/actions/test_group_actions.py index 6664784..9ea5f9c 100644 --- a/tests/actions/test_group_actions.py +++ b/tests/actions/test_group_actions.py @@ -1,5 +1,37 @@ -# import pytest -# from homematicip.base.enums import ClimateControlMode +import pytest + +from homematicip.action.group_actions import group_action_set_switch_state +from homematicip.connection.rest_connection import RestConnection, RestResult +from homematicip.model.model_components import Group +from homematicip.runner import Runner + + +@pytest.fixture +def runner(mocker, filled_model): + conn = mocker.Mock(spec=RestConnection) + conn.async_post.return_value = RestResult(status=200) + runner = Runner(_rest_connection=conn, model=filled_model) + return runner + + +@pytest.fixture +def sample_group() -> Group: + return Group(id="00000000-0000-0000-0000-000000000001", type="", channels=[]) + + +@pytest.mark.asyncio +async def test_wrong_group_type(runner, sample_group): + sample_group.type = "ASDF" + with pytest.raises(ValueError): + await group_action_set_switch_state(runner.rest_connection, sample_group, True) + +@pytest.mark.asyncio +async def test_group_set_switch_state(runner, sample_group): + sample_group.type = "SWITCHING" + await group_action_set_switch_state(runner.rest_connection, sample_group, True) + runner.rest_connection.async_post.assert_called_once_with("group/switching/setState", + {"groupId": sample_group.id, "on": True}) + # # from homematicip.action.group_actions import SetPointTemperatureGroupAction, SetBoostGroupAction, \ # SetBoostDurationGroupAction, SetActiveProfileGroupAction, SetControlModeGroupAction, \