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,
|
|
|
|
)
|
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,
|
|
|
|
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
|
|
|
|
def allow_clip_sensor(self) -> bool:
|
|
|
|
"""Allow loading clip sensor from gateway."""
|
2019-08-09 17:31:58 +00:00
|
|
|
return self.config_entry.options.get(CONF_ALLOW_CLIP_SENSOR, True)
|
2019-04-05 00:48:24 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def allow_deconz_groups(self) -> bool:
|
|
|
|
"""Allow loading deCONZ groups from gateway."""
|
2019-08-09 17:31:58 +00:00
|
|
|
return self.config_entry.options.get(CONF_ALLOW_DECONZ_GROUPS, True)
|
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(
|
|
|
|
hass, self.async_event_new_device(NEW_SENSOR), self.async_add_remote
|
|
|
|
)
|
|
|
|
)
|
2018-10-31 21:38:04 +00:00
|
|
|
|
|
|
|
self.async_add_remote(self.api.sensors.values())
|
|
|
|
|
|
|
|
self.api.start()
|
|
|
|
|
2019-04-15 04:50:01 +00:00
|
|
|
self.config_entry.add_update_listener(self.async_new_address_callback)
|
|
|
|
|
2018-10-31 21:38:04 +00:00
|
|
|
return True
|
|
|
|
|
2019-04-15 04:50:01 +00:00
|
|
|
@staticmethod
|
|
|
|
async def async_new_address_callback(hass, entry):
|
|
|
|
"""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.
|
|
|
|
"""
|
|
|
|
gateway = hass.data[DOMAIN][entry.data[CONF_BRIDGEID]]
|
|
|
|
gateway.api.close()
|
|
|
|
gateway.api.host = entry.data[CONF_HOST]
|
|
|
|
gateway.api.start()
|
|
|
|
|
2019-04-05 00:48:24 +00:00
|
|
|
@property
|
|
|
|
def event_reachable(self):
|
|
|
|
"""Gateway specific event to signal a change in connection status."""
|
2019-09-03 15:09:59 +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-05-27 04:56:00 +00:00
|
|
|
async_dispatcher_send(self.hass, self.event_reachable, True)
|
2019-04-05 00:48:24 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_event_new_device(self, device_type):
|
|
|
|
"""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-07-31 19:25:30 +00:00
|
|
|
self.hass, self.async_event_new_device(device_type), device
|
|
|
|
)
|
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 (
|
|
|
|
not self.allow_clip_sensor and sensor.type.startswith("CLIP")
|
|
|
|
):
|
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.
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
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)
|