2019-02-13 20:21:14 +00:00
|
|
|
"""Support for deCONZ switches."""
|
2021-09-29 19:19:21 +00:00
|
|
|
|
2021-11-16 16:25:56 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
2022-05-11 11:31:16 +00:00
|
|
|
from pydeconz.models.event import EventType
|
2022-04-24 08:27:56 +00:00
|
|
|
from pydeconz.models.light.light import Light
|
2021-09-29 19:19:21 +00:00
|
|
|
|
2020-09-25 20:49:28 +00:00
|
|
|
from homeassistant.components.switch import DOMAIN, SwitchEntity
|
2021-11-16 16:25:56 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2018-08-01 09:03:08 +00:00
|
|
|
|
2022-04-24 21:32:13 +00:00
|
|
|
from .const import POWER_PLUGS
|
2019-01-16 07:33:04 +00:00
|
|
|
from .deconz_device import DeconzDevice
|
2019-04-05 00:48:24 +00:00
|
|
|
from .gateway import get_gateway_from_config_entry
|
2019-01-16 07:33:04 +00:00
|
|
|
|
2018-08-01 09:03:08 +00:00
|
|
|
|
2021-11-16 16:25:56 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2018-08-01 09:03:08 +00:00
|
|
|
"""Set up switches for deCONZ component.
|
|
|
|
|
2020-09-25 20:49:28 +00:00
|
|
|
Switches are based on the same device class as lights in deCONZ.
|
2018-08-01 09:03:08 +00:00
|
|
|
"""
|
2019-04-05 00:48:24 +00:00
|
|
|
gateway = get_gateway_from_config_entry(hass, config_entry)
|
2020-09-25 20:49:28 +00:00
|
|
|
gateway.entities[DOMAIN] = set()
|
2018-11-05 15:21:44 +00:00
|
|
|
|
2018-08-01 09:03:08 +00:00
|
|
|
@callback
|
2022-05-16 22:04:57 +00:00
|
|
|
def async_add_switch(_: EventType, switch_id: str) -> None:
|
2018-08-01 09:03:08 +00:00
|
|
|
"""Add switch from deCONZ."""
|
2022-05-16 22:04:57 +00:00
|
|
|
switch = gateway.api.lights.lights[switch_id]
|
|
|
|
if switch.type not in POWER_PLUGS:
|
2022-05-11 11:31:16 +00:00
|
|
|
return
|
2022-05-16 22:04:57 +00:00
|
|
|
async_add_entities([DeconzPowerPlug(switch, gateway)])
|
2018-08-01 09:03:08 +00:00
|
|
|
|
2022-05-25 03:48:09 +00:00
|
|
|
gateway.register_platform_add_device_callback(
|
|
|
|
async_add_switch,
|
|
|
|
gateway.api.lights.lights,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-08-01 09:03:08 +00:00
|
|
|
|
|
|
|
|
2022-08-01 17:18:29 +00:00
|
|
|
class DeconzPowerPlug(DeconzDevice[Light], SwitchEntity):
|
2019-01-16 07:33:04 +00:00
|
|
|
"""Representation of a deCONZ power plug."""
|
2018-08-10 17:22:12 +00:00
|
|
|
|
2020-09-25 20:49:28 +00:00
|
|
|
TYPE = DOMAIN
|
|
|
|
|
2018-08-10 17:22:12 +00:00
|
|
|
@property
|
2021-11-16 16:25:56 +00:00
|
|
|
def is_on(self) -> bool:
|
2018-08-10 17:22:12 +00:00
|
|
|
"""Return true if switch is on."""
|
2022-04-23 05:27:47 +00:00
|
|
|
return self._device.on
|
2018-08-10 17:22:12 +00:00
|
|
|
|
2021-11-16 16:25:56 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2018-08-01 09:03:08 +00:00
|
|
|
"""Turn on switch."""
|
2022-06-21 14:50:44 +00:00
|
|
|
await self.gateway.api.lights.lights.set_state(
|
|
|
|
id=self._device.resource_id,
|
|
|
|
on=True,
|
|
|
|
)
|
2018-08-01 09:03:08 +00:00
|
|
|
|
2021-11-16 16:25:56 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2018-08-01 09:03:08 +00:00
|
|
|
"""Turn off switch."""
|
2022-06-21 14:50:44 +00:00
|
|
|
await self.gateway.api.lights.lights.set_state(
|
|
|
|
id=self._device.resource_id,
|
|
|
|
on=False,
|
|
|
|
)
|