core/homeassistant/components/homematicip_cloud/switch.py

172 lines
6.2 KiB
Python
Raw Normal View History

"""Support for HomematicIP Cloud switches."""
2021-03-18 08:25:40 +00:00
from __future__ import annotations
from typing import Any
from homematicip.aio.device import (
AsyncBrandSwitch2,
2019-07-31 19:25:30 +00:00
AsyncBrandSwitchMeasuring,
AsyncDinRailSwitch,
AsyncDinRailSwitch4,
AsyncFullFlushInputSwitch,
2019-07-31 19:25:30 +00:00
AsyncFullFlushSwitchMeasuring,
AsyncHeatingSwitch2,
2019-07-31 19:25:30 +00:00
AsyncMultiIOBox,
AsyncOpenCollector8Module,
AsyncPlugableSwitch,
AsyncPlugableSwitchMeasuring,
AsyncPrintedCircuitBoardSwitch2,
AsyncPrintedCircuitBoardSwitchBattery,
AsyncWiredSwitch8,
2019-07-31 19:25:30 +00:00
)
from homematicip.aio.group import AsyncExtendedLinkedSwitchingGroup, AsyncSwitchingGroup
from homeassistant.components.switch import SwitchEntity
2019-04-25 22:13:07 +00:00
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import DOMAIN as HMIPC_DOMAIN, HomematicipGenericEntity
from .generic_entity import ATTR_GROUP_MEMBER_UNREACHABLE
from .hap import HomematicipHAP
2019-07-31 19:25:30 +00:00
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
2019-07-31 19:25:30 +00:00
) -> None:
Add HomematicIP Cloud Config Flow and Entries loading (#14861) * Add HomematicIP Cloud to config flow * Inititial trial for config_flow * Integrations text files * Load and write config_flow and init homematicip_cloud * Split into dedicated files * Ceanup of text messages * Working config_flow * Move imports inside a function * Enable laoding even no accesspoints are defined * Revert unnecassary changes in CONFIG_SCHEMA * Better error handling * fix flask8 * Migration to async for token generation * A few fixes * Simplify config_flow * Bump version to 9.6 with renamed package * Requirements file * First fixes after review * Implement async_step_import * Cleanup for Config Flow * First tests for homematicip_cloud setup * Remove config_flow tests * Really remove all things * Fix comment * Update picture * Add support for async_setup_entry to switch and climate platform * Update path of the config_flow picture * Refactoring for better tesability * Further tests implemented * Move 3th party lib inside function * Fix lint * Update requirments_test_all.txt file * UPdate of requirments_test_all.txt did not work * Furder cleanup in websocket connection * Remove a test for the hap * Revert "Remove a test for the hap" This reverts commit 968d58cba108e0f371022c7ab540374aa2ab13f4. * First tests implemented for config_flow * Fix lint * Rework of client registration process * Implemented tests for config_flow 100% coverage * Cleanup * Cleanup comments and code * Try to fix import problem * Add homematicip to the test env requirements
2018-07-06 21:05:34 +00:00
"""Set up the HomematicIP switch from a config entry."""
hap = hass.data[HMIPC_DOMAIN][config_entry.unique_id]
entities: list[HomematicipGenericEntity] = []
for device in hap.home.devices:
if isinstance(device, AsyncBrandSwitchMeasuring):
# BrandSwitchMeasuring inherits PlugableSwitchMeasuring
# This entity is implemented in the light platform and will
# not be added in the switch platform
pass
2019-07-31 19:25:30 +00:00
elif isinstance(
device, (AsyncPlugableSwitchMeasuring, AsyncFullFlushSwitchMeasuring)
):
entities.append(HomematicipSwitchMeasuring(hap, device))
elif isinstance(device, AsyncWiredSwitch8):
for channel in range(1, 9):
entities.append(HomematicipMultiSwitch(hap, device, channel=channel))
elif isinstance(device, AsyncDinRailSwitch):
entities.append(HomematicipMultiSwitch(hap, device, channel=1))
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(
device,
(
AsyncPlugableSwitch,
AsyncPrintedCircuitBoardSwitchBattery,
AsyncFullFlushInputSwitch,
),
2019-07-31 19:25:30 +00:00
):
entities.append(HomematicipSwitch(hap, device))
elif isinstance(device, AsyncOpenCollector8Module):
for channel in range(1, 9):
entities.append(HomematicipMultiSwitch(hap, device, channel=channel))
elif isinstance(device, AsyncHeatingSwitch2):
for channel in range(1, 3):
entities.append(HomematicipMultiSwitch(hap, device, channel=channel))
elif isinstance(device, AsyncMultiIOBox):
for channel in range(1, 3):
entities.append(HomematicipMultiSwitch(hap, device, channel=channel))
elif isinstance(device, AsyncPrintedCircuitBoardSwitch2):
for channel in range(1, 3):
entities.append(HomematicipMultiSwitch(hap, device, channel=channel))
elif isinstance(device, AsyncBrandSwitch2):
for channel in range(1, 3):
entities.append(HomematicipMultiSwitch(hap, device, channel=channel))
for group in hap.home.groups:
if isinstance(group, (AsyncExtendedLinkedSwitchingGroup, AsyncSwitchingGroup)):
entities.append(HomematicipGroupSwitch(hap, group))
async_add_entities(entities)
class HomematicipMultiSwitch(HomematicipGenericEntity, SwitchEntity):
"""Representation of the HomematicIP multi switch."""
def __init__(
self,
hap: HomematicipHAP,
device,
channel=1,
is_multi_channel=True,
) -> None:
"""Initialize the multi switch device."""
super().__init__(
hap, device, channel=channel, is_multi_channel=is_multi_channel
)
@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:
"""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:
"""Turn the switch off."""
await self._device.turn_off(self._channel)
class HomematicipSwitch(HomematicipMultiSwitch, SwitchEntity):
"""Representation of the HomematicIP switch."""
def __init__(self, hap: HomematicipHAP, device) -> None:
"""Initialize the switch device."""
super().__init__(hap, device, is_multi_channel=False)
class HomematicipGroupSwitch(HomematicipGenericEntity, SwitchEntity):
"""Representation of the HomematicIP switching group."""
def __init__(self, hap: HomematicipHAP, device, post: str = "Group") -> None:
"""Initialize switching group."""
device.modelType = f"HmIP-{post}"
super().__init__(hap, device, post)
@property
2019-04-25 22:13:07 +00:00
def is_on(self) -> bool:
"""Return true if group is on."""
return self._device.on
@property
2019-04-25 22:13:07 +00:00
def available(self) -> bool:
"""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]:
"""Return the state attributes of the switch-group."""
state_attr = super().extra_state_attributes
if self._device.unreach:
state_attr[ATTR_GROUP_MEMBER_UNREACHABLE] = True
return state_attr
2022-08-30 18:55:01 +00:00
async def async_turn_on(self, **kwargs: Any) -> None:
"""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:
"""Turn the group off."""
await self._device.turn_off()
class HomematicipSwitchMeasuring(HomematicipSwitch):
"""Representation of the HomematicIP measuring switch."""