From 1ae58ce48b81565745dec960d80185968c01715e Mon Sep 17 00:00:00 2001 From: damarco Date: Sat, 1 Dec 2018 10:31:49 +0100 Subject: [PATCH] Add support for zha device registry (#18755) --- homeassistant/components/zha/__init__.py | 14 ++++++++++++++ homeassistant/components/zha/entities/entity.py | 17 +++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/homeassistant/components/zha/__init__.py b/homeassistant/components/zha/__init__.py index 0fc2b978fbb..d67fbd02b8f 100644 --- a/homeassistant/components/zha/__init__.py +++ b/homeassistant/components/zha/__init__.py @@ -15,6 +15,7 @@ from homeassistant.helpers.entity_component import EntityComponent from homeassistant.components.zha.entities import ZhaDeviceEntity from homeassistant import config_entries, const as ha_const from homeassistant.helpers.dispatcher import async_dispatcher_send +from homeassistant.helpers.device_registry import CONNECTION_ZIGBEE from . import const as zha_const # Loading the config flow file will register the flow @@ -116,10 +117,12 @@ async def async_setup_entry(hass, config_entry): import bellows.ezsp from bellows.zigbee.application import ControllerApplication radio = bellows.ezsp.EZSP() + radio_description = "EZSP" elif radio_type == RadioType.xbee.name: import zigpy_xbee.api from zigpy_xbee.zigbee.application import ControllerApplication radio = zigpy_xbee.api.XBee() + radio_description = "XBee" await radio.connect(usb_path, baudrate) hass.data[DATA_ZHA][DATA_ZHA_RADIO] = radio @@ -137,6 +140,17 @@ async def async_setup_entry(hass, config_entry): hass.async_create_task( listener.async_device_initialized(device, False)) + device_registry = await \ + hass.helpers.device_registry.async_get_registry() + device_registry.async_get_or_create( + config_entry_id=config_entry.entry_id, + connections={(CONNECTION_ZIGBEE, str(APPLICATION_CONTROLLER.ieee))}, + identifiers={(DOMAIN, str(APPLICATION_CONTROLLER.ieee))}, + name="Zigbee Coordinator", + manufacturer="ZHA", + model=radio_description, + ) + hass.data[DATA_ZHA][DATA_ZHA_BRIDGE_ID] = str(APPLICATION_CONTROLLER.ieee) for component in COMPONENTS: diff --git a/homeassistant/components/zha/entities/entity.py b/homeassistant/components/zha/entities/entity.py index a16f29f447a..a4454244364 100644 --- a/homeassistant/components/zha/entities/entity.py +++ b/homeassistant/components/zha/entities/entity.py @@ -7,6 +7,10 @@ https://home-assistant.io/components/zha/ from homeassistant.helpers import entity from homeassistant.util import slugify from homeassistant.core import callback +from homeassistant.helpers.device_registry import CONNECTION_ZIGBEE +from homeassistant.components.zha.const import ( + DOMAIN, DATA_ZHA, DATA_ZHA_BRIDGE_ID +) class ZhaEntity(entity.Entity): @@ -87,3 +91,16 @@ class ZhaEntity(entity.Entity): def zdo_command(self, tsn, command_id, args): """Handle a ZDO command received on this cluster.""" pass + + @property + def device_info(self): + """Return a device description for device registry.""" + ieee = str(self._endpoint.device.ieee) + return { + 'connections': {(CONNECTION_ZIGBEE, ieee)}, + 'identifiers': {(DOMAIN, ieee)}, + 'manufacturer': self._endpoint.manufacturer, + 'model': self._endpoint.model, + 'name': self._device_state_attributes['friendly_name'], + 'via_hub': (DOMAIN, self.hass.data[DATA_ZHA][DATA_ZHA_BRIDGE_ID]), + }