2019-02-13 21:21:14 +01:00
|
|
|
"""Support for deCONZ switches."""
|
2021-09-29 21:19:21 +02:00
|
|
|
|
2021-11-16 17:25:56 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from pydeconz.light import Light, Siren
|
2021-09-29 21:19:21 +02:00
|
|
|
|
2020-09-25 22:49:28 +02:00
|
|
|
from homeassistant.components.switch import DOMAIN, SwitchEntity
|
2021-11-16 17:25:56 +01:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-10-20 11:16:28 +02:00
|
|
|
from homeassistant.helpers import entity_registry as er
|
2018-08-01 11:03:08 +02:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2021-11-16 17:25:56 +01:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2018-08-01 11:03:08 +02:00
|
|
|
|
2021-10-07 12:48:27 +02:00
|
|
|
from .const import DOMAIN as DECONZ_DOMAIN, POWER_PLUGS
|
2019-01-16 08:33:04 +01:00
|
|
|
from .deconz_device import DeconzDevice
|
2019-04-05 02:48:24 +02:00
|
|
|
from .gateway import get_gateway_from_config_entry
|
2019-01-16 08:33:04 +01:00
|
|
|
|
2018-08-01 11:03:08 +02:00
|
|
|
|
2021-11-16 17:25:56 +01:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2018-08-01 11:03:08 +02:00
|
|
|
"""Set up switches for deCONZ component.
|
|
|
|
|
2020-09-25 22:49:28 +02:00
|
|
|
Switches are based on the same device class as lights in deCONZ.
|
2018-08-01 11:03:08 +02:00
|
|
|
"""
|
2019-04-05 02:48:24 +02:00
|
|
|
gateway = get_gateway_from_config_entry(hass, config_entry)
|
2020-09-25 22:49:28 +02:00
|
|
|
gateway.entities[DOMAIN] = set()
|
2018-11-05 16:21:44 +01:00
|
|
|
|
2021-10-20 11:16:28 +02:00
|
|
|
entity_registry = er.async_get(hass)
|
2021-09-18 21:59:04 +02:00
|
|
|
|
|
|
|
# Siren platform replacing sirens in switch platform added in 2021.10
|
2022-04-05 22:44:04 +02:00
|
|
|
for light in gateway.api.lights.sirens.values():
|
2021-09-29 21:19:21 +02:00
|
|
|
if isinstance(light, Siren) and (
|
|
|
|
entity_id := entity_registry.async_get_entity_id(
|
|
|
|
DOMAIN, DECONZ_DOMAIN, light.unique_id
|
|
|
|
)
|
2021-09-18 21:59:04 +02:00
|
|
|
):
|
|
|
|
entity_registry.async_remove(entity_id)
|
|
|
|
|
2018-08-01 11:03:08 +02:00
|
|
|
@callback
|
2022-04-05 22:44:04 +02:00
|
|
|
def async_add_switch(lights: list[Light] | None = None) -> None:
|
2018-08-01 11:03:08 +02:00
|
|
|
"""Add switch from deCONZ."""
|
|
|
|
entities = []
|
2019-04-05 02:48:24 +02:00
|
|
|
|
2022-04-05 22:44:04 +02:00
|
|
|
if lights is None:
|
|
|
|
lights = list(gateway.api.lights.lights.values())
|
|
|
|
|
2018-08-01 11:03:08 +02:00
|
|
|
for light in lights:
|
2019-04-05 02:48:24 +02:00
|
|
|
|
2020-09-25 22:49:28 +02:00
|
|
|
if (
|
|
|
|
light.type in POWER_PLUGS
|
2021-09-18 09:05:08 +02:00
|
|
|
and light.unique_id not in gateway.entities[DOMAIN]
|
2020-09-25 22:49:28 +02:00
|
|
|
):
|
2018-11-05 16:21:44 +01:00
|
|
|
entities.append(DeconzPowerPlug(light, gateway))
|
2019-04-05 02:48:24 +02:00
|
|
|
|
2020-10-02 11:20:33 +02:00
|
|
|
if entities:
|
2020-10-16 17:14:26 +02:00
|
|
|
async_add_entities(entities)
|
2018-08-01 11:03:08 +02:00
|
|
|
|
2021-04-20 20:20:57 +02:00
|
|
|
config_entry.async_on_unload(
|
2019-07-31 12:25:30 -07:00
|
|
|
async_dispatcher_connect(
|
2021-10-07 12:48:27 +02:00
|
|
|
hass,
|
|
|
|
gateway.signal_new_light,
|
|
|
|
async_add_switch,
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
|
|
|
)
|
2018-08-01 11:03:08 +02:00
|
|
|
|
2020-12-02 16:21:27 +01:00
|
|
|
async_add_switch()
|
2018-08-01 11:03:08 +02:00
|
|
|
|
|
|
|
|
2020-04-26 18:50:37 +02:00
|
|
|
class DeconzPowerPlug(DeconzDevice, SwitchEntity):
|
2019-01-16 08:33:04 +01:00
|
|
|
"""Representation of a deCONZ power plug."""
|
2018-08-10 19:22:12 +02:00
|
|
|
|
2020-09-25 22:49:28 +02:00
|
|
|
TYPE = DOMAIN
|
2021-11-16 17:25:56 +01:00
|
|
|
_device: Light
|
2020-09-25 22:49:28 +02:00
|
|
|
|
2018-08-10 19:22:12 +02:00
|
|
|
@property
|
2021-11-16 17:25:56 +01:00
|
|
|
def is_on(self) -> bool:
|
2018-08-10 19:22:12 +02:00
|
|
|
"""Return true if switch is on."""
|
2022-04-23 07:27:47 +02:00
|
|
|
return self._device.on
|
2018-08-10 19:22:12 +02:00
|
|
|
|
2021-11-16 17:25:56 +01:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2018-08-01 11:03:08 +02:00
|
|
|
"""Turn on switch."""
|
2021-09-18 09:05:08 +02:00
|
|
|
await self._device.set_state(on=True)
|
2018-08-01 11:03:08 +02:00
|
|
|
|
2021-11-16 17:25:56 +01:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2018-08-01 11:03:08 +02:00
|
|
|
"""Turn off switch."""
|
2021-09-18 09:05:08 +02:00
|
|
|
await self._device.set_state(on=False)
|