From adbec5bffc0914f051c56e506a42df6fcf92adba Mon Sep 17 00:00:00 2001 From: Penny Wood Date: Sun, 7 Jul 2019 13:36:57 +0800 Subject: [PATCH] Changes as per code review of #24646 (#24917) --- homeassistant/components/zwave/__init__.py | 38 +++++++++---------- homeassistant/components/zwave/node_entity.py | 15 ++++---- homeassistant/components/zwave/util.py | 9 +++++ 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/homeassistant/components/zwave/__init__.py b/homeassistant/components/zwave/__init__.py index b32a76be40b..9b6cf58425b 100644 --- a/homeassistant/components/zwave/__init__.py +++ b/homeassistant/components/zwave/__init__.py @@ -35,8 +35,9 @@ from .const import ( from .node_entity import ZWaveBaseEntity, ZWaveNodeEntity from . import workaround from .discovery_schemas import DISCOVERY_SCHEMAS -from .util import (check_node_schema, check_value_schema, node_name, - check_has_unique_id, is_node_parsed) +from .util import ( + check_node_schema, check_value_schema, node_name, check_has_unique_id, + is_node_parsed, node_device_id_and_name) _LOGGER = logging.getLogger(__name__) @@ -492,8 +493,7 @@ async def async_setup_entry(hass, config_entry): if hass.state == CoreState.running: hass.bus.fire(const.EVENT_NETWORK_STOP) - @callback - def rename_node(service): + async def rename_node(service): """Rename a node.""" node_id = service.data.get(const.ATTR_NODE_ID) node = network.nodes[node_id] @@ -506,15 +506,14 @@ async def async_setup_entry(hass, config_entry): # and all the contained entities node_key = 'node-{}'.format(node_id) 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]): if not key.startswith('{}-'.format(node_id)): continue entity = hass.data[DATA_DEVICES][key] - hass.async_create_task(entity.value_renamed(update_ids)) + await entity.value_renamed(update_ids) - @callback - def rename_value(service): + async def rename_value(service): """Rename a node value.""" node_id = service.data.get(const.ATTR_NODE_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) value_key = '{}-{}'.format(node_id, value_id) 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): """Set the polling intensity of a node value.""" @@ -1069,6 +1068,7 @@ class ZWaveDeviceEntity(ZWaveBaseEntity): ent_reg.async_update_entity( self.entity_id, new_entity_id=new_entity_id) return + # else for the above two ifs, update if not using update_entity self.async_schedule_update_ha_state() async def async_added_to_hass(self): @@ -1110,24 +1110,20 @@ class ZWaveDeviceEntity(ZWaveBaseEntity): @property def device_info(self): """Return device information.""" + identifier, name = node_device_id_and_name( + self.node, self.values.primary.instance) info = { + 'name': name, + 'identifiers': { + identifier + }, 'manufacturer': self.node.manufacturer_name, 'model': self.node.product_name, } 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, ) - else: - info['name'] = node_name(self.node) - info['identifiers'] = { - (DOMAIN, self.node_id), - } - if self.node_id > 1: - info['via_device'] = (DOMAIN, 1, ) + elif self.node_id > 1: + info['via_device'] = (DOMAIN, 1, ) return info @property diff --git a/homeassistant/components/zwave/node_entity.py b/homeassistant/components/zwave/node_entity.py index 9a721ecf2d7..91f50007310 100644 --- a/homeassistant/components/zwave/node_entity.py +++ b/homeassistant/components/zwave/node_entity.py @@ -14,7 +14,7 @@ from .const import ( ATTR_NODE_ID, COMMAND_CLASS_WAKE_UP, ATTR_SCENE_ID, ATTR_SCENE_DATA, ATTR_BASIC_LEVEL, EVENT_NODE_EVENT, EVENT_SCENE_ACTIVATED, 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__) @@ -128,13 +128,14 @@ class ZWaveNodeEntity(ZWaveBaseEntity): @property def device_info(self): """Return device information.""" + identifier, name = node_device_id_and_name(self.node) info = { 'identifiers': { - (DOMAIN, self.node_id) + identifier }, 'manufacturer': self.node.manufacturer_name, 'model': self.node.product_name, - 'name': node_name(self.node) + 'name': name } if self.node_id > 1: info['via_device'] = (DOMAIN, 1) @@ -198,23 +199,22 @@ class ZWaveNodeEntity(ZWaveBaseEntity): async def node_renamed(self, update_ids=False): """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 # the customisation will not be stored as name and will stick. dev_reg = await get_dev_reg(self.hass) device = dev_reg.async_get_device( - identifiers={(DOMAIN, self.node_id), }, + identifiers={identifier, }, connections=set()) dev_reg.async_update_device(device.id, name=self._name) # update sub-devices too 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( identifiers={identifier, }, connections=set()) if not device: break - new_name = "{} ({})".format(self._name, i) dev_reg.async_update_device(device.id, name=new_name) # Update entity ID. @@ -230,6 +230,7 @@ class ZWaveNodeEntity(ZWaveBaseEntity): ent_reg.async_update_entity( self.entity_id, new_entity_id=new_entity_id) return + # else for the above two ifs, update if not using update_entity self.async_schedule_update_ha_state() def network_node_event(self, node, value): diff --git a/homeassistant/components/zwave/util.py b/homeassistant/components/zwave/util.py index a3b6d9a956d..bc803113f52 100644 --- a/homeassistant/components/zwave/util.py +++ b/homeassistant/components/zwave/util.py @@ -74,6 +74,15 @@ def node_name(node): 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): """Wait for entity to have unique_id.""" start_time = dt_util.utcnow()