2019-02-14 15:01:46 +00:00
|
|
|
"""Support for HomematicIP Cloud switches."""
|
2021-03-18 08:25:40 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
2018-05-08 07:57:51 +00:00
|
|
|
|
2019-04-23 23:47:31 +00:00
|
|
|
from homematicip.aio.device import (
|
2023-06-28 07:33:48 +00:00
|
|
|
AsyncBrandSwitch2,
|
2019-07-31 19:25:30 +00:00
|
|
|
AsyncBrandSwitchMeasuring,
|
2021-01-25 12:19:11 +00:00
|
|
|
AsyncDinRailSwitch,
|
2020-12-07 23:16:22 +00:00
|
|
|
AsyncDinRailSwitch4,
|
2020-07-14 20:43:21 +00:00
|
|
|
AsyncFullFlushInputSwitch,
|
2019-07-31 19:25:30 +00:00
|
|
|
AsyncFullFlushSwitchMeasuring,
|
2020-01-06 13:35:41 +00:00
|
|
|
AsyncHeatingSwitch2,
|
2019-07-31 19:25:30 +00:00
|
|
|
AsyncMultiIOBox,
|
|
|
|
AsyncOpenCollector8Module,
|
|
|
|
AsyncPlugableSwitch,
|
|
|
|
AsyncPlugableSwitchMeasuring,
|
|
|
|
AsyncPrintedCircuitBoardSwitch2,
|
|
|
|
AsyncPrintedCircuitBoardSwitchBattery,
|
2020-10-24 02:14:54 +00:00
|
|
|
AsyncWiredSwitch8,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-01-17 17:37:32 +00:00
|
|
|
from homematicip.aio.group import AsyncExtendedLinkedSwitchingGroup, AsyncSwitchingGroup
|
2019-04-23 23:47:31 +00:00
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2019-04-25 22:13:07 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-04-23 07:49:02 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-01-07 15:26:57 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2018-05-08 07:57:51 +00:00
|
|
|
|
2020-08-25 23:55:55 +00:00
|
|
|
from . import DOMAIN as HMIPC_DOMAIN, HomematicipGenericEntity
|
2020-10-25 16:03:41 +00:00
|
|
|
from .generic_entity import ATTR_GROUP_MEMBER_UNREACHABLE
|
2019-10-11 14:36:46 +00:00
|
|
|
from .hap import HomematicipHAP
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2018-05-08 07:57:51 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_entry(
|
2022-01-07 15:26:57 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
2019-07-31 19:25:30 +00:00
|
|
|
) -> None:
|
2018-07-06 21:05:34 +00:00
|
|
|
"""Set up the HomematicIP switch from a config entry."""
|
2020-01-26 13:54:33 +00:00
|
|
|
hap = hass.data[HMIPC_DOMAIN][config_entry.unique_id]
|
2021-08-21 18:19:56 +00:00
|
|
|
entities: list[HomematicipGenericEntity] = []
|
2019-10-11 14:36:46 +00:00
|
|
|
for device in hap.home.devices:
|
2019-02-26 22:48:19 +00:00
|
|
|
if isinstance(device, AsyncBrandSwitchMeasuring):
|
2018-05-08 07:57:51 +00:00
|
|
|
# BrandSwitchMeasuring inherits PlugableSwitchMeasuring
|
2020-08-25 23:55:55 +00:00
|
|
|
# This entity is implemented in the light platform and will
|
2018-05-08 07:57:51 +00:00
|
|
|
# not be added in the switch platform
|
|
|
|
pass
|
2019-07-31 19:25:30 +00:00
|
|
|
elif isinstance(
|
|
|
|
device, (AsyncPlugableSwitchMeasuring, AsyncFullFlushSwitchMeasuring)
|
|
|
|
):
|
2019-11-25 13:17:14 +00:00
|
|
|
entities.append(HomematicipSwitchMeasuring(hap, device))
|
2020-10-24 02:14:54 +00:00
|
|
|
elif isinstance(device, AsyncWiredSwitch8):
|
|
|
|
for channel in range(1, 9):
|
2020-10-25 16:03:41 +00:00
|
|
|
entities.append(HomematicipMultiSwitch(hap, device, channel=channel))
|
2021-01-25 12:19:11 +00:00
|
|
|
elif isinstance(device, AsyncDinRailSwitch):
|
|
|
|
entities.append(HomematicipMultiSwitch(hap, device, channel=1))
|
2020-12-07 23:16:22 +00:00
|
|
|
elif isinstance(device, AsyncDinRailSwitch4):
|
|
|
|
for channel in range(1, 5):
|
|
|
|
entities.append(HomematicipMultiSwitch(hap, device, channel=channel))
|
2019-07-31 19:25:30 +00:00
|
|
|
elif isinstance(
|
2020-07-14 20:43:21 +00:00
|
|
|
device,
|
|
|
|
(
|
|
|
|
AsyncPlugableSwitch,
|
|
|
|
AsyncPrintedCircuitBoardSwitchBattery,
|
|
|
|
AsyncFullFlushInputSwitch,
|
|
|
|
),
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2019-11-25 13:17:14 +00:00
|
|
|
entities.append(HomematicipSwitch(hap, device))
|
2019-02-26 22:48:19 +00:00
|
|
|
elif isinstance(device, AsyncOpenCollector8Module):
|
|
|
|
for channel in range(1, 9):
|
2020-10-03 14:35:04 +00:00
|
|
|
entities.append(HomematicipMultiSwitch(hap, device, channel=channel))
|
2020-01-06 13:35:41 +00:00
|
|
|
elif isinstance(device, AsyncHeatingSwitch2):
|
|
|
|
for channel in range(1, 3):
|
2020-10-03 14:35:04 +00:00
|
|
|
entities.append(HomematicipMultiSwitch(hap, device, channel=channel))
|
2019-04-11 08:49:02 +00:00
|
|
|
elif isinstance(device, AsyncMultiIOBox):
|
|
|
|
for channel in range(1, 3):
|
2020-10-03 14:35:04 +00:00
|
|
|
entities.append(HomematicipMultiSwitch(hap, device, channel=channel))
|
2019-07-17 19:29:25 +00:00
|
|
|
elif isinstance(device, AsyncPrintedCircuitBoardSwitch2):
|
|
|
|
for channel in range(1, 3):
|
2020-10-03 14:35:04 +00:00
|
|
|
entities.append(HomematicipMultiSwitch(hap, device, channel=channel))
|
2023-06-28 07:33:48 +00:00
|
|
|
elif isinstance(device, AsyncBrandSwitch2):
|
|
|
|
for channel in range(1, 3):
|
|
|
|
entities.append(HomematicipMultiSwitch(hap, device, channel=channel))
|
2018-05-08 07:57:51 +00:00
|
|
|
|
2019-10-11 14:36:46 +00:00
|
|
|
for group in hap.home.groups:
|
2020-01-17 17:37:32 +00:00
|
|
|
if isinstance(group, (AsyncExtendedLinkedSwitchingGroup, AsyncSwitchingGroup)):
|
2019-11-25 13:17:14 +00:00
|
|
|
entities.append(HomematicipGroupSwitch(hap, group))
|
2019-02-17 05:24:13 +00:00
|
|
|
|
2022-10-17 19:11:58 +00:00
|
|
|
async_add_entities(entities)
|
2018-05-08 07:57:51 +00:00
|
|
|
|
|
|
|
|
2020-10-24 02:14:54 +00:00
|
|
|
class HomematicipMultiSwitch(HomematicipGenericEntity, SwitchEntity):
|
|
|
|
"""Representation of the HomematicIP multi switch."""
|
|
|
|
|
2020-12-07 23:16:22 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
hap: HomematicipHAP,
|
|
|
|
device,
|
|
|
|
channel=1,
|
|
|
|
is_multi_channel=True,
|
|
|
|
) -> None:
|
2020-10-24 02:14:54 +00:00
|
|
|
"""Initialize the multi switch device."""
|
2020-12-07 23:16:22 +00:00
|
|
|
super().__init__(
|
|
|
|
hap, device, channel=channel, is_multi_channel=is_multi_channel
|
|
|
|
)
|
2020-10-24 02:14:54 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return true if switch is on."""
|
|
|
|
return self._device.functionalChannels[self._channel].on
|
|
|
|
|
2022-08-30 18:55:01 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2020-10-24 02:14:54 +00:00
|
|
|
"""Turn the switch on."""
|
|
|
|
await self._device.turn_on(self._channel)
|
|
|
|
|
2022-08-30 18:55:01 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2020-10-24 02:14:54 +00:00
|
|
|
"""Turn the switch off."""
|
|
|
|
await self._device.turn_off(self._channel)
|
|
|
|
|
|
|
|
|
2020-12-07 23:16:22 +00:00
|
|
|
class HomematicipSwitch(HomematicipMultiSwitch, SwitchEntity):
|
2020-08-25 23:55:55 +00:00
|
|
|
"""Representation of the HomematicIP switch."""
|
2018-05-08 07:57:51 +00:00
|
|
|
|
2019-10-11 14:36:46 +00:00
|
|
|
def __init__(self, hap: HomematicipHAP, device) -> None:
|
2018-05-08 07:57:51 +00:00
|
|
|
"""Initialize the switch device."""
|
2020-12-07 23:16:22 +00:00
|
|
|
super().__init__(hap, device, is_multi_channel=False)
|
2018-05-08 07:57:51 +00:00
|
|
|
|
|
|
|
|
2020-08-25 23:55:55 +00:00
|
|
|
class HomematicipGroupSwitch(HomematicipGenericEntity, SwitchEntity):
|
|
|
|
"""Representation of the HomematicIP switching group."""
|
2019-02-17 05:24:13 +00:00
|
|
|
|
2019-10-11 14:36:46 +00:00
|
|
|
def __init__(self, hap: HomematicipHAP, device, post: str = "Group") -> None:
|
2019-02-17 05:24:13 +00:00
|
|
|
"""Initialize switching group."""
|
2019-09-03 15:27:14 +00:00
|
|
|
device.modelType = f"HmIP-{post}"
|
2019-10-11 14:36:46 +00:00
|
|
|
super().__init__(hap, device, post)
|
2019-02-17 05:24:13 +00:00
|
|
|
|
|
|
|
@property
|
2019-04-25 22:13:07 +00:00
|
|
|
def is_on(self) -> bool:
|
2019-02-17 05:24:13 +00:00
|
|
|
"""Return true if group is on."""
|
|
|
|
return self._device.on
|
|
|
|
|
2019-03-07 10:12:03 +00:00
|
|
|
@property
|
2019-04-25 22:13:07 +00:00
|
|
|
def available(self) -> bool:
|
2019-03-07 10:12:03 +00:00
|
|
|
"""Switch-Group available."""
|
|
|
|
# A switch-group must be available, and should not be affected by the
|
|
|
|
# individual availability of group members.
|
|
|
|
# This allows switching even when individual group members
|
|
|
|
# are not available.
|
|
|
|
return True
|
|
|
|
|
|
|
|
@property
|
2021-03-18 08:25:40 +00:00
|
|
|
def extra_state_attributes(self) -> dict[str, Any]:
|
2019-03-07 10:12:03 +00:00
|
|
|
"""Return the state attributes of the switch-group."""
|
2021-03-11 15:57:47 +00:00
|
|
|
state_attr = super().extra_state_attributes
|
2019-10-08 17:52:43 +00:00
|
|
|
|
2019-03-07 10:12:03 +00:00
|
|
|
if self._device.unreach:
|
2019-09-06 13:28:24 +00:00
|
|
|
state_attr[ATTR_GROUP_MEMBER_UNREACHABLE] = True
|
2019-10-08 17:52:43 +00:00
|
|
|
|
2019-09-06 13:28:24 +00:00
|
|
|
return state_attr
|
2019-03-07 10:12:03 +00:00
|
|
|
|
2022-08-30 18:55:01 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2019-02-17 05:24:13 +00:00
|
|
|
"""Turn the group on."""
|
|
|
|
await self._device.turn_on()
|
|
|
|
|
2022-08-30 18:55:01 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2019-02-17 05:24:13 +00:00
|
|
|
"""Turn the group off."""
|
|
|
|
await self._device.turn_off()
|
|
|
|
|
|
|
|
|
2018-05-08 07:57:51 +00:00
|
|
|
class HomematicipSwitchMeasuring(HomematicipSwitch):
|
2020-08-25 23:55:55 +00:00
|
|
|
"""Representation of the HomematicIP measuring switch."""
|