Split devices of nodes with multiple instances (#24032)

* Split devices of nodes with multiple instances

* Note entities to register with device registry

* Use EntityPlatform
This commit is contained in:
Penny Wood 2019-06-04 00:40:40 +08:00 committed by Paulus Schoutsen
parent 449a7d3fd5
commit b1dcfaf6b3
2 changed files with 38 additions and 12 deletions

View File

@ -11,7 +11,8 @@ from homeassistant import config_entries
from homeassistant.core import callback, CoreState from homeassistant.core import callback, CoreState
from homeassistant.helpers import discovery from homeassistant.helpers import discovery
from homeassistant.helpers.entity import generate_entity_id from homeassistant.helpers.entity import generate_entity_id
from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.entity_component import DEFAULT_SCAN_INTERVAL
from homeassistant.helpers.entity_platform import EntityPlatform
from homeassistant.helpers.entity_registry import async_get_registry from homeassistant.helpers.entity_registry import async_get_registry
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP) ATTR_ENTITY_ID, EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
@ -291,6 +292,8 @@ async def async_setup_entry(hass, config_entry):
hass.data[DATA_DEVICES] = {} hass.data[DATA_DEVICES] = {}
hass.data[DATA_ENTITY_VALUES] = [] hass.data[DATA_ENTITY_VALUES] = []
registry = await async_get_registry(hass)
if use_debug: # pragma: no cover if use_debug: # pragma: no cover
def log_all(signal, value=None): def log_all(signal, value=None):
"""Log all the signals.""" """Log all the signals."""
@ -332,14 +335,23 @@ async def async_setup_entry(hass, config_entry):
new_values = hass.data[DATA_ENTITY_VALUES] + [values] new_values = hass.data[DATA_ENTITY_VALUES] + [values]
hass.data[DATA_ENTITY_VALUES] = new_values hass.data[DATA_ENTITY_VALUES] = new_values
component = EntityComponent(_LOGGER, DOMAIN, hass) platform = EntityPlatform(
registry = await async_get_registry(hass) hass=hass,
logger=_LOGGER,
domain=DOMAIN,
platform_name=DOMAIN,
platform=None,
scan_interval=DEFAULT_SCAN_INTERVAL,
entity_namespace=None,
async_entities_added_callback=lambda: None,
)
platform.config_entry = config_entry
def node_added(node): def node_added(node):
"""Handle a new node on the network.""" """Handle a new node on the network."""
entity = ZWaveNodeEntity(node, network) entity = ZWaveNodeEntity(node, network)
def _add_node_to_component(): async def _add_node_to_component():
if hass.data[DATA_DEVICES].get(entity.unique_id): if hass.data[DATA_DEVICES].get(entity.unique_id):
return return
@ -353,10 +365,10 @@ async def async_setup_entry(hass, config_entry):
return return
hass.data[DATA_DEVICES][entity.unique_id] = entity hass.data[DATA_DEVICES][entity.unique_id] = entity
component.add_entities([entity]) await platform.async_add_entities([entity])
if entity.unique_id: if entity.unique_id:
_add_node_to_component() hass.async_add_job(_add_node_to_component())
return return
@callback @callback
@ -1057,14 +1069,25 @@ class ZWaveDeviceEntity(ZWaveBaseEntity):
@property @property
def device_info(self): def device_info(self):
"""Return device information.""" """Return device information."""
return { info = {
'identifiers': {
(DOMAIN, self.node_id)
},
'manufacturer': self.node.manufacturer_name, 'manufacturer': self.node.manufacturer_name,
'model': self.node.product_name, 'model': self.node.product_name,
'name': node_name(self.node),
} }
if self.values.primary.instance > 1:
info['name'] = '{} ({})'.format(
node_name(self.node), self.values.primary.instance)
info['identifiers'] = {
(DOMAIN, self.node_id, self.values.primary.instance, ),
}
info['via_hub'] = (DOMAIN, self.node_id, )
else:
info['name'] = node_name(self.node)
info['identifiers'] = {
(DOMAIN, self.node_id),
}
if self.node_id > 1:
info['via_hub'] = (DOMAIN, 1, )
return info
@property @property
def name(self): def name(self):

View File

@ -124,7 +124,7 @@ class ZWaveNodeEntity(ZWaveBaseEntity):
@property @property
def device_info(self): def device_info(self):
"""Return device information.""" """Return device information."""
return { info = {
'identifiers': { 'identifiers': {
(DOMAIN, self.node_id) (DOMAIN, self.node_id)
}, },
@ -132,6 +132,9 @@ class ZWaveNodeEntity(ZWaveBaseEntity):
'model': self.node.product_name, 'model': self.node.product_name,
'name': node_name(self.node) 'name': node_name(self.node)
} }
if self.node_id > 1:
info['via_hub'] = (DOMAIN, 1)
return info
def network_node_changed(self, node=None, value=None, args=None): def network_node_changed(self, node=None, value=None, args=None):
"""Handle a changed node on the network.""" """Handle a changed node on the network."""