2018-10-31 21:38:04 +00:00
|
|
|
"""Representation of a deCONZ gateway."""
|
2019-03-24 18:27:32 +00:00
|
|
|
import asyncio
|
|
|
|
import async_timeout
|
|
|
|
|
2019-05-27 04:56:00 +00:00
|
|
|
from pydeconz import DeconzSession, errors
|
|
|
|
from pydeconz.sensor import Switch
|
|
|
|
|
2019-02-14 04:36:06 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2019-03-24 18:27:32 +00:00
|
|
|
from homeassistant.const import CONF_EVENT, CONF_HOST, CONF_ID
|
2018-10-31 21:38:04 +00:00
|
|
|
from homeassistant.core import EventOrigin, callback
|
|
|
|
from homeassistant.helpers import aiohttp_client
|
2019-04-05 00:48:24 +00:00
|
|
|
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
|
2018-10-31 21:38:04 +00:00
|
|
|
from homeassistant.helpers.dispatcher import (
|
2019-07-31 19:25:30 +00:00
|
|
|
async_dispatcher_connect,
|
|
|
|
async_dispatcher_send,
|
|
|
|
)
|
2019-09-05 23:38:00 +00:00
|
|
|
from homeassistant.helpers.entity_registry import (
|
|
|
|
async_get_registry,
|
|
|
|
DISABLED_CONFIG_ENTRY,
|
|
|
|
)
|
2018-10-31 21:38:04 +00:00
|
|
|
from homeassistant.util import slugify
|
|
|
|
|
|
|
|
from .const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER,
|
|
|
|
CONF_ALLOW_CLIP_SENSOR,
|
|
|
|
CONF_ALLOW_DECONZ_GROUPS,
|
|
|
|
CONF_BRIDGEID,
|
|
|
|
CONF_MASTER_GATEWAY,
|
2019-09-05 23:38:00 +00:00
|
|
|
DEFAULT_ALLOW_CLIP_SENSOR,
|
|
|
|
DEFAULT_ALLOW_DECONZ_GROUPS,
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN,
|
|
|
|
NEW_DEVICE,
|
|
|
|
NEW_SENSOR,
|
|
|
|
SUPPORTED_PLATFORMS,
|
|
|
|
)
|
2019-03-24 18:27:32 +00:00
|
|
|
from .errors import AuthenticationRequired, CannotConnect
|
2018-10-31 21:38:04 +00:00
|
|
|
|
|
|
|
|
2019-04-05 00:48:24 +00:00
|
|
|
@callback
|
|
|
|
def get_gateway_from_config_entry(hass, config_entry):
|
|
|
|
"""Return gateway with a matching bridge id."""
|
|
|
|
return hass.data[DOMAIN][config_entry.data[CONF_BRIDGEID]]
|
|
|
|
|
|
|
|
|
2018-10-31 21:38:04 +00:00
|
|
|
class DeconzGateway:
|
|
|
|
"""Manages a single deCONZ gateway."""
|
|
|
|
|
|
|
|
def __init__(self, hass, config_entry):
|
|
|
|
"""Initialize the system."""
|
|
|
|
self.hass = hass
|
|
|
|
self.config_entry = config_entry
|
2018-11-05 15:21:44 +00:00
|
|
|
self.available = True
|
2018-10-31 21:38:04 +00:00
|
|
|
self.api = None
|
|
|
|
|
|
|
|
self.deconz_ids = {}
|
|
|
|
self.events = []
|
|
|
|
self.listeners = []
|
|
|
|
|
2019-04-05 00:48:24 +00:00
|
|
|
@property
|
|
|
|
def bridgeid(self) -> str:
|
|
|
|
"""Return the unique identifier of the gateway."""
|
|
|
|
return self.config_entry.data[CONF_BRIDGEID]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def master(self) -> bool:
|
|
|
|
"""Gateway which is used with deCONZ services without defining id."""
|
|
|
|
return self.config_entry.options[CONF_MASTER_GATEWAY]
|
|
|
|
|
|
|
|
@property
|
2019-09-05 23:38:00 +00:00
|
|
|
def option_allow_clip_sensor(self) -> bool:
|
2019-04-05 00:48:24 +00:00
|
|
|
"""Allow loading clip sensor from gateway."""
|
2019-09-05 23:38:00 +00:00
|
|
|
return self.config_entry.options.get(
|
|
|
|
CONF_ALLOW_CLIP_SENSOR, DEFAULT_ALLOW_CLIP_SENSOR
|
|
|
|
)
|
2019-04-05 00:48:24 +00:00
|
|
|
|
|
|
|
@property
|
2019-09-05 23:38:00 +00:00
|
|
|
def option_allow_deconz_groups(self) -> bool:
|
2019-04-05 00:48:24 +00:00
|
|
|
"""Allow loading deCONZ groups from gateway."""
|
2019-09-05 23:38:00 +00:00
|
|
|
return self.config_entry.options.get(
|
|
|
|
CONF_ALLOW_DECONZ_GROUPS, DEFAULT_ALLOW_DECONZ_GROUPS
|
|
|
|
)
|
2019-04-05 00:48:24 +00:00
|
|
|
|
|
|
|
async def async_update_device_registry(self):
|
|
|
|
"""Update device registry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
device_registry = await self.hass.helpers.device_registry.async_get_registry()
|
2019-04-05 00:48:24 +00:00
|
|
|
device_registry.async_get_or_create(
|
|
|
|
config_entry_id=self.config_entry.entry_id,
|
|
|
|
connections={(CONNECTION_NETWORK_MAC, self.api.config.mac)},
|
|
|
|
identifiers={(DOMAIN, self.api.config.bridgeid)},
|
2019-07-31 19:25:30 +00:00
|
|
|
manufacturer="Dresden Elektronik",
|
2019-04-05 00:48:24 +00:00
|
|
|
model=self.api.config.modelid,
|
|
|
|
name=self.api.config.name,
|
2019-07-31 19:25:30 +00:00
|
|
|
sw_version=self.api.config.swversion,
|
2019-04-05 00:48:24 +00:00
|
|
|
)
|
|
|
|
|
2019-03-24 18:27:32 +00:00
|
|
|
async def async_setup(self):
|
2018-10-31 21:38:04 +00:00
|
|
|
"""Set up a deCONZ gateway."""
|
|
|
|
hass = self.hass
|
|
|
|
|
2019-03-24 18:27:32 +00:00
|
|
|
try:
|
|
|
|
self.api = await get_gateway(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass,
|
|
|
|
self.config_entry.data,
|
|
|
|
self.async_add_device_callback,
|
|
|
|
self.async_connection_status_callback,
|
2019-03-24 18:27:32 +00:00
|
|
|
)
|
2018-10-31 21:38:04 +00:00
|
|
|
|
2019-03-24 18:27:32 +00:00
|
|
|
except CannotConnect:
|
2019-02-14 04:36:06 +00:00
|
|
|
raise ConfigEntryNotReady
|
2018-10-31 21:38:04 +00:00
|
|
|
|
2019-03-24 18:27:32 +00:00
|
|
|
except Exception: # pylint: disable=broad-except
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("Error connecting with deCONZ gateway")
|
2019-03-24 18:27:32 +00:00
|
|
|
return False
|
|
|
|
|
2018-10-31 21:38:04 +00:00
|
|
|
for component in SUPPORTED_PLATFORMS:
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.config_entry, component
|
|
|
|
)
|
|
|
|
)
|
2018-10-31 21:38:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self.listeners.append(
|
|
|
|
async_dispatcher_connect(
|
2019-09-05 23:38:00 +00:00
|
|
|
hass, self.async_signal_new_device(NEW_SENSOR), self.async_add_remote
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
)
|
2018-10-31 21:38:04 +00:00
|
|
|
|
|
|
|
self.async_add_remote(self.api.sensors.values())
|
|
|
|
|
|
|
|
self.api.start()
|
|
|
|
|
2019-09-05 23:38:00 +00:00
|
|
|
self.config_entry.add_update_listener(self.async_new_address)
|
|
|
|
self.config_entry.add_update_listener(self.async_options_updated)
|
2019-04-15 04:50:01 +00:00
|
|
|
|
2018-10-31 21:38:04 +00:00
|
|
|
return True
|
|
|
|
|
2019-04-15 04:50:01 +00:00
|
|
|
@staticmethod
|
2019-09-05 23:38:00 +00:00
|
|
|
async def async_new_address(hass, entry):
|
2019-04-15 04:50:01 +00:00
|
|
|
"""Handle signals of gateway getting new address.
|
|
|
|
|
|
|
|
This is a static method because a class method (bound method),
|
|
|
|
can not be used with weak references.
|
|
|
|
"""
|
2019-09-05 23:38:00 +00:00
|
|
|
gateway = get_gateway_from_config_entry(hass, entry)
|
|
|
|
if gateway.api.host != entry.data[CONF_HOST]:
|
|
|
|
gateway.api.close()
|
|
|
|
gateway.api.host = entry.data[CONF_HOST]
|
|
|
|
gateway.api.start()
|
2019-04-15 04:50:01 +00:00
|
|
|
|
2019-04-05 00:48:24 +00:00
|
|
|
@property
|
2019-09-05 23:38:00 +00:00
|
|
|
def signal_reachable(self):
|
2019-04-05 00:48:24 +00:00
|
|
|
"""Gateway specific event to signal a change in connection status."""
|
2019-09-05 23:38:00 +00:00
|
|
|
return f"deconz-reachable-{self.bridgeid}"
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2018-11-05 15:21:44 +00:00
|
|
|
@callback
|
|
|
|
def async_connection_status_callback(self, available):
|
|
|
|
"""Handle signals of gateway connection status."""
|
|
|
|
self.available = available
|
2019-09-05 23:38:00 +00:00
|
|
|
async_dispatcher_send(self.hass, self.signal_reachable, True)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def signal_options_update(self):
|
|
|
|
"""Event specific per deCONZ entry to signal new options."""
|
|
|
|
return f"deconz-options-{self.bridgeid}"
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def async_options_updated(hass, entry):
|
|
|
|
"""Triggered by config entry options updates."""
|
|
|
|
gateway = get_gateway_from_config_entry(hass, entry)
|
|
|
|
|
|
|
|
registry = await async_get_registry(hass)
|
|
|
|
async_dispatcher_send(hass, gateway.signal_options_update, registry)
|
2019-04-05 00:48:24 +00:00
|
|
|
|
|
|
|
@callback
|
2019-09-05 23:38:00 +00:00
|
|
|
def async_signal_new_device(self, device_type):
|
2019-04-05 00:48:24 +00:00
|
|
|
"""Gateway specific event to signal new device."""
|
|
|
|
return NEW_DEVICE[device_type].format(self.bridgeid)
|
2018-11-05 15:21:44 +00:00
|
|
|
|
2018-10-31 21:38:04 +00:00
|
|
|
@callback
|
|
|
|
def async_add_device_callback(self, device_type, device):
|
|
|
|
"""Handle event of new device creation in deCONZ."""
|
|
|
|
if not isinstance(device, list):
|
|
|
|
device = [device]
|
2019-04-05 00:48:24 +00:00
|
|
|
async_dispatcher_send(
|
2019-09-05 23:38:00 +00:00
|
|
|
self.hass, self.async_signal_new_device(device_type), device
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-10-31 21:38:04 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_add_remote(self, sensors):
|
|
|
|
"""Set up remote from deCONZ."""
|
|
|
|
for sensor in sensors:
|
2019-07-31 19:25:30 +00:00
|
|
|
if sensor.type in Switch.ZHATYPE and not (
|
2019-09-05 23:38:00 +00:00
|
|
|
not self.option_allow_clip_sensor and sensor.type.startswith("CLIP")
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2018-10-31 21:38:04 +00:00
|
|
|
self.events.append(DeconzEvent(self.hass, sensor))
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def shutdown(self, event):
|
|
|
|
"""Wrap the call to deconz.close.
|
|
|
|
|
|
|
|
Used as an argument to EventBus.async_listen_once.
|
|
|
|
"""
|
|
|
|
self.api.close()
|
|
|
|
|
|
|
|
async def async_reset(self):
|
|
|
|
"""Reset this gateway to default state.
|
|
|
|
|
|
|
|
Will cancel any scheduled setup retry and will unload
|
|
|
|
the config entry.
|
|
|
|
"""
|
2019-09-05 23:38:00 +00:00
|
|
|
self.api.async_connection_status_callback = None
|
2018-10-31 21:38:04 +00:00
|
|
|
self.api.close()
|
|
|
|
|
|
|
|
for component in SUPPORTED_PLATFORMS:
|
|
|
|
await self.hass.config_entries.async_forward_entry_unload(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.config_entry, component
|
|
|
|
)
|
2018-10-31 21:38:04 +00:00
|
|
|
|
|
|
|
for unsub_dispatcher in self.listeners:
|
|
|
|
unsub_dispatcher()
|
|
|
|
self.listeners = []
|
|
|
|
|
|
|
|
for event in self.events:
|
|
|
|
event.async_will_remove_from_hass()
|
|
|
|
self.events.remove(event)
|
|
|
|
|
|
|
|
self.deconz_ids = {}
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def get_gateway(
|
|
|
|
hass, config, async_add_device_callback, async_connection_status_callback
|
|
|
|
):
|
2018-10-31 21:38:04 +00:00
|
|
|
"""Create a gateway object and verify configuration."""
|
|
|
|
session = aiohttp_client.async_get_clientsession(hass)
|
2019-03-24 18:27:32 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
deconz = DeconzSession(
|
|
|
|
hass.loop,
|
|
|
|
session,
|
|
|
|
**config,
|
|
|
|
async_add_device=async_add_device_callback,
|
|
|
|
connection_status=async_connection_status_callback,
|
|
|
|
)
|
2019-03-24 18:27:32 +00:00
|
|
|
try:
|
|
|
|
with async_timeout.timeout(10):
|
|
|
|
await deconz.async_load_parameters()
|
2018-10-31 21:38:04 +00:00
|
|
|
return deconz
|
2019-03-24 18:27:32 +00:00
|
|
|
|
|
|
|
except errors.Unauthorized:
|
2019-03-26 06:43:58 +00:00
|
|
|
_LOGGER.warning("Invalid key for deCONZ at %s", config[CONF_HOST])
|
2019-03-24 18:27:32 +00:00
|
|
|
raise AuthenticationRequired
|
|
|
|
|
|
|
|
except (asyncio.TimeoutError, errors.RequestError):
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("Error connecting to deCONZ gateway at %s", config[CONF_HOST])
|
2019-03-24 18:27:32 +00:00
|
|
|
raise CannotConnect
|
2018-10-31 21:38:04 +00:00
|
|
|
|
|
|
|
|
2019-09-05 23:38:00 +00:00
|
|
|
class DeconzEntityHandler:
|
|
|
|
"""Platform entity handler to help with updating disabled by."""
|
|
|
|
|
|
|
|
def __init__(self, gateway):
|
|
|
|
"""Create an entity handler."""
|
|
|
|
self.gateway = gateway
|
|
|
|
self._entities = []
|
|
|
|
|
|
|
|
gateway.listeners.append(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
gateway.hass, gateway.signal_options_update, self.update_entity_registry
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def add_entity(self, entity):
|
|
|
|
"""Add a new entity to handler."""
|
|
|
|
self._entities.append(entity)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def update_entity_registry(self, entity_registry):
|
|
|
|
"""Update entity registry disabled by status."""
|
|
|
|
for entity in self._entities:
|
|
|
|
|
|
|
|
if entity.entity_registry_enabled_default != entity.enabled:
|
|
|
|
disabled_by = None
|
|
|
|
|
|
|
|
if entity.enabled:
|
|
|
|
disabled_by = DISABLED_CONFIG_ENTRY
|
|
|
|
|
|
|
|
entity_registry.async_update_entity(
|
|
|
|
entity.registry_entry.entity_id, disabled_by=disabled_by
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-10-31 21:38:04 +00:00
|
|
|
class DeconzEvent:
|
|
|
|
"""When you want signals instead of entities.
|
|
|
|
|
|
|
|
Stateless sensors such as remotes are expected to generate an event
|
|
|
|
instead of a sensor entity in hass.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, hass, device):
|
|
|
|
"""Register callback that will be used for signals."""
|
|
|
|
self._hass = hass
|
|
|
|
self._device = device
|
|
|
|
self._device.register_async_callback(self.async_update_callback)
|
2019-09-03 15:09:59 +00:00
|
|
|
self._event = f"deconz_{CONF_EVENT}"
|
2018-10-31 21:38:04 +00:00
|
|
|
self._id = slugify(self._device.name)
|
2019-02-17 15:58:46 +00:00
|
|
|
_LOGGER.debug("deCONZ event created: %s", self._id)
|
2018-10-31 21:38:04 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_will_remove_from_hass(self) -> None:
|
|
|
|
"""Disconnect event object when removed."""
|
|
|
|
self._device.remove_callback(self.async_update_callback)
|
|
|
|
self._device = None
|
|
|
|
|
|
|
|
@callback
|
2019-05-27 04:56:00 +00:00
|
|
|
def async_update_callback(self, force_update=False):
|
2018-10-31 21:38:04 +00:00
|
|
|
"""Fire the event if reason is that state is updated."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if "state" in self._device.changed_keys:
|
2018-10-31 21:38:04 +00:00
|
|
|
data = {CONF_ID: self._id, CONF_EVENT: self._device.state}
|
|
|
|
self._hass.bus.async_fire(self._event, data, EventOrigin.remote)
|