Improve code quality in async_setup_entry of switches in homematicip_cloud (#146816)
improve setup of switchespull/146830/head
parent
6204fd5363
commit
2ac8901a0d
|
@ -4,13 +4,14 @@ from __future__ import annotations
|
||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from homematicip.base.enums import DeviceType
|
from homematicip.base.enums import DeviceType, FunctionalChannelType
|
||||||
from homematicip.device import (
|
from homematicip.device import (
|
||||||
BrandSwitch2,
|
BrandSwitch2,
|
||||||
DinRailSwitch,
|
DinRailSwitch,
|
||||||
DinRailSwitch4,
|
DinRailSwitch4,
|
||||||
FullFlushInputSwitch,
|
FullFlushInputSwitch,
|
||||||
HeatingSwitch2,
|
HeatingSwitch2,
|
||||||
|
MotionDetectorSwitchOutdoor,
|
||||||
MultiIOBox,
|
MultiIOBox,
|
||||||
OpenCollector8Module,
|
OpenCollector8Module,
|
||||||
PlugableSwitch,
|
PlugableSwitch,
|
||||||
|
@ -47,18 +48,34 @@ async def async_setup_entry(
|
||||||
and getattr(device, "deviceType", None) != DeviceType.BRAND_SWITCH_MEASURING
|
and getattr(device, "deviceType", None) != DeviceType.BRAND_SWITCH_MEASURING
|
||||||
):
|
):
|
||||||
entities.append(HomematicipSwitchMeasuring(hap, device))
|
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(
|
entities.extend(
|
||||||
HomematicipMultiSwitch(hap, device, channel=channel)
|
HomematicipMultiSwitch(hap, device, channel=channel)
|
||||||
for channel in range(1, 9)
|
for channel in channel_indices
|
||||||
)
|
|
||||||
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)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
elif isinstance(
|
elif isinstance(
|
||||||
device,
|
device,
|
||||||
(
|
(
|
||||||
|
@ -68,24 +85,6 @@ async def async_setup_entry(
|
||||||
),
|
),
|
||||||
):
|
):
|
||||||
entities.append(HomematicipSwitch(hap, device))
|
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)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
@ -108,15 +107,15 @@ class HomematicipMultiSwitch(HomematicipGenericEntity, SwitchEntity):
|
||||||
@property
|
@property
|
||||||
def is_on(self) -> bool:
|
def is_on(self) -> bool:
|
||||||
"""Return true if switch is on."""
|
"""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:
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn the switch on."""
|
"""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:
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||||
"""Turn the switch off."""
|
"""Turn the switch off."""
|
||||||
await self._device.turn_off_async(self._channel)
|
await self.functional_channel.async_turn_off()
|
||||||
|
|
||||||
|
|
||||||
class HomematicipSwitch(HomematicipMultiSwitch, SwitchEntity):
|
class HomematicipSwitch(HomematicipMultiSwitch, SwitchEntity):
|
||||||
|
|
|
@ -25,14 +25,14 @@ async def test_hmip_switch(
|
||||||
)
|
)
|
||||||
|
|
||||||
assert ha_state.state == STATE_ON
|
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(
|
await hass.services.async_call(
|
||||||
"switch", "turn_off", {"entity_id": entity_id}, blocking=True
|
"switch", "turn_off", {"entity_id": entity_id}, blocking=True
|
||||||
)
|
)
|
||||||
assert len(hmip_device.mock_calls) == service_call_counter + 1
|
assert len(hmip_device.functionalChannels[1].mock_calls) == service_call_counter + 1
|
||||||
assert hmip_device.mock_calls[-1][0] == "turn_off_async"
|
assert hmip_device.functionalChannels[1].mock_calls[-1][0] == "async_turn_off"
|
||||||
assert hmip_device.mock_calls[-1][1] == (1,)
|
assert hmip_device.functionalChannels[1].mock_calls[-1][1] == ()
|
||||||
await async_manipulate_test_data(hass, hmip_device, "on", False)
|
await async_manipulate_test_data(hass, hmip_device, "on", False)
|
||||||
ha_state = hass.states.get(entity_id)
|
ha_state = hass.states.get(entity_id)
|
||||||
assert ha_state.state == STATE_OFF
|
assert ha_state.state == STATE_OFF
|
||||||
|
@ -40,9 +40,9 @@ async def test_hmip_switch(
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
"switch", "turn_on", {"entity_id": entity_id}, blocking=True
|
"switch", "turn_on", {"entity_id": entity_id}, blocking=True
|
||||||
)
|
)
|
||||||
assert len(hmip_device.mock_calls) == service_call_counter + 3
|
assert len(hmip_device.functionalChannels[1].mock_calls) == service_call_counter + 2
|
||||||
assert hmip_device.mock_calls[-1][0] == "turn_on_async"
|
assert hmip_device.functionalChannels[1].mock_calls[-1][0] == "async_turn_on"
|
||||||
assert hmip_device.mock_calls[-1][1] == (1,)
|
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, "on", True)
|
||||||
ha_state = hass.states.get(entity_id)
|
ha_state = hass.states.get(entity_id)
|
||||||
assert ha_state.state == STATE_ON
|
assert ha_state.state == STATE_ON
|
||||||
|
@ -64,14 +64,14 @@ async def test_hmip_switch_input(
|
||||||
)
|
)
|
||||||
|
|
||||||
assert ha_state.state == STATE_ON
|
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(
|
await hass.services.async_call(
|
||||||
"switch", "turn_off", {"entity_id": entity_id}, blocking=True
|
"switch", "turn_off", {"entity_id": entity_id}, blocking=True
|
||||||
)
|
)
|
||||||
assert len(hmip_device.mock_calls) == service_call_counter + 1
|
assert len(hmip_device.functionalChannels[1].mock_calls) == service_call_counter + 1
|
||||||
assert hmip_device.mock_calls[-1][0] == "turn_off_async"
|
assert hmip_device.functionalChannels[1].mock_calls[-1][0] == "async_turn_off"
|
||||||
assert hmip_device.mock_calls[-1][1] == (1,)
|
assert hmip_device.functionalChannels[1].mock_calls[-1][1] == ()
|
||||||
await async_manipulate_test_data(hass, hmip_device, "on", False)
|
await async_manipulate_test_data(hass, hmip_device, "on", False)
|
||||||
ha_state = hass.states.get(entity_id)
|
ha_state = hass.states.get(entity_id)
|
||||||
assert ha_state.state == STATE_OFF
|
assert ha_state.state == STATE_OFF
|
||||||
|
@ -79,9 +79,9 @@ async def test_hmip_switch_input(
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
"switch", "turn_on", {"entity_id": entity_id}, blocking=True
|
"switch", "turn_on", {"entity_id": entity_id}, blocking=True
|
||||||
)
|
)
|
||||||
assert len(hmip_device.mock_calls) == service_call_counter + 3
|
assert len(hmip_device.functionalChannels[1].mock_calls) == service_call_counter + 2
|
||||||
assert hmip_device.mock_calls[-1][0] == "turn_on_async"
|
assert hmip_device.functionalChannels[1].mock_calls[-1][0] == "async_turn_on"
|
||||||
assert hmip_device.mock_calls[-1][1] == (1,)
|
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, "on", True)
|
||||||
ha_state = hass.states.get(entity_id)
|
ha_state = hass.states.get(entity_id)
|
||||||
assert ha_state.state == STATE_ON
|
assert ha_state.state == STATE_ON
|
||||||
|
@ -103,14 +103,14 @@ async def test_hmip_switch_measuring(
|
||||||
)
|
)
|
||||||
|
|
||||||
assert ha_state.state == STATE_ON
|
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(
|
await hass.services.async_call(
|
||||||
"switch", "turn_off", {"entity_id": entity_id}, blocking=True
|
"switch", "turn_off", {"entity_id": entity_id}, blocking=True
|
||||||
)
|
)
|
||||||
assert len(hmip_device.mock_calls) == service_call_counter + 1
|
assert len(hmip_device.functionalChannels[1].mock_calls) == service_call_counter + 1
|
||||||
assert hmip_device.mock_calls[-1][0] == "turn_off_async"
|
assert hmip_device.functionalChannels[1].mock_calls[-1][0] == "async_turn_off"
|
||||||
assert hmip_device.mock_calls[-1][1] == (1,)
|
assert hmip_device.functionalChannels[1].mock_calls[-1][1] == ()
|
||||||
await async_manipulate_test_data(hass, hmip_device, "on", False)
|
await async_manipulate_test_data(hass, hmip_device, "on", False)
|
||||||
ha_state = hass.states.get(entity_id)
|
ha_state = hass.states.get(entity_id)
|
||||||
assert ha_state.state == STATE_OFF
|
assert ha_state.state == STATE_OFF
|
||||||
|
@ -118,9 +118,9 @@ async def test_hmip_switch_measuring(
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
"switch", "turn_on", {"entity_id": entity_id}, blocking=True
|
"switch", "turn_on", {"entity_id": entity_id}, blocking=True
|
||||||
)
|
)
|
||||||
assert len(hmip_device.mock_calls) == service_call_counter + 3
|
assert len(hmip_device.functionalChannels[1].mock_calls) == service_call_counter + 2
|
||||||
assert hmip_device.mock_calls[-1][0] == "turn_on_async"
|
assert hmip_device.functionalChannels[1].mock_calls[-1][0] == "async_turn_on"
|
||||||
assert hmip_device.mock_calls[-1][1] == (1,)
|
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, "on", True)
|
||||||
await async_manipulate_test_data(hass, hmip_device, "currentPowerConsumption", 50)
|
await async_manipulate_test_data(hass, hmip_device, "currentPowerConsumption", 50)
|
||||||
ha_state = hass.states.get(entity_id)
|
ha_state = hass.states.get(entity_id)
|
||||||
|
@ -191,14 +191,14 @@ async def test_hmip_multi_switch(
|
||||||
)
|
)
|
||||||
|
|
||||||
assert ha_state.state == STATE_OFF
|
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(
|
await hass.services.async_call(
|
||||||
"switch", "turn_on", {"entity_id": entity_id}, blocking=True
|
"switch", "turn_on", {"entity_id": entity_id}, blocking=True
|
||||||
)
|
)
|
||||||
assert len(hmip_device.mock_calls) == service_call_counter + 1
|
assert len(hmip_device.functionalChannels[1].mock_calls) == service_call_counter + 1
|
||||||
assert hmip_device.mock_calls[-1][0] == "turn_on_async"
|
assert hmip_device.functionalChannels[1].mock_calls[-1][0] == "async_turn_on"
|
||||||
assert hmip_device.mock_calls[-1][1] == (1,)
|
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, "on", True)
|
||||||
ha_state = hass.states.get(entity_id)
|
ha_state = hass.states.get(entity_id)
|
||||||
assert ha_state.state == STATE_ON
|
assert ha_state.state == STATE_ON
|
||||||
|
@ -206,9 +206,9 @@ async def test_hmip_multi_switch(
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
"switch", "turn_off", {"entity_id": entity_id}, blocking=True
|
"switch", "turn_off", {"entity_id": entity_id}, blocking=True
|
||||||
)
|
)
|
||||||
assert len(hmip_device.mock_calls) == service_call_counter + 3
|
assert len(hmip_device.functionalChannels[1].mock_calls) == service_call_counter + 2
|
||||||
assert hmip_device.mock_calls[-1][0] == "turn_off_async"
|
assert hmip_device.functionalChannels[1].mock_calls[-1][0] == "async_turn_off"
|
||||||
assert hmip_device.mock_calls[-1][1] == (1,)
|
assert hmip_device.functionalChannels[1].mock_calls[-1][1] == ()
|
||||||
await async_manipulate_test_data(hass, hmip_device, "on", False)
|
await async_manipulate_test_data(hass, hmip_device, "on", False)
|
||||||
ha_state = hass.states.get(entity_id)
|
ha_state = hass.states.get(entity_id)
|
||||||
assert ha_state.state == STATE_OFF
|
assert ha_state.state == STATE_OFF
|
||||||
|
@ -242,14 +242,14 @@ async def test_hmip_wired_multi_switch(
|
||||||
)
|
)
|
||||||
|
|
||||||
assert ha_state.state == STATE_ON
|
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(
|
await hass.services.async_call(
|
||||||
"switch", "turn_off", {"entity_id": entity_id}, blocking=True
|
"switch", "turn_off", {"entity_id": entity_id}, blocking=True
|
||||||
)
|
)
|
||||||
assert len(hmip_device.mock_calls) == service_call_counter + 1
|
assert len(hmip_device.functionalChannels[1].mock_calls) == service_call_counter + 1
|
||||||
assert hmip_device.mock_calls[-1][0] == "turn_off_async"
|
assert hmip_device.functionalChannels[1].mock_calls[-1][0] == "async_turn_off"
|
||||||
assert hmip_device.mock_calls[-1][1] == (1,)
|
assert hmip_device.functionalChannels[1].mock_calls[-1][1] == ()
|
||||||
await async_manipulate_test_data(hass, hmip_device, "on", False)
|
await async_manipulate_test_data(hass, hmip_device, "on", False)
|
||||||
ha_state = hass.states.get(entity_id)
|
ha_state = hass.states.get(entity_id)
|
||||||
assert ha_state.state == STATE_OFF
|
assert ha_state.state == STATE_OFF
|
||||||
|
@ -257,9 +257,9 @@ async def test_hmip_wired_multi_switch(
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
"switch", "turn_on", {"entity_id": entity_id}, blocking=True
|
"switch", "turn_on", {"entity_id": entity_id}, blocking=True
|
||||||
)
|
)
|
||||||
assert len(hmip_device.mock_calls) == service_call_counter + 3
|
assert len(hmip_device.functionalChannels[1].mock_calls) == service_call_counter + 2
|
||||||
assert hmip_device.mock_calls[-1][0] == "turn_on_async"
|
assert hmip_device.functionalChannels[1].mock_calls[-1][0] == "async_turn_on"
|
||||||
assert hmip_device.mock_calls[-1][1] == (1,)
|
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, "on", True)
|
||||||
ha_state = hass.states.get(entity_id)
|
ha_state = hass.states.get(entity_id)
|
||||||
assert ha_state.state == STATE_ON
|
assert ha_state.state == STATE_ON
|
||||||
|
|
Loading…
Reference in New Issue