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
|
2021-10-07 10:48:27 +00:00
|
|
|
from pydeconz import DeconzSession, errors, group, light, sensor
|
2019-05-27 04:56:00 +00:00
|
|
|
|
2021-10-22 17:41:49 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_HASSIO
|
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
|
2021-04-10 05:41:29 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
2021-10-20 09:16:28 +00:00
|
|
|
from homeassistant.helpers import (
|
|
|
|
aiohttp_client,
|
|
|
|
device_registry as dr,
|
|
|
|
entity_registry as er,
|
|
|
|
)
|
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,
|
2020-09-29 09:07:19 +00:00
|
|
|
CONF_ALLOW_NEW_DEVICES,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_MASTER_GATEWAY,
|
2019-09-05 23:38:00 +00:00
|
|
|
DEFAULT_ALLOW_CLIP_SENSOR,
|
|
|
|
DEFAULT_ALLOW_DECONZ_GROUPS,
|
2020-09-29 09:07:19 +00:00
|
|
|
DEFAULT_ALLOW_NEW_DEVICES,
|
2021-01-22 22:37:16 +00:00
|
|
|
DOMAIN as DECONZ_DOMAIN,
|
2020-02-01 19:02:57 +00:00
|
|
|
LOGGER,
|
2021-03-02 20:43:59 +00:00
|
|
|
PLATFORMS,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
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):
|
2021-07-06 15:18:54 +00:00
|
|
|
"""Return gateway with a matching config entry ID."""
|
|
|
|
return hass.data[DECONZ_DOMAIN][config_entry.entry_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-10-31 21:38:04 +00:00
|
|
|
self.api = None
|
2020-09-30 15:24:30 +00:00
|
|
|
|
|
|
|
self.available = True
|
|
|
|
self.ignore_state_updates = False
|
|
|
|
|
2021-10-07 10:48:27 +00:00
|
|
|
self.signal_reachable = f"deconz-reachable-{config_entry.entry_id}"
|
|
|
|
|
|
|
|
self.signal_new_group = f"deconz_new_group_{config_entry.entry_id}"
|
|
|
|
self.signal_new_light = f"deconz_new_light_{config_entry.entry_id}"
|
|
|
|
self.signal_new_scene = f"deconz_new_scene_{config_entry.entry_id}"
|
|
|
|
self.signal_new_sensor = f"deconz_new_sensor_{config_entry.entry_id}"
|
|
|
|
|
|
|
|
self.deconz_resource_type_to_signal_new_device = {
|
|
|
|
group.RESOURCE_TYPE: self.signal_new_group,
|
|
|
|
light.RESOURCE_TYPE: self.signal_new_light,
|
|
|
|
group.RESOURCE_TYPE_SCENE: self.signal_new_scene,
|
|
|
|
sensor.RESOURCE_TYPE: self.signal_new_sensor,
|
|
|
|
}
|
|
|
|
|
2018-10-31 21:38:04 +00:00
|
|
|
self.deconz_ids = {}
|
2020-09-30 15:24:30 +00:00
|
|
|
self.entities = {}
|
2018-10-31 21:38:04 +00:00
|
|
|
self.events = []
|
|
|
|
|
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
|
|
|
|
2020-09-30 15:24:30 +00:00
|
|
|
@property
|
|
|
|
def host(self) -> str:
|
|
|
|
"""Return the host of the gateway."""
|
|
|
|
return self.config_entry.data[CONF_HOST]
|
|
|
|
|
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]
|
|
|
|
|
2020-09-30 15:24:30 +00:00
|
|
|
# Options
|
|
|
|
|
2019-04-05 00:48:24 +00:00
|
|
|
@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
|
|
|
|
2020-09-29 09:07:19 +00:00
|
|
|
@property
|
|
|
|
def option_allow_new_devices(self) -> bool:
|
|
|
|
"""Allow automatic adding of new devices."""
|
|
|
|
return self.config_entry.options.get(
|
|
|
|
CONF_ALLOW_NEW_DEVICES, DEFAULT_ALLOW_NEW_DEVICES
|
|
|
|
)
|
|
|
|
|
2020-09-30 15:24:30 +00:00
|
|
|
# Callbacks
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_connection_status_callback(self, available) -> None:
|
|
|
|
"""Handle signals of gateway connection status."""
|
|
|
|
self.available = available
|
|
|
|
self.ignore_state_updates = False
|
2021-10-20 12:59:36 +00:00
|
|
|
async_dispatcher_send(self.hass, self.signal_reachable)
|
2020-09-30 15:24:30 +00:00
|
|
|
|
|
|
|
@callback
|
2020-12-05 10:53:43 +00:00
|
|
|
def async_add_device_callback(
|
2021-10-07 10:48:27 +00:00
|
|
|
self, resource_type, device=None, force: bool = False
|
2020-12-05 10:53:43 +00:00
|
|
|
) -> None:
|
2020-09-30 15:24:30 +00:00
|
|
|
"""Handle event of new device creation in deCONZ."""
|
2021-10-07 10:48:27 +00:00
|
|
|
if (
|
|
|
|
not force
|
|
|
|
and not self.option_allow_new_devices
|
|
|
|
or resource_type not in self.deconz_resource_type_to_signal_new_device
|
|
|
|
):
|
2020-09-30 15:24:30 +00:00
|
|
|
return
|
|
|
|
|
2020-12-02 15:21:27 +00:00
|
|
|
args = []
|
|
|
|
|
|
|
|
if device is not None and not isinstance(device, list):
|
|
|
|
args.append([device])
|
2020-09-30 15:24:30 +00:00
|
|
|
|
|
|
|
async_dispatcher_send(
|
2020-12-02 15:21:27 +00:00
|
|
|
self.hass,
|
2021-10-07 10:48:27 +00:00
|
|
|
self.deconz_resource_type_to_signal_new_device[resource_type],
|
2020-12-02 15:21:27 +00:00
|
|
|
*args, # Don't send device if None, it would override default value in listeners
|
2020-09-30 15:24:30 +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."""
|
2021-10-20 09:16:28 +00:00
|
|
|
device_registry = dr.async_get(self.hass)
|
2020-10-17 16:44:23 +00:00
|
|
|
|
|
|
|
# Host device
|
|
|
|
device_registry.async_get_or_create(
|
2019-04-05 00:48:24 +00:00
|
|
|
config_entry_id=self.config_entry.entry_id,
|
|
|
|
connections={(CONNECTION_NETWORK_MAC, self.api.config.mac)},
|
2020-10-17 16:44:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Gateway service
|
2021-10-22 17:41:49 +00:00
|
|
|
configuration_url = f"http://{self.host}:{self.config_entry.data[CONF_PORT]}"
|
|
|
|
if self.config_entry.source == SOURCE_HASSIO:
|
2021-11-14 10:49:02 +00:00
|
|
|
configuration_url = "homeassistant://hassio/ingress/core_deconz"
|
2020-10-17 16:44:23 +00:00
|
|
|
device_registry.async_get_or_create(
|
|
|
|
config_entry_id=self.config_entry.entry_id,
|
2021-10-22 17:41:49 +00:00
|
|
|
configuration_url=configuration_url,
|
2021-11-23 09:04:33 +00:00
|
|
|
entry_type=dr.DeviceEntryType.SERVICE,
|
2021-09-18 07:05:08 +00:00
|
|
|
identifiers={(DECONZ_DOMAIN, self.api.config.bridge_id)},
|
2019-07-31 19:25:30 +00:00
|
|
|
manufacturer="Dresden Elektronik",
|
2021-09-18 07:05:08 +00:00
|
|
|
model=self.api.config.model_id,
|
2019-04-05 00:48:24 +00:00
|
|
|
name=self.api.config.name,
|
2021-09-18 07:05:08 +00:00
|
|
|
sw_version=self.api.config.software_version,
|
2020-10-17 16:44:23 +00:00
|
|
|
via_device=(CONNECTION_NETWORK_MAC, self.api.config.mac),
|
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
|
|
|
|
2021-04-10 05:41:29 +00:00
|
|
|
except AuthenticationRequired as err:
|
|
|
|
raise ConfigEntryAuthFailed from err
|
2019-03-24 18:27:32 +00:00
|
|
|
|
2021-04-27 06:46:49 +00:00
|
|
|
self.hass.config_entries.async_setup_platforms(self.config_entry, PLATFORMS)
|
2018-10-31 21:38:04 +00:00
|
|
|
|
2020-12-02 15:21:27 +00:00
|
|
|
await async_setup_events(self)
|
2020-09-22 17:55:10 +00:00
|
|
|
|
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-09-30 15:24:30 +00:00
|
|
|
|
|
|
|
if gateway.api.host != gateway.host:
|
2019-09-05 23:38:00 +00:00
|
|
|
gateway.api.close()
|
2020-09-30 15:24:30 +00:00
|
|
|
gateway.api.host = gateway.host
|
2019-09-05 23:38:00 +00:00
|
|
|
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 = []
|
|
|
|
|
2020-12-02 15:21:27 +00:00
|
|
|
if self.option_allow_clip_sensor:
|
2021-10-07 10:48:27 +00:00
|
|
|
self.async_add_device_callback(sensor.RESOURCE_TYPE)
|
2020-02-18 21:24:25 +00:00
|
|
|
|
2020-12-02 15:21:27 +00:00
|
|
|
else:
|
|
|
|
deconz_ids += [
|
|
|
|
sensor.deconz_id
|
2020-02-18 21:24:25 +00:00
|
|
|
for sensor in self.api.sensors.values()
|
|
|
|
if sensor.type.startswith("CLIP")
|
|
|
|
]
|
|
|
|
|
2020-12-02 15:21:27 +00:00
|
|
|
if self.option_allow_deconz_groups:
|
2021-10-07 10:48:27 +00:00
|
|
|
self.async_add_device_callback(group.RESOURCE_TYPE)
|
2020-02-18 21:24:25 +00:00
|
|
|
|
2020-12-02 15:21:27 +00:00
|
|
|
else:
|
|
|
|
deconz_ids += [group.deconz_id for group in self.api.groups.values()]
|
2020-02-18 21:24:25 +00:00
|
|
|
|
2021-10-20 09:16:28 +00:00
|
|
|
entity_registry = er.async_get(self.hass)
|
2020-02-18 21:24:25 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
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()
|
|
|
|
|
2021-04-27 06:46:49 +00:00
|
|
|
await self.hass.config_entries.async_unload_platforms(
|
|
|
|
self.config_entry, PLATFORMS
|
|
|
|
)
|
2018-10-31 21:38:04 +00:00
|
|
|
|
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],
|
2021-09-18 07:05:08 +00:00
|
|
|
add_device=async_add_device_callback,
|
2019-07-31 19:25:30 +00:00
|
|
|
connection_status=async_connection_status_callback,
|
|
|
|
)
|
2019-03-24 18:27:32 +00:00
|
|
|
try:
|
2021-11-04 15:07:50 +00:00
|
|
|
async with async_timeout.timeout(10):
|
2021-09-18 07:05:08 +00:00
|
|
|
await deconz.refresh_state()
|
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
|