diff --git a/homeassistant/components/homematicip_cloud/switch.py b/homeassistant/components/homematicip_cloud/switch.py index 66a40229c7e..ca591adbf5e 100644 --- a/homeassistant/components/homematicip_cloud/switch.py +++ b/homeassistant/components/homematicip_cloud/switch.py @@ -4,13 +4,14 @@ from __future__ import annotations from typing import Any -from homematicip.base.enums import DeviceType +from homematicip.base.enums import DeviceType, FunctionalChannelType from homematicip.device import ( BrandSwitch2, DinRailSwitch, DinRailSwitch4, FullFlushInputSwitch, HeatingSwitch2, + MotionDetectorSwitchOutdoor, MultiIOBox, OpenCollector8Module, PlugableSwitch, @@ -47,18 +48,34 @@ async def async_setup_entry( and getattr(device, "deviceType", None) != DeviceType.BRAND_SWITCH_MEASURING ): entities.append(HomematicipSwitchMeasuring(hap, device)) - elif isinstance(device, WiredSwitch8): + elif isinstance( + device, + ( + WiredSwitch8, + OpenCollector8Module, + BrandSwitch2, + PrintedCircuitBoardSwitch2, + HeatingSwitch2, + MultiIOBox, + MotionDetectorSwitchOutdoor, + DinRailSwitch, + DinRailSwitch4, + ), + ): + channel_indices = [ + ch.index + for ch in device.functionalChannels + if ch.functionalChannelType + in ( + FunctionalChannelType.SWITCH_CHANNEL, + FunctionalChannelType.MULTI_MODE_INPUT_SWITCH_CHANNEL, + ) + ] entities.extend( HomematicipMultiSwitch(hap, device, channel=channel) - for channel in range(1, 9) - ) - elif isinstance(device, DinRailSwitch): - entities.append(HomematicipMultiSwitch(hap, device, channel=1)) - elif isinstance(device, DinRailSwitch4): - entities.extend( - HomematicipMultiSwitch(hap, device, channel=channel) - for channel in range(1, 5) + for channel in channel_indices ) + elif isinstance( device, ( @@ -68,24 +85,6 @@ async def async_setup_entry( ), ): entities.append(HomematicipSwitch(hap, device)) - elif isinstance(device, OpenCollector8Module): - entities.extend( - HomematicipMultiSwitch(hap, device, channel=channel) - for channel in range(1, 9) - ) - elif isinstance( - device, - ( - BrandSwitch2, - PrintedCircuitBoardSwitch2, - HeatingSwitch2, - MultiIOBox, - ), - ): - entities.extend( - HomematicipMultiSwitch(hap, device, channel=channel) - for channel in range(1, 3) - ) async_add_entities(entities) @@ -108,15 +107,15 @@ class HomematicipMultiSwitch(HomematicipGenericEntity, SwitchEntity): @property def is_on(self) -> bool: """Return true if switch is on.""" - return self._device.functionalChannels[self._channel].on + return self.functional_channel.on async def async_turn_on(self, **kwargs: Any) -> None: """Turn the switch on.""" - await self._device.turn_on_async(self._channel) + await self.functional_channel.async_turn_on() async def async_turn_off(self, **kwargs: Any) -> None: """Turn the switch off.""" - await self._device.turn_off_async(self._channel) + await self.functional_channel.async_turn_off() class HomematicipSwitch(HomematicipMultiSwitch, SwitchEntity): diff --git a/tests/components/homematicip_cloud/test_switch.py b/tests/components/homematicip_cloud/test_switch.py index 1a728bfecd4..50d527775bd 100644 --- a/tests/components/homematicip_cloud/test_switch.py +++ b/tests/components/homematicip_cloud/test_switch.py @@ -25,14 +25,14 @@ async def test_hmip_switch( ) assert ha_state.state == STATE_ON - service_call_counter = len(hmip_device.mock_calls) + service_call_counter = len(hmip_device.functionalChannels[1].mock_calls) await hass.services.async_call( "switch", "turn_off", {"entity_id": entity_id}, blocking=True ) - assert len(hmip_device.mock_calls) == service_call_counter + 1 - assert hmip_device.mock_calls[-1][0] == "turn_off_async" - assert hmip_device.mock_calls[-1][1] == (1,) + assert len(hmip_device.functionalChannels[1].mock_calls) == service_call_counter + 1 + assert hmip_device.functionalChannels[1].mock_calls[-1][0] == "async_turn_off" + assert hmip_device.functionalChannels[1].mock_calls[-1][1] == () await async_manipulate_test_data(hass, hmip_device, "on", False) ha_state = hass.states.get(entity_id) assert ha_state.state == STATE_OFF @@ -40,9 +40,9 @@ async def test_hmip_switch( await hass.services.async_call( "switch", "turn_on", {"entity_id": entity_id}, blocking=True ) - assert len(hmip_device.mock_calls) == service_call_counter + 3 - assert hmip_device.mock_calls[-1][0] == "turn_on_async" - assert hmip_device.mock_calls[-1][1] == (1,) + assert len(hmip_device.functionalChannels[1].mock_calls) == service_call_counter + 2 + assert hmip_device.functionalChannels[1].mock_calls[-1][0] == "async_turn_on" + assert hmip_device.functionalChannels[1].mock_calls[-1][1] == () await async_manipulate_test_data(hass, hmip_device, "on", True) ha_state = hass.states.get(entity_id) assert ha_state.state == STATE_ON @@ -64,14 +64,14 @@ async def test_hmip_switch_input( ) assert ha_state.state == STATE_ON - service_call_counter = len(hmip_device.mock_calls) + service_call_counter = len(hmip_device.functionalChannels[1].mock_calls) await hass.services.async_call( "switch", "turn_off", {"entity_id": entity_id}, blocking=True ) - assert len(hmip_device.mock_calls) == service_call_counter + 1 - assert hmip_device.mock_calls[-1][0] == "turn_off_async" - assert hmip_device.mock_calls[-1][1] == (1,) + assert len(hmip_device.functionalChannels[1].mock_calls) == service_call_counter + 1 + assert hmip_device.functionalChannels[1].mock_calls[-1][0] == "async_turn_off" + assert hmip_device.functionalChannels[1].mock_calls[-1][1] == () await async_manipulate_test_data(hass, hmip_device, "on", False) ha_state = hass.states.get(entity_id) assert ha_state.state == STATE_OFF @@ -79,9 +79,9 @@ async def test_hmip_switch_input( await hass.services.async_call( "switch", "turn_on", {"entity_id": entity_id}, blocking=True ) - assert len(hmip_device.mock_calls) == service_call_counter + 3 - assert hmip_device.mock_calls[-1][0] == "turn_on_async" - assert hmip_device.mock_calls[-1][1] == (1,) + assert len(hmip_device.functionalChannels[1].mock_calls) == service_call_counter + 2 + assert hmip_device.functionalChannels[1].mock_calls[-1][0] == "async_turn_on" + assert hmip_device.functionalChannels[1].mock_calls[-1][1] == () await async_manipulate_test_data(hass, hmip_device, "on", True) ha_state = hass.states.get(entity_id) assert ha_state.state == STATE_ON @@ -103,14 +103,14 @@ async def test_hmip_switch_measuring( ) assert ha_state.state == STATE_ON - service_call_counter = len(hmip_device.mock_calls) + service_call_counter = len(hmip_device.functionalChannels[1].mock_calls) await hass.services.async_call( "switch", "turn_off", {"entity_id": entity_id}, blocking=True ) - assert len(hmip_device.mock_calls) == service_call_counter + 1 - assert hmip_device.mock_calls[-1][0] == "turn_off_async" - assert hmip_device.mock_calls[-1][1] == (1,) + assert len(hmip_device.functionalChannels[1].mock_calls) == service_call_counter + 1 + assert hmip_device.functionalChannels[1].mock_calls[-1][0] == "async_turn_off" + assert hmip_device.functionalChannels[1].mock_calls[-1][1] == () await async_manipulate_test_data(hass, hmip_device, "on", False) ha_state = hass.states.get(entity_id) assert ha_state.state == STATE_OFF @@ -118,9 +118,9 @@ async def test_hmip_switch_measuring( await hass.services.async_call( "switch", "turn_on", {"entity_id": entity_id}, blocking=True ) - assert len(hmip_device.mock_calls) == service_call_counter + 3 - assert hmip_device.mock_calls[-1][0] == "turn_on_async" - assert hmip_device.mock_calls[-1][1] == (1,) + assert len(hmip_device.functionalChannels[1].mock_calls) == service_call_counter + 2 + assert hmip_device.functionalChannels[1].mock_calls[-1][0] == "async_turn_on" + assert hmip_device.functionalChannels[1].mock_calls[-1][1] == () await async_manipulate_test_data(hass, hmip_device, "on", True) await async_manipulate_test_data(hass, hmip_device, "currentPowerConsumption", 50) ha_state = hass.states.get(entity_id) @@ -191,14 +191,14 @@ async def test_hmip_multi_switch( ) assert ha_state.state == STATE_OFF - service_call_counter = len(hmip_device.mock_calls) + service_call_counter = len(hmip_device.functionalChannels[1].mock_calls) await hass.services.async_call( "switch", "turn_on", {"entity_id": entity_id}, blocking=True ) - assert len(hmip_device.mock_calls) == service_call_counter + 1 - assert hmip_device.mock_calls[-1][0] == "turn_on_async" - assert hmip_device.mock_calls[-1][1] == (1,) + assert len(hmip_device.functionalChannels[1].mock_calls) == service_call_counter + 1 + assert hmip_device.functionalChannels[1].mock_calls[-1][0] == "async_turn_on" + assert hmip_device.functionalChannels[1].mock_calls[-1][1] == () await async_manipulate_test_data(hass, hmip_device, "on", True) ha_state = hass.states.get(entity_id) assert ha_state.state == STATE_ON @@ -206,9 +206,9 @@ async def test_hmip_multi_switch( await hass.services.async_call( "switch", "turn_off", {"entity_id": entity_id}, blocking=True ) - assert len(hmip_device.mock_calls) == service_call_counter + 3 - assert hmip_device.mock_calls[-1][0] == "turn_off_async" - assert hmip_device.mock_calls[-1][1] == (1,) + assert len(hmip_device.functionalChannels[1].mock_calls) == service_call_counter + 2 + assert hmip_device.functionalChannels[1].mock_calls[-1][0] == "async_turn_off" + assert hmip_device.functionalChannels[1].mock_calls[-1][1] == () await async_manipulate_test_data(hass, hmip_device, "on", False) ha_state = hass.states.get(entity_id) assert ha_state.state == STATE_OFF @@ -242,14 +242,14 @@ async def test_hmip_wired_multi_switch( ) assert ha_state.state == STATE_ON - service_call_counter = len(hmip_device.mock_calls) + service_call_counter = len(hmip_device.functionalChannels[1].mock_calls) await hass.services.async_call( "switch", "turn_off", {"entity_id": entity_id}, blocking=True ) - assert len(hmip_device.mock_calls) == service_call_counter + 1 - assert hmip_device.mock_calls[-1][0] == "turn_off_async" - assert hmip_device.mock_calls[-1][1] == (1,) + assert len(hmip_device.functionalChannels[1].mock_calls) == service_call_counter + 1 + assert hmip_device.functionalChannels[1].mock_calls[-1][0] == "async_turn_off" + assert hmip_device.functionalChannels[1].mock_calls[-1][1] == () await async_manipulate_test_data(hass, hmip_device, "on", False) ha_state = hass.states.get(entity_id) assert ha_state.state == STATE_OFF @@ -257,9 +257,9 @@ async def test_hmip_wired_multi_switch( await hass.services.async_call( "switch", "turn_on", {"entity_id": entity_id}, blocking=True ) - assert len(hmip_device.mock_calls) == service_call_counter + 3 - assert hmip_device.mock_calls[-1][0] == "turn_on_async" - assert hmip_device.mock_calls[-1][1] == (1,) + assert len(hmip_device.functionalChannels[1].mock_calls) == service_call_counter + 2 + assert hmip_device.functionalChannels[1].mock_calls[-1][0] == "async_turn_on" + assert hmip_device.functionalChannels[1].mock_calls[-1][1] == () await async_manipulate_test_data(hass, hmip_device, "on", True) ha_state = hass.states.get(entity_id) assert ha_state.state == STATE_ON