2018-10-31 21:38:04 +00:00
|
|
|
"""Representation of a deCONZ gateway."""
|
2019-03-24 18:27:32 +00:00
|
|
|
import asyncio
|
|
|
|
|
2019-12-05 05:17:18 +00:00
|
|
|
import async_timeout
|
2019-05-27 04:56:00 +00:00
|
|
|
from pydeconz import DeconzSession, errors
|
|
|
|
|
2019-12-08 15:53:34 +00:00
|
|
|
from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PORT
|
2019-09-10 23:56:28 +00:00
|
|
|
from homeassistant.core import callback
|
2019-12-05 05:17:18 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2018-10-31 21:38:04 +00:00
|
|
|
from homeassistant.helpers import aiohttp_client
|
2019-04-05 00:48:24 +00:00
|
|
|
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
|
2020-02-05 00:37:01 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
2018-10-31 21:38:04 +00:00
|
|
|
|
|
|
|
from .const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_ALLOW_CLIP_SENSOR,
|
|
|
|
CONF_ALLOW_DECONZ_GROUPS,
|
|
|
|
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,
|
2020-02-01 19:02:57 +00:00
|
|
|
LOGGER,
|
2020-02-18 21:24:25 +00:00
|
|
|
NEW_GROUP,
|
2020-02-24 16:47:52 +00:00
|
|
|
NEW_LIGHT,
|
|
|
|
NEW_SCENE,
|
2020-02-18 21:24:25 +00:00
|
|
|
NEW_SENSOR,
|
2019-07-31 19:25:30 +00:00
|
|
|
SUPPORTED_PLATFORMS,
|
|
|
|
)
|
2020-09-22 17:55:10 +00:00
|
|
|
from .deconz_event import async_setup_events, async_unload_events
|
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."""
|
2020-03-31 16:19:34 +00:00
|
|
|
return hass.data[DOMAIN].get(config_entry.unique_id)
|
2019-04-05 00:48:24 +00:00
|
|
|
|
|
|
|
|
2018-10-31 21:38:04 +00:00
|
|
|
class DeconzGateway:
|
|
|
|
"""Manages a single deCONZ gateway."""
|
|
|
|
|
2019-12-08 15:53:34 +00:00
|
|
|
def __init__(self, hass, config_entry) -> None:
|
2018-10-31 21:38:04 +00:00
|
|
|
"""Initialize the system."""
|
|
|
|
self.hass = hass
|
|
|
|
self.config_entry = config_entry
|
2019-12-08 15:53:34 +00:00
|
|
|
|
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 = []
|
|
|
|
|
2020-09-25 20:49:28 +00:00
|
|
|
self.entities = {}
|
|
|
|
|
2020-02-18 21:24:25 +00:00
|
|
|
self._current_option_allow_clip_sensor = self.option_allow_clip_sensor
|
|
|
|
self._current_option_allow_deconz_groups = self.option_allow_deconz_groups
|
|
|
|
|
2019-04-05 00:48:24 +00:00
|
|
|
@property
|
|
|
|
def bridgeid(self) -> str:
|
|
|
|
"""Return the unique identifier of the gateway."""
|
2020-01-03 10:50:53 +00:00
|
|
|
return self.config_entry.unique_id
|
2019-04-05 00:48:24 +00:00
|
|
|
|
|
|
|
@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
|
|
|
|
2019-12-08 15:53:34 +00:00
|
|
|
async def async_update_device_registry(self) -> None:
|
2019-04-05 00:48:24 +00:00
|
|
|
"""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-12-08 15:53:34 +00:00
|
|
|
async def async_setup(self) -> bool:
|
2018-10-31 21:38:04 +00:00
|
|
|
"""Set up a deCONZ gateway."""
|
2019-03-24 18:27:32 +00:00
|
|
|
try:
|
|
|
|
self.api = await get_gateway(
|
2020-01-03 10:50:53 +00:00
|
|
|
self.hass,
|
2019-07-31 19:25:30 +00:00
|
|
|
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
|
|
|
|
2020-08-28 11:50:32 +00:00
|
|
|
except CannotConnect as err:
|
|
|
|
raise ConfigEntryNotReady from err
|
2018-10-31 21:38:04 +00:00
|
|
|
|
2019-12-08 15:53:34 +00:00
|
|
|
except Exception as err: # pylint: disable=broad-except
|
2020-02-01 19:02:57 +00:00
|
|
|
LOGGER.error("Error connecting with deCONZ gateway: %s", err)
|
2019-03-24 18:27:32 +00:00
|
|
|
return False
|
|
|
|
|
2018-10-31 21:38:04 +00:00
|
|
|
for component in SUPPORTED_PLATFORMS:
|
2020-01-03 10:50:53 +00:00
|
|
|
self.hass.async_create_task(
|
|
|
|
self.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
|
|
|
|
2020-09-22 17:55:10 +00:00
|
|
|
self.hass.async_create_task(async_setup_events(self))
|
|
|
|
|
2018-10-31 21:38:04 +00:00
|
|
|
self.api.start()
|
|
|
|
|
2020-02-18 21:24:25 +00:00
|
|
|
self.config_entry.add_update_listener(self.async_config_entry_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
|
2020-02-18 21:24:25 +00:00
|
|
|
async def async_config_entry_updated(hass, entry) -> None:
|
|
|
|
"""Handle signals of config entry being updated.
|
2019-04-15 04:50:01 +00:00
|
|
|
|
2020-02-18 21:24:25 +00:00
|
|
|
This is a static method because a class method (bound method), can not be used with weak references.
|
|
|
|
Causes for this is either discovery updating host address or config entry options changing.
|
2019-04-15 04:50:01 +00:00
|
|
|
"""
|
2019-09-05 23:38:00 +00:00
|
|
|
gateway = get_gateway_from_config_entry(hass, entry)
|
2020-03-31 16:19:34 +00:00
|
|
|
if not gateway:
|
|
|
|
return
|
2019-09-05 23:38:00 +00:00
|
|
|
if gateway.api.host != entry.data[CONF_HOST]:
|
|
|
|
gateway.api.close()
|
|
|
|
gateway.api.host = entry.data[CONF_HOST]
|
|
|
|
gateway.api.start()
|
2020-02-18 21:24:25 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
await gateway.options_updated()
|
|
|
|
|
|
|
|
async def options_updated(self):
|
|
|
|
"""Manage entities affected by config entry options."""
|
|
|
|
deconz_ids = []
|
|
|
|
|
|
|
|
if self._current_option_allow_clip_sensor != self.option_allow_clip_sensor:
|
|
|
|
self._current_option_allow_clip_sensor = self.option_allow_clip_sensor
|
|
|
|
|
|
|
|
sensors = [
|
|
|
|
sensor
|
|
|
|
for sensor in self.api.sensors.values()
|
|
|
|
if sensor.type.startswith("CLIP")
|
|
|
|
]
|
|
|
|
|
|
|
|
if self.option_allow_clip_sensor:
|
|
|
|
self.async_add_device_callback(NEW_SENSOR, sensors)
|
|
|
|
else:
|
|
|
|
deconz_ids += [sensor.deconz_id for sensor in sensors]
|
|
|
|
|
|
|
|
if self._current_option_allow_deconz_groups != self.option_allow_deconz_groups:
|
|
|
|
self._current_option_allow_deconz_groups = self.option_allow_deconz_groups
|
|
|
|
|
|
|
|
groups = list(self.api.groups.values())
|
|
|
|
|
|
|
|
if self.option_allow_deconz_groups:
|
|
|
|
self.async_add_device_callback(NEW_GROUP, groups)
|
|
|
|
else:
|
|
|
|
deconz_ids += [group.deconz_id for group in groups]
|
|
|
|
|
|
|
|
entity_registry = await self.hass.helpers.entity_registry.async_get_registry()
|
|
|
|
|
|
|
|
for entity_id, deconz_id in self.deconz_ids.items():
|
|
|
|
if deconz_id in deconz_ids and entity_registry.async_is_registered(
|
|
|
|
entity_id
|
|
|
|
):
|
2020-08-19 12:57:38 +00:00
|
|
|
# Removing an entity from the entity registry will also remove them
|
|
|
|
# from Home Assistant
|
2020-02-18 21:24:25 +00:00
|
|
|
entity_registry.async_remove(entity_id)
|
2019-04-15 04:50:01 +00:00
|
|
|
|
2019-04-05 00:48:24 +00:00
|
|
|
@property
|
2019-12-08 15:53:34 +00:00
|
|
|
def signal_reachable(self) -> str:
|
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
|
2019-12-08 15:53:34 +00:00
|
|
|
def async_connection_status_callback(self, available) -> None:
|
2018-11-05 15:21:44 +00:00
|
|
|
"""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)
|
|
|
|
|
2019-04-05 00:48:24 +00:00
|
|
|
@callback
|
2019-12-08 15:53:34 +00:00
|
|
|
def async_signal_new_device(self, device_type) -> str:
|
2019-04-05 00:48:24 +00:00
|
|
|
"""Gateway specific event to signal new device."""
|
2020-02-24 16:47:52 +00:00
|
|
|
new_device = {
|
|
|
|
NEW_GROUP: f"deconz_new_group_{self.bridgeid}",
|
|
|
|
NEW_LIGHT: f"deconz_new_light_{self.bridgeid}",
|
|
|
|
NEW_SCENE: f"deconz_new_scene_{self.bridgeid}",
|
|
|
|
NEW_SENSOR: f"deconz_new_sensor_{self.bridgeid}",
|
|
|
|
}
|
|
|
|
return new_device[device_type]
|
2018-11-05 15:21:44 +00:00
|
|
|
|
2018-10-31 21:38:04 +00:00
|
|
|
@callback
|
2019-12-08 15:53:34 +00:00
|
|
|
def async_add_device_callback(self, device_type, device) -> None:
|
2018-10-31 21:38:04 +00:00
|
|
|
"""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
|
2019-12-08 15:53:34 +00:00
|
|
|
def shutdown(self, event) -> None:
|
2018-10-31 21:38:04 +00:00
|
|
|
"""Wrap the call to deconz.close.
|
|
|
|
|
|
|
|
Used as an argument to EventBus.async_listen_once.
|
|
|
|
"""
|
|
|
|
self.api.close()
|
|
|
|
|
|
|
|
async def async_reset(self):
|
2019-09-19 21:44:09 +00:00
|
|
|
"""Reset this gateway to default state."""
|
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 = []
|
|
|
|
|
2020-09-27 09:02:45 +00:00
|
|
|
async_unload_events(self)
|
2018-10-31 21:38:04 +00:00
|
|
|
|
|
|
|
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
|
2019-12-08 15:53:34 +00:00
|
|
|
) -> DeconzSession:
|
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(
|
|
|
|
session,
|
2019-12-08 15:53:34 +00:00
|
|
|
config[CONF_HOST],
|
|
|
|
config[CONF_PORT],
|
|
|
|
config[CONF_API_KEY],
|
2019-07-31 19:25:30 +00:00
|
|
|
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):
|
2019-12-08 15:53:34 +00:00
|
|
|
await deconz.initialize()
|
2018-10-31 21:38:04 +00:00
|
|
|
return deconz
|
2019-03-24 18:27:32 +00:00
|
|
|
|
2020-08-28 11:50:32 +00:00
|
|
|
except errors.Unauthorized as err:
|
2020-02-01 19:02:57 +00:00
|
|
|
LOGGER.warning("Invalid key for deCONZ at %s", config[CONF_HOST])
|
2020-08-28 11:50:32 +00:00
|
|
|
raise AuthenticationRequired from err
|
2019-03-24 18:27:32 +00:00
|
|
|
|
2020-08-28 11:50:32 +00:00
|
|
|
except (asyncio.TimeoutError, errors.RequestError) as err:
|
2020-02-01 19:02:57 +00:00
|
|
|
LOGGER.error("Error connecting to deCONZ gateway at %s", config[CONF_HOST])
|
2020-08-28 11:50:32 +00:00
|
|
|
raise CannotConnect from err
|