Changes as per code review of #24646 (#24917)

This commit is contained in:
Penny Wood 2019-07-07 13:36:57 +08:00 committed by Anders Melchiorsen
parent e8a5306c23
commit adbec5bffc
3 changed files with 34 additions and 28 deletions

View File

@ -35,8 +35,9 @@ from .const import (
from .node_entity import ZWaveBaseEntity, ZWaveNodeEntity from .node_entity import ZWaveBaseEntity, ZWaveNodeEntity
from . import workaround from . import workaround
from .discovery_schemas import DISCOVERY_SCHEMAS from .discovery_schemas import DISCOVERY_SCHEMAS
from .util import (check_node_schema, check_value_schema, node_name, from .util import (
check_has_unique_id, is_node_parsed) check_node_schema, check_value_schema, node_name, check_has_unique_id,
is_node_parsed, node_device_id_and_name)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -492,8 +493,7 @@ async def async_setup_entry(hass, config_entry):
if hass.state == CoreState.running: if hass.state == CoreState.running:
hass.bus.fire(const.EVENT_NETWORK_STOP) hass.bus.fire(const.EVENT_NETWORK_STOP)
@callback async def rename_node(service):
def rename_node(service):
"""Rename a node.""" """Rename a node."""
node_id = service.data.get(const.ATTR_NODE_ID) node_id = service.data.get(const.ATTR_NODE_ID)
node = network.nodes[node_id] node = network.nodes[node_id]
@ -506,15 +506,14 @@ async def async_setup_entry(hass, config_entry):
# and all the contained entities # and all the contained entities
node_key = 'node-{}'.format(node_id) node_key = 'node-{}'.format(node_id)
entity = hass.data[DATA_DEVICES][node_key] entity = hass.data[DATA_DEVICES][node_key]
hass.async_create_task(entity.node_renamed(update_ids)) await entity.node_renamed(update_ids)
for key in list(hass.data[DATA_DEVICES]): for key in list(hass.data[DATA_DEVICES]):
if not key.startswith('{}-'.format(node_id)): if not key.startswith('{}-'.format(node_id)):
continue continue
entity = hass.data[DATA_DEVICES][key] entity = hass.data[DATA_DEVICES][key]
hass.async_create_task(entity.value_renamed(update_ids)) await entity.value_renamed(update_ids)
@callback async def rename_value(service):
def rename_value(service):
"""Rename a node value.""" """Rename a node value."""
node_id = service.data.get(const.ATTR_NODE_ID) node_id = service.data.get(const.ATTR_NODE_ID)
value_id = service.data.get(const.ATTR_VALUE_ID) value_id = service.data.get(const.ATTR_VALUE_ID)
@ -528,7 +527,7 @@ async def async_setup_entry(hass, config_entry):
update_ids = service.data.get(const.ATTR_UPDATE_IDS) update_ids = service.data.get(const.ATTR_UPDATE_IDS)
value_key = '{}-{}'.format(node_id, value_id) value_key = '{}-{}'.format(node_id, value_id)
entity = hass.data[DATA_DEVICES][value_key] entity = hass.data[DATA_DEVICES][value_key]
hass.async_create_task(entity.value_renamed(update_ids)) await entity.value_renamed(update_ids)
def set_poll_intensity(service): def set_poll_intensity(service):
"""Set the polling intensity of a node value.""" """Set the polling intensity of a node value."""
@ -1069,6 +1068,7 @@ class ZWaveDeviceEntity(ZWaveBaseEntity):
ent_reg.async_update_entity( ent_reg.async_update_entity(
self.entity_id, new_entity_id=new_entity_id) self.entity_id, new_entity_id=new_entity_id)
return return
# else for the above two ifs, update if not using update_entity
self.async_schedule_update_ha_state() self.async_schedule_update_ha_state()
async def async_added_to_hass(self): async def async_added_to_hass(self):
@ -1110,24 +1110,20 @@ class ZWaveDeviceEntity(ZWaveBaseEntity):
@property @property
def device_info(self): def device_info(self):
"""Return device information.""" """Return device information."""
identifier, name = node_device_id_and_name(
self.node, self.values.primary.instance)
info = { info = {
'name': name,
'identifiers': {
identifier
},
'manufacturer': self.node.manufacturer_name, 'manufacturer': self.node.manufacturer_name,
'model': self.node.product_name, 'model': self.node.product_name,
} }
if self.values.primary.instance > 1: 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_device'] = (DOMAIN, self.node_id, ) info['via_device'] = (DOMAIN, self.node_id, )
else: elif self.node_id > 1:
info['name'] = node_name(self.node) info['via_device'] = (DOMAIN, 1, )
info['identifiers'] = {
(DOMAIN, self.node_id),
}
if self.node_id > 1:
info['via_device'] = (DOMAIN, 1, )
return info return info
@property @property

View File

@ -14,7 +14,7 @@ from .const import (
ATTR_NODE_ID, COMMAND_CLASS_WAKE_UP, ATTR_SCENE_ID, ATTR_SCENE_DATA, ATTR_NODE_ID, COMMAND_CLASS_WAKE_UP, ATTR_SCENE_ID, ATTR_SCENE_DATA,
ATTR_BASIC_LEVEL, EVENT_NODE_EVENT, EVENT_SCENE_ACTIVATED, ATTR_BASIC_LEVEL, EVENT_NODE_EVENT, EVENT_SCENE_ACTIVATED,
COMMAND_CLASS_CENTRAL_SCENE, DOMAIN) COMMAND_CLASS_CENTRAL_SCENE, DOMAIN)
from .util import node_name, is_node_parsed from .util import node_name, is_node_parsed, node_device_id_and_name
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -128,13 +128,14 @@ class ZWaveNodeEntity(ZWaveBaseEntity):
@property @property
def device_info(self): def device_info(self):
"""Return device information.""" """Return device information."""
identifier, name = node_device_id_and_name(self.node)
info = { info = {
'identifiers': { 'identifiers': {
(DOMAIN, self.node_id) identifier
}, },
'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) 'name': name
} }
if self.node_id > 1: if self.node_id > 1:
info['via_device'] = (DOMAIN, 1) info['via_device'] = (DOMAIN, 1)
@ -198,23 +199,22 @@ class ZWaveNodeEntity(ZWaveBaseEntity):
async def node_renamed(self, update_ids=False): async def node_renamed(self, update_ids=False):
"""Rename the node and update any IDs.""" """Rename the node and update any IDs."""
self._name = node_name(self.node) identifier, self._name = node_device_id_and_name(self.node)
# Set the name in the devices. If they're customised # Set the name in the devices. If they're customised
# the customisation will not be stored as name and will stick. # the customisation will not be stored as name and will stick.
dev_reg = await get_dev_reg(self.hass) dev_reg = await get_dev_reg(self.hass)
device = dev_reg.async_get_device( device = dev_reg.async_get_device(
identifiers={(DOMAIN, self.node_id), }, identifiers={identifier, },
connections=set()) connections=set())
dev_reg.async_update_device(device.id, name=self._name) dev_reg.async_update_device(device.id, name=self._name)
# update sub-devices too # update sub-devices too
for i in count(2): for i in count(2):
identifier = (DOMAIN, self.node_id, i) identifier, new_name = node_device_id_and_name(self.node, i)
device = dev_reg.async_get_device( device = dev_reg.async_get_device(
identifiers={identifier, }, identifiers={identifier, },
connections=set()) connections=set())
if not device: if not device:
break break
new_name = "{} ({})".format(self._name, i)
dev_reg.async_update_device(device.id, name=new_name) dev_reg.async_update_device(device.id, name=new_name)
# Update entity ID. # Update entity ID.
@ -230,6 +230,7 @@ class ZWaveNodeEntity(ZWaveBaseEntity):
ent_reg.async_update_entity( ent_reg.async_update_entity(
self.entity_id, new_entity_id=new_entity_id) self.entity_id, new_entity_id=new_entity_id)
return return
# else for the above two ifs, update if not using update_entity
self.async_schedule_update_ha_state() self.async_schedule_update_ha_state()
def network_node_event(self, node, value): def network_node_event(self, node, value):

View File

@ -74,6 +74,15 @@ def node_name(node):
return 'Unknown Node {}'.format(node.node_id) return 'Unknown Node {}'.format(node.node_id)
def node_device_id_and_name(node, instance=1):
"""Return the name and device ID for the value with the given index."""
name = node_name(node)
if instance == 1:
return ((const.DOMAIN, node.node_id), name)
name = "{} ({})".format(name, instance)
return ((const.DOMAIN, node.node_id, instance), name)
async def check_has_unique_id(entity, ready_callback, timeout_callback): async def check_has_unique_id(entity, ready_callback, timeout_callback):
"""Wait for entity to have unique_id.""" """Wait for entity to have unique_id."""
start_time = dt_util.utcnow() start_time = dt_util.utcnow()