2019-04-03 15:40:03 +00:00
|
|
|
"""Switches on Zigbee Home Automation networks."""
|
2019-12-31 16:09:58 +00:00
|
|
|
import functools
|
2017-04-25 05:24:57 +00:00
|
|
|
import logging
|
|
|
|
|
2019-06-07 15:16:34 +00:00
|
|
|
from zigpy.zcl.foundation import Status
|
2019-08-02 10:05:23 +00:00
|
|
|
|
2017-04-25 05:24:57 +00:00
|
|
|
from homeassistant.components.switch import DOMAIN, SwitchDevice
|
2019-03-10 04:09:09 +00:00
|
|
|
from homeassistant.const import STATE_ON
|
|
|
|
from homeassistant.core import callback
|
2018-12-04 10:38:57 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2019-08-02 10:05:23 +00:00
|
|
|
|
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,
|
|
|
|
DATA_ZHA_DISPATCHERS,
|
|
|
|
SIGNAL_ATTR_UPDATED,
|
2019-08-02 10:05:23 +00:00
|
|
|
ZHA_DISCOVERY_NEW,
|
2019-02-06 18:33:21 +00:00
|
|
|
)
|
2019-12-31 16:09:58 +00:00
|
|
|
from .core.registries import ZHA_ENTITIES
|
2019-01-26 13:54:49 +00:00
|
|
|
from .entity import ZhaEntity
|
2017-04-25 05:24:57 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2019-12-31 16:09:58 +00:00
|
|
|
STRICT_MATCH = functools.partial(ZHA_ENTITIES.strict_match, DOMAIN)
|
2017-04-25 05:24:57 +00:00
|
|
|
|
|
|
|
|
2018-11-27 20:21:25 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up the Zigbee Home Automation switch from config entry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-11-27 20:21:25 +00:00
|
|
|
async def async_discover(discovery_info):
|
2019-07-31 19:25:30 +00:00
|
|
|
await _async_setup_entities(
|
|
|
|
hass, config_entry, async_add_entities, [discovery_info]
|
|
|
|
)
|
2018-09-13 07:11:47 +00:00
|
|
|
|
2018-11-27 20:21:25 +00:00
|
|
|
unsub = async_dispatcher_connect(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, ZHA_DISCOVERY_NEW.format(DOMAIN), async_discover
|
|
|
|
)
|
2018-11-27 20:21:25 +00:00
|
|
|
hass.data[DATA_ZHA][DATA_ZHA_DISPATCHERS].append(unsub)
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2018-11-27 20:21:25 +00:00
|
|
|
switches = hass.data.get(DATA_ZHA, {}).get(DOMAIN)
|
|
|
|
if switches is not None:
|
2019-07-31 19:25:30 +00:00
|
|
|
await _async_setup_entities(
|
|
|
|
hass, config_entry, async_add_entities, switches.values()
|
|
|
|
)
|
2018-11-27 20:21:25 +00:00
|
|
|
del hass.data[DATA_ZHA][DOMAIN]
|
2018-09-13 07:11:47 +00:00
|
|
|
|
2018-03-09 03:31:52 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def _async_setup_entities(
|
|
|
|
hass, config_entry, async_add_entities, discovery_infos
|
|
|
|
):
|
2018-11-27 20:21:25 +00:00
|
|
|
"""Set up the ZHA switches."""
|
|
|
|
entities = []
|
|
|
|
for discovery_info in discovery_infos:
|
2019-12-31 16:09:58 +00:00
|
|
|
zha_dev = discovery_info["zha_device"]
|
|
|
|
channels = discovery_info["channels"]
|
2018-11-27 20:21:25 +00:00
|
|
|
|
2019-12-31 16:09:58 +00:00
|
|
|
entity = ZHA_ENTITIES.get_entity(DOMAIN, zha_dev, channels, Switch)
|
|
|
|
if entity:
|
|
|
|
entities.append(entity(**discovery_info))
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2019-12-31 16:09:58 +00:00
|
|
|
if entities:
|
|
|
|
async_add_entities(entities, update_before_add=True)
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2019-12-31 16:09:58 +00:00
|
|
|
|
|
|
|
@STRICT_MATCH(channel_names=CHANNEL_ON_OFF)
|
2018-11-22 18:00:46 +00:00
|
|
|
class Switch(ZhaEntity, SwitchDevice):
|
2017-04-25 05:24:57 +00:00
|
|
|
"""ZHA switch."""
|
|
|
|
|
2019-02-06 18:33:21 +00:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
"""Initialize the ZHA switch."""
|
|
|
|
super().__init__(**kwargs)
|
2019-08-02 10:05:23 +00:00
|
|
|
self._on_off_channel = self.cluster_channels.get(CHANNEL_ON_OFF)
|
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
|
|
|
|
2018-03-09 03:31:52 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
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
|
|
|
|
self.async_schedule_update_ha_state()
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2018-03-09 03:31:52 +00:00
|
|
|
async def async_turn_off(self, **kwargs):
|
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
|
|
|
|
self.async_schedule_update_ha_state()
|
2019-02-06 18:33:21 +00:00
|
|
|
|
2020-01-29 21:59:45 +00:00
|
|
|
@callback
|
2019-02-06 18:33:21 +00:00
|
|
|
def async_set_state(self, state):
|
2019-02-19 17:58:22 +00:00
|
|
|
"""Handle state update from channel."""
|
2019-02-06 18:33:21 +00:00
|
|
|
self._state = bool(state)
|
2018-09-21 10:11:23 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2018-03-09 03:31:52 +00:00
|
|
|
|
2019-02-06 18:33:21 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return state attributes."""
|
|
|
|
return self.state_attributes
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Run when about to be added to hass."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
await 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
|
|
|
|
def async_restore_last_state(self, last_state):
|
|
|
|
"""Restore previous state."""
|
|
|
|
self._state = last_state.state == STATE_ON
|
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Attempt to retrieve on off state from the switch."""
|
|
|
|
await super().async_update()
|
|
|
|
if self._on_off_channel:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = await self._on_off_channel.get_attribute_value("on_off")
|