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:
parent
1c2f4866e2
commit
afb9cba806
8 changed files with 247 additions and 123 deletions
|
@ -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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue