Skip to content

Commit

Permalink
Changes as per code review of home-assistant#24646 (home-assistant#24917
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Swamp-Ig authored and amelchio committed Jul 7, 2019
1 parent e8a5306 commit adbec5b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
38 changes: 17 additions & 21 deletions homeassistant/components/zwave/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@
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__)

Expand Down Expand Up @@ -492,8 +493,7 @@ def stop_network(_service_or_event):
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]
Expand All @@ -506,15 +506,14 @@ def rename_node(service):
# 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)
Expand All @@ -528,7 +527,7 @@ def rename_value(service):
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."""
Expand Down Expand Up @@ -1069,6 +1068,7 @@ async def value_renamed(self, update_ids=False):
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):
Expand Down Expand Up @@ -1110,24 +1110,20 @@ def unique_id(self):
@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
Expand Down
15 changes: 8 additions & 7 deletions homeassistant/components/zwave/node_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
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__)

Expand Down Expand Up @@ -128,13 +128,14 @@ def unique_id(self):
@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)
Expand Down Expand Up @@ -198,23 +199,22 @@ def node_changed(self):

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.
Expand All @@ -230,6 +230,7 @@ async def node_renamed(self, update_ids=False):
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):
Expand Down
9 changes: 9 additions & 0 deletions homeassistant/components/zwave/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit adbec5b

Please sign in to comment.