Use standard entity_ids for zwave entities (#7786)

* Use standard entity_ids for zwave entities

* Include temporary opt-in for new entity ids

* Update link to blog post

* Update tests

* Add old entity_id as state attribute

* Expose ZWave value details

* Update tests

* Also show new_entity_id

* Just can't win with this one
This commit is contained in:
Adam Mills committed 2017-06-16 13:25:12 -04:00
1 parent 4339e9aab1
commit d67f3b8060
8 files changed
+247 -123

No files matched your search

+39 -30
View File
@@ -16,6 +16,7 @@ import voluptuous as vol
from homeassistant.core import CoreState
from homeassistant.loader import get_platform
from homeassistant.helpers import discovery
from homeassistant.helpers.entity import generate_entity_id
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.const import (
ATTR_ENTITY_ID, EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
@@ -55,6 +56,7 @@ CONF_DEVICE_CONFIG = 'device_config'
CONF_DEVICE_CONFIG_GLOB = 'device_config_glob'
CONF_DEVICE_CONFIG_DOMAIN = 'device_config_domain'
CONF_NETWORK_KEY = 'network_key'
CONF_NEW_ENTITY_IDS = 'new_entity_ids'
ATTR_POWER = 'power_consumption'
@@ -149,6 +151,7 @@ CONFIG_SCHEMA = vol.Schema({
cv.positive_int,
vol.Optional(CONF_USB_STICK_PATH, default=DEFAULT_CONF_USB_STICK_PATH):
cv.string,
vol.Optional(CONF_NEW_ENTITY_IDS, default=False): cv.boolean,
}),
}, extra=vol.ALLOW_EXTRA)
@@ -162,7 +165,7 @@ def _obj_to_dict(obj):
def _value_name(value):
"""Return the name of the value."""
return '{} {}'.format(node_name(value.node), value.label)
return '{} {}'.format(node_name(value.node), value.label).strip()
def _node_object_id(node):
@@ -250,6 +253,13 @@ def setup(hass, config):
config[DOMAIN][CONF_DEVICE_CONFIG],
config[DOMAIN][CONF_DEVICE_CONFIG_DOMAIN],
config[DOMAIN][CONF_DEVICE_CONFIG_GLOB])
new_entity_ids = config[DOMAIN][CONF_NEW_ENTITY_IDS]
if not new_entity_ids:
_LOGGER.warning(
"ZWave entity_ids will soon be changing. To opt in to new "
"entity_ids now, set `new_entity_ids: true` under zwave in your "
"configuration.yaml. See the following blog post for details: "
"https://home-assistant.io/blog/2017/06/15/zwave-entity-ids/")
# Setup options
options = ZWaveOption(
@@ -311,30 +321,20 @@ def setup(hass, config):
def node_added(node):
"""Handle a new node on the network."""
entity = ZWaveNodeEntity(node, network)
node_config = device_config.get(entity.entity_id)
entity = ZWaveNodeEntity(node, network, new_entity_ids)
name = node_name(node)
if new_entity_ids:
generated_id = generate_entity_id(DOMAIN + '.{}', name, [])
else:
generated_id = entity.entity_id
node_config = device_config.get(generated_id)
if node_config.get(CONF_IGNORED):
_LOGGER.info(
"Ignoring node entity %s due to device settings",
entity.entity_id)
generated_id)
return
component.add_entities([entity])
def scene_activated(node, scene_id):
"""Handle an activated scene on any node in the network."""
hass.bus.fire(const.EVENT_SCENE_ACTIVATED, {
ATTR_ENTITY_ID: _node_object_id(node),
const.ATTR_OBJECT_ID: _node_object_id(node),
const.ATTR_SCENE_ID: scene_id
})
def node_event_activated(node, value):
"""Handle a nodeevent on any node in the network."""
hass.bus.fire(const.EVENT_NODE_EVENT, {
const.ATTR_OBJECT_ID: _node_object_id(node),
const.ATTR_BASIC_LEVEL: value
})
def network_ready():
"""Handle the query of all awake nodes."""
_LOGGER.info("Zwave network is ready for use. All awake nodes "
@@ -352,10 +352,6 @@ def setup(hass, config):
value_added, ZWaveNetwork.SIGNAL_VALUE_ADDED, weak=False)
dispatcher.connect(
node_added, ZWaveNetwork.SIGNAL_NODE_ADDED, weak=False)
dispatcher.connect(
scene_activated, ZWaveNetwork.SIGNAL_SCENE_EVENT, weak=False)
dispatcher.connect(
node_event_activated, ZWaveNetwork.SIGNAL_NODE_EVENT, weak=False)
dispatcher.connect(
network_ready, ZWaveNetwork.SIGNAL_AWAKE_NODES_QUERIED, weak=False)
dispatcher.connect(
@@ -757,9 +753,8 @@ class ZWaveDeviceEntityValues():
self.primary)
if workaround_component and workaround_component != component:
if workaround_component == workaround.WORKAROUND_IGNORE:
_LOGGER.info("Ignoring device %s due to workaround.",
"{}.{}".format(
component, object_id(self.primary)))
_LOGGER.info("Ignoring Node %d Value %d due to workaround.",
self.primary.node.node_id, self.primary.value_id)
# No entity will be created for this value
self._workaround_ignore = True
return
@@ -767,8 +762,13 @@ class ZWaveDeviceEntityValues():
workaround_component, component)
component = workaround_component
name = "{}.{}".format(component, object_id(self.primary))
node_config = self._device_config.get(name)
value_name = _value_name(self.primary)
if self._zwave_config[DOMAIN][CONF_NEW_ENTITY_IDS]:
generated_id = generate_entity_id(
component + '.{}', value_name, [])
else:
generated_id = "{}.{}".format(component, object_id(self.primary))
node_config = self._device_config.get(generated_id)
# Configure node
_LOGGER.debug("Adding Node_id=%s Generic_command_class=%s, "
@@ -781,7 +781,7 @@ class ZWaveDeviceEntityValues():
if node_config.get(CONF_IGNORED):
_LOGGER.info(
"Ignoring entity %s due to device settings", name)
"Ignoring entity %s due to device settings", generated_id)
# No entity will be created for this value
self._workaround_ignore = True
return
@@ -802,6 +802,12 @@ class ZWaveDeviceEntityValues():
self._workaround_ignore = True
return
device.old_entity_id = "{}.{}".format(
component, object_id(self.primary))
device.new_entity_id = "{}.{}".format(component, slugify(device.name))
if not self._zwave_config[DOMAIN][CONF_NEW_ENTITY_IDS]:
device.entity_id = device.old_entity_id
self._entity = device
dict_id = id(self)
@@ -828,7 +834,6 @@ class ZWaveDeviceEntity(ZWaveBaseEntity):
self.values = values
self.node = values.primary.node
self.values.primary.set_change_verified(False)
self.entity_id = "{}.{}".format(domain, object_id(values.primary))
self._name = _value_name(self.values.primary)
self._unique_id = "ZWAVE-{}-{}".format(self.node.node_id,
@@ -895,6 +900,10 @@ class ZWaveDeviceEntity(ZWaveBaseEntity):
"""Return the device specific state attributes."""
attrs = {
const.ATTR_NODE_ID: self.node_id,
const.ATTR_VALUE_INDEX: self.values.primary.index,
const.ATTR_VALUE_INSTANCE: self.values.primary.instance,
'old_entity_id': self.old_entity_id,
'new_entity_id': self.new_entity_id,
}
if self.power_consumption is not None:
+2
View File
@@ -31,6 +31,8 @@ class ZWaveNodeValueView(HomeAssistantView):
values_data[entity_values.primary.value_id] = {
'label': entity_values.primary.label,
'index': entity_values.primary.index,
'instance': entity_values.primary.instance,
}
return self.json(values_data)
+2
View File
@@ -14,6 +14,8 @@ ATTR_BASIC_LEVEL = "basic_level"
ATTR_CONFIG_PARAMETER = "parameter"
ATTR_CONFIG_SIZE = "size"
ATTR_CONFIG_VALUE = "value"
ATTR_VALUE_INDEX = "value_index"
ATTR_VALUE_INSTANCE = "value_instance"
NETWORK_READY_WAIT_SECS = 30
DISCOVERY_DEVICE = 'device'
+49 -4
View File
@@ -2,11 +2,13 @@
import logging
from homeassistant.core import callback
from homeassistant.const import ATTR_BATTERY_LEVEL, ATTR_WAKEUP
from homeassistant.const import ATTR_BATTERY_LEVEL, ATTR_WAKEUP, ATTR_ENTITY_ID
from homeassistant.helpers.entity import Entity
from homeassistant.util import slugify
from .const import ATTR_NODE_ID, DOMAIN, COMMAND_CLASS_WAKE_UP
from .const import (
ATTR_NODE_ID, COMMAND_CLASS_WAKE_UP, ATTR_SCENE_ID, ATTR_BASIC_LEVEL,
EVENT_NODE_EVENT, EVENT_SCENE_ACTIVATED, DOMAIN)
from .util import node_name
_LOGGER = logging.getLogger(__name__)
@@ -38,6 +40,8 @@ class ZWaveBaseEntity(Entity):
def __init__(self):
"""Initialize the base Z-Wave class."""
self._update_scheduled = False
self.old_entity_id = None
self.new_entity_id = None
def maybe_schedule_update(self):
"""Maybe schedule state update.
@@ -72,7 +76,7 @@ def sub_status(status, stage):
class ZWaveNodeEntity(ZWaveBaseEntity):
"""Representation of a Z-Wave node."""
def __init__(self, node, network):
def __init__(self, node, network, new_entity_ids):
"""Initialize node."""
# pylint: disable=import-error
super().__init__()
@@ -84,8 +88,11 @@ class ZWaveNodeEntity(ZWaveBaseEntity):
self._name = node_name(self.node)
self._product_name = node.product_name
self._manufacturer_name = node.manufacturer_name
self.entity_id = "{}.{}_{}".format(
self.old_entity_id = "{}.{}_{}".format(
DOMAIN, slugify(self._name), self.node_id)
self.new_entity_id = "{}.{}".format(DOMAIN, slugify(self._name))
if not new_entity_ids:
self.entity_id = self.old_entity_id
self._attributes = {}
self.wakeup_interval = None
self.location = None
@@ -95,6 +102,10 @@ class ZWaveNodeEntity(ZWaveBaseEntity):
dispatcher.connect(self.network_node_changed, ZWaveNetwork.SIGNAL_NODE)
dispatcher.connect(
self.network_node_changed, ZWaveNetwork.SIGNAL_NOTIFICATION)
dispatcher.connect(
self.network_node_event, ZWaveNetwork.SIGNAL_NODE_EVENT)
dispatcher.connect(
self.network_scene_activated, ZWaveNetwork.SIGNAL_SCENE_EVENT)
def network_node_changed(self, node=None, args=None):
"""Handle a changed node on the network."""
@@ -134,6 +145,38 @@ class ZWaveNodeEntity(ZWaveBaseEntity):
self.maybe_schedule_update()
def network_node_event(self, node, value):
"""Handle a node activated event on the network."""
if node.node_id == self.node.node_id:
self.node_event(value)
def node_event(self, value):
"""Handle a node activated event for this node."""
if self.hass is None:
return
self.hass.bus.fire(EVENT_NODE_EVENT, {
ATTR_ENTITY_ID: self.entity_id,
ATTR_NODE_ID: self.node.node_id,
ATTR_BASIC_LEVEL: value
})
def network_scene_activated(self, node, scene_id):
"""Handle a scene activated event on the network."""
if node.node_id == self.node.node_id:
self.scene_activated(scene_id)
def scene_activated(self, scene_id):
"""Handle an activated scene for this node."""
if self.hass is None:
return
self.hass.bus.fire(EVENT_SCENE_ACTIVATED, {
ATTR_ENTITY_ID: self.entity_id,
ATTR_NODE_ID: self.node.node_id,
ATTR_SCENE_ID: scene_id
})
@property
def state(self):
"""Return the state."""
@@ -169,6 +212,8 @@ class ZWaveNodeEntity(ZWaveBaseEntity):
ATTR_NODE_NAME: self._name,
ATTR_MANUFACTURER_NAME: self._manufacturer_name,
ATTR_PRODUCT_NAME: self._product_name,
'old_entity_id': self.old_entity_id,
'new_entity_id': self.new_entity_id,
}
attrs.update(self._attributes)
if self.battery_level is not None: