Update IDs for rename node/value (#24646)

* Update IDs for rename node/value

* Rename devices and entities

* Improved coverage
This commit is contained in:
Penny Wood authored and Paulus Schoutsen committed 2019-07-01 15:54:19 -07:00
1 parent 7f90a1cab2
commit 23dd644f4a
7 files changed
+250 -19

No files matched your search

+41 -1
View File
@@ -1,9 +1,13 @@
"""Entity class that represents Z-Wave node."""
import logging
from itertools import count
from homeassistant.core import callback
from homeassistant.const import ATTR_BATTERY_LEVEL, ATTR_WAKEUP, ATTR_ENTITY_ID
from homeassistant.const import (
ATTR_BATTERY_LEVEL, ATTR_WAKEUP, ATTR_ENTITY_ID)
from homeassistant.helpers.entity_registry import async_get_registry
from homeassistant.helpers.device_registry import (
async_get_registry as get_dev_reg)
from homeassistant.helpers.entity import Entity
from .const import (
@@ -192,6 +196,42 @@ class ZWaveNodeEntity(ZWaveBaseEntity):
self.maybe_schedule_update()
async def node_renamed(self, update_ids=False):
"""Rename the node and update any IDs."""
self._name = node_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), },
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)
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.
if update_ids:
ent_reg = await async_get_registry(self.hass)
new_entity_id = ent_reg.async_generate_entity_id(
DOMAIN, self._name,
self.platform.entities.keys() - {self.entity_id})
if new_entity_id != self.entity_id:
# Don't change the name attribute, it will be None unless
# customised and if it's been customised, keep the
# customisation.
ent_reg.async_update_entity(
self.entity_id, new_entity_id=new_entity_id)
return
self.async_schedule_update_ha_state()
def network_node_event(self, node, value):
"""Handle a node activated event on the network."""
if node.node_id == self.node.node_id: