2020-05-29 07:59:44 +00:00
|
|
|
"""Platform for switch integration."""
|
2021-07-02 16:37:18 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from devolo_home_control_api.devices.zwave import Zwave
|
|
|
|
from devolo_home_control_api.homecontrol import HomeControl
|
|
|
|
|
2020-05-29 07:59:44 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2020-05-07 14:08:51 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-04-17 10:48:03 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-07-02 16:37:18 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-05-07 14:08:51 +00:00
|
|
|
|
|
|
|
from .const import DOMAIN
|
2020-09-24 15:57:52 +00:00
|
|
|
from .devolo_device import DevoloDeviceEntity
|
2020-05-07 14:08:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
2021-07-02 16:37:18 +00:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
2020-05-07 14:08:51 +00:00
|
|
|
) -> None:
|
|
|
|
"""Get all devices and setup the switch devices via config entry."""
|
|
|
|
entities = []
|
2020-10-22 13:01:44 +00:00
|
|
|
|
2020-10-28 19:54:57 +00:00
|
|
|
for gateway in hass.data[DOMAIN][entry.entry_id]["gateways"]:
|
2020-10-22 13:01:44 +00:00
|
|
|
for device in gateway.binary_switch_devices:
|
|
|
|
for binary_switch in device.binary_switch_property:
|
|
|
|
# Exclude the binary switch which also has multi_level_switches here,
|
|
|
|
# because those are implemented as light entities now.
|
|
|
|
if not hasattr(device, "multi_level_switch_property"):
|
|
|
|
entities.append(
|
|
|
|
DevoloSwitch(
|
|
|
|
homecontrol=gateway,
|
|
|
|
device_instance=device,
|
|
|
|
element_uid=binary_switch,
|
|
|
|
)
|
2020-08-05 16:16:21 +00:00
|
|
|
)
|
2020-10-22 13:01:44 +00:00
|
|
|
|
2020-05-07 14:08:51 +00:00
|
|
|
async_add_entities(entities)
|
|
|
|
|
|
|
|
|
2020-09-24 15:57:52 +00:00
|
|
|
class DevoloSwitch(DevoloDeviceEntity, SwitchEntity):
|
2020-05-29 07:59:44 +00:00
|
|
|
"""Representation of a switch."""
|
2020-05-07 14:08:51 +00:00
|
|
|
|
2021-07-02 16:37:18 +00:00
|
|
|
def __init__(
|
|
|
|
self, homecontrol: HomeControl, device_instance: Zwave, element_uid: str
|
|
|
|
) -> None:
|
2020-05-07 14:08:51 +00:00
|
|
|
"""Initialize an devolo Switch."""
|
2020-09-24 15:57:52 +00:00
|
|
|
super().__init__(
|
|
|
|
homecontrol=homecontrol,
|
|
|
|
device_instance=device_instance,
|
|
|
|
element_uid=element_uid,
|
|
|
|
)
|
2022-04-12 17:07:17 +00:00
|
|
|
self._binary_switch_property = self._device_instance.binary_switch_property[
|
|
|
|
self._attr_unique_id # type: ignore[index]
|
|
|
|
]
|
2021-07-24 04:48:32 +00:00
|
|
|
self._attr_is_on = self._binary_switch_property.state
|
2020-05-07 14:08:51 +00:00
|
|
|
|
2021-07-02 16:37:18 +00:00
|
|
|
def turn_on(self, **kwargs: Any) -> None:
|
2020-05-07 14:08:51 +00:00
|
|
|
"""Switch on the device."""
|
2020-05-08 19:41:56 +00:00
|
|
|
self._binary_switch_property.set(state=True)
|
2020-05-07 14:08:51 +00:00
|
|
|
|
2021-07-02 16:37:18 +00:00
|
|
|
def turn_off(self, **kwargs: Any) -> None:
|
2020-05-07 14:08:51 +00:00
|
|
|
"""Switch off the device."""
|
2020-05-08 19:41:56 +00:00
|
|
|
self._binary_switch_property.set(state=False)
|
2020-05-07 14:08:51 +00:00
|
|
|
|
2021-07-02 16:37:18 +00:00
|
|
|
def _sync(self, message: tuple) -> None:
|
2020-05-07 14:08:51 +00:00
|
|
|
"""Update the binary switch state and consumption."""
|
|
|
|
if message[0].startswith("devolo.BinarySwitch"):
|
2021-07-24 04:48:32 +00:00
|
|
|
self._attr_is_on = self._device_instance.binary_switch_property[
|
|
|
|
message[0]
|
|
|
|
].state
|
2020-05-07 14:08:51 +00:00
|
|
|
else:
|
2020-09-28 15:37:46 +00:00
|
|
|
self._generic_message(message)
|
2020-05-07 14:08:51 +00:00
|
|
|
self.schedule_update_ha_state()
|