2019-04-03 15:40:03 +00:00
|
|
|
"""Switches on Zigbee Home Automation networks."""
|
2021-03-18 14:08:35 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-12-31 16:09:58 +00:00
|
|
|
import functools
|
2021-03-18 14:08:35 +00:00
|
|
|
from typing import Any
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2020-03-25 17:29:40 +00:00
|
|
|
from zigpy.zcl.clusters.general import OnOff
|
2019-06-07 15:16:34 +00:00
|
|
|
from zigpy.zcl.foundation import Status
|
2019-08-02 10:05:23 +00:00
|
|
|
|
2021-12-11 16:06:39 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2021-12-11 16:50:03 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-11 16:06:39 +00:00
|
|
|
from homeassistant.const import STATE_ON, STATE_UNAVAILABLE, Platform
|
2021-12-11 16:50:03 +00:00
|
|
|
from homeassistant.core import HomeAssistant, State, callback
|
2018-12-04 10:38:57 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2021-12-11 16:50:03 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-08-02 10:05:23 +00:00
|
|
|
|
2020-02-21 23:06:57 +00:00
|
|
|
from .core import discovery
|
2019-01-26 13:54:49 +00:00
|
|
|
from .core.const import (
|
2019-08-02 14:37:21 +00:00
|
|
|
CHANNEL_ON_OFF,
|
2019-07-31 19:25:30 +00:00
|
|
|
DATA_ZHA,
|
2020-02-21 23:06:57 +00:00
|
|
|
SIGNAL_ADD_ENTITIES,
|
2019-07-31 19:25:30 +00:00
|
|
|
SIGNAL_ATTR_UPDATED,
|
2019-02-06 18:33:21 +00:00
|
|
|
)
|
2019-12-31 16:09:58 +00:00
|
|
|
from .core.registries import ZHA_ENTITIES
|
2020-03-29 00:38:48 +00:00
|
|
|
from .entity import ZhaEntity, ZhaGroupEntity
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2021-12-11 16:06:39 +00:00
|
|
|
STRICT_MATCH = functools.partial(ZHA_ENTITIES.strict_match, Platform.SWITCH)
|
|
|
|
GROUP_MATCH = functools.partial(ZHA_ENTITIES.group_match, Platform.SWITCH)
|
2017-04-25 05:24:57 +00:00
|
|
|
|
|
|
|
|
2021-12-11 16:50:03 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
):
|
2018-11-27 20:21:25 +00:00
|
|
|
"""Set up the Zigbee Home Automation switch from config entry."""
|
2021-12-11 16:06:39 +00:00
|
|
|
entities_to_create = hass.data[DATA_ZHA][Platform.SWITCH]
|
2018-09-13 07:11:47 +00:00
|
|
|
|
2018-11-27 20:21:25 +00:00
|
|
|
unsub = async_dispatcher_connect(
|
2020-02-21 23:06:57 +00:00
|
|
|
hass,
|
|
|
|
SIGNAL_ADD_ENTITIES,
|
|
|
|
functools.partial(
|
|
|
|
discovery.async_add_entities, async_add_entities, entities_to_create
|
|
|
|
),
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2021-12-11 16:50:03 +00:00
|
|
|
config_entry.async_on_unload(unsub)
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2019-12-31 16:09:58 +00:00
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class BaseSwitch(SwitchEntity):
|
2020-03-25 17:29:40 +00:00
|
|
|
"""Common base class for zha switches."""
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2020-03-25 17:29:40 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2019-02-06 18:33:21 +00:00
|
|
|
"""Initialize the ZHA switch."""
|
2020-03-25 17:29:40 +00:00
|
|
|
self._on_off_channel = None
|
|
|
|
self._state = None
|
|
|
|
super().__init__(*args, **kwargs)
|
2018-12-19 13:52:20 +00:00
|
|
|
|
2017-04-25 05:24:57 +00:00
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return if the switch is on based on the statemachine."""
|
2018-05-12 12:41:44 +00:00
|
|
|
if self._state is None:
|
2017-04-25 05:24:57 +00:00
|
|
|
return False
|
2019-02-06 18:33:21 +00:00
|
|
|
return self._state
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2020-03-25 17:29:40 +00:00
|
|
|
async def async_turn_on(self, **kwargs) -> None:
|
2017-04-25 05:24:57 +00:00
|
|
|
"""Turn the entity on."""
|
2019-06-07 15:16:34 +00:00
|
|
|
result = await self._on_off_channel.on()
|
|
|
|
if not isinstance(result, list) or result[1] is not Status.SUCCESS:
|
2019-03-02 19:09:01 +00:00
|
|
|
return
|
|
|
|
self._state = True
|
2020-03-11 11:17:53 +00:00
|
|
|
self.async_write_ha_state()
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2020-03-25 17:29:40 +00:00
|
|
|
async def async_turn_off(self, **kwargs) -> None:
|
2017-04-25 05:24:57 +00:00
|
|
|
"""Turn the entity off."""
|
2019-06-07 15:16:34 +00:00
|
|
|
result = await self._on_off_channel.off()
|
|
|
|
if not isinstance(result, list) or result[1] is not Status.SUCCESS:
|
2019-03-02 19:09:01 +00:00
|
|
|
return
|
|
|
|
self._state = False
|
2020-03-11 11:17:53 +00:00
|
|
|
self.async_write_ha_state()
|
2019-02-06 18:33:21 +00:00
|
|
|
|
2020-03-25 17:29:40 +00:00
|
|
|
|
|
|
|
@STRICT_MATCH(channel_names=CHANNEL_ON_OFF)
|
2020-03-29 00:38:48 +00:00
|
|
|
class Switch(BaseSwitch, ZhaEntity):
|
2020-03-25 17:29:40 +00:00
|
|
|
"""ZHA switch."""
|
|
|
|
|
|
|
|
def __init__(self, unique_id, zha_device, channels, **kwargs):
|
|
|
|
"""Initialize the ZHA switch."""
|
|
|
|
super().__init__(unique_id, zha_device, channels, **kwargs)
|
|
|
|
self._on_off_channel = self.cluster_channels.get(CHANNEL_ON_OFF)
|
|
|
|
|
2020-01-29 21:59:45 +00:00
|
|
|
@callback
|
2020-03-25 17:29:40 +00:00
|
|
|
def async_set_state(self, attr_id: int, attr_name: str, value: Any):
|
2019-02-19 17:58:22 +00:00
|
|
|
"""Handle state update from channel."""
|
2020-03-04 18:11:53 +00:00
|
|
|
self._state = bool(value)
|
2020-03-11 11:17:53 +00:00
|
|
|
self.async_write_ha_state()
|
2018-03-09 03:31:52 +00:00
|
|
|
|
2020-03-25 17:29:40 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
2019-02-06 18:33:21 +00:00
|
|
|
"""Run when about to be added to hass."""
|
|
|
|
await super().async_added_to_hass()
|
2020-07-20 14:04:57 +00:00
|
|
|
self.async_accept_signal(
|
2019-07-31 19:25:30 +00:00
|
|
|
self._on_off_channel, SIGNAL_ATTR_UPDATED, self.async_set_state
|
|
|
|
)
|
2019-03-10 04:09:09 +00:00
|
|
|
|
|
|
|
@callback
|
2020-03-25 17:29:40 +00:00
|
|
|
def async_restore_last_state(self, last_state) -> None:
|
2019-03-10 04:09:09 +00:00
|
|
|
"""Restore previous state."""
|
|
|
|
self._state = last_state.state == STATE_ON
|
|
|
|
|
2020-03-25 17:29:40 +00:00
|
|
|
async def async_update(self) -> None:
|
2019-03-10 04:09:09 +00:00
|
|
|
"""Attempt to retrieve on off state from the switch."""
|
|
|
|
await super().async_update()
|
|
|
|
if self._on_off_channel:
|
2020-03-07 12:33:59 +00:00
|
|
|
state = await self._on_off_channel.get_attribute_value("on_off")
|
|
|
|
if state is not None:
|
|
|
|
self._state = state
|
2020-03-25 17:29:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
@GROUP_MATCH()
|
2020-03-29 00:38:48 +00:00
|
|
|
class SwitchGroup(BaseSwitch, ZhaGroupEntity):
|
2020-03-25 17:29:40 +00:00
|
|
|
"""Representation of a switch group."""
|
|
|
|
|
|
|
|
def __init__(
|
2021-03-18 14:08:35 +00:00
|
|
|
self, entity_ids: list[str], unique_id: str, group_id: int, zha_device, **kwargs
|
2020-03-25 17:29:40 +00:00
|
|
|
) -> None:
|
|
|
|
"""Initialize a switch group."""
|
2020-03-29 00:38:48 +00:00
|
|
|
super().__init__(entity_ids, unique_id, group_id, zha_device, **kwargs)
|
2020-03-25 17:29:40 +00:00
|
|
|
self._available: bool = False
|
|
|
|
group = self.zha_device.gateway.get_group(self._group_id)
|
|
|
|
self._on_off_channel = group.endpoint[OnOff.cluster_id]
|
|
|
|
|
|
|
|
async def async_update(self) -> None:
|
|
|
|
"""Query all members and determine the light group state."""
|
|
|
|
all_states = [self.hass.states.get(x) for x in self._entity_ids]
|
2021-03-18 14:08:35 +00:00
|
|
|
states: list[State] = list(filter(None, all_states))
|
2020-03-25 17:29:40 +00:00
|
|
|
on_states = [state for state in states if state.state == STATE_ON]
|
|
|
|
|
|
|
|
self._state = len(on_states) > 0
|
|
|
|
self._available = any(state.state != STATE_UNAVAILABLE for state in states)
|