This commit is contained in:
Paulus Schoutsen committed 2019-07-31 12:25:30 -07:00
1 parent da05dfe708
commit 4de97abc3a
2676 files changed
+163166 -140084

No files matched your search

+96 -71
View File
@@ -3,39 +3,60 @@ 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.device_registry import async_get_registry as get_dev_reg
from homeassistant.helpers.entity import Entity
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)
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, node_device_id_and_name
_LOGGER = logging.getLogger(__name__)
ATTR_QUERY_STAGE = 'query_stage'
ATTR_AWAKE = 'is_awake'
ATTR_READY = 'is_ready'
ATTR_FAILED = 'is_failed'
ATTR_PRODUCT_NAME = 'product_name'
ATTR_MANUFACTURER_NAME = 'manufacturer_name'
ATTR_NODE_NAME = 'node_name'
ATTR_QUERY_STAGE = "query_stage"
ATTR_AWAKE = "is_awake"
ATTR_READY = "is_ready"
ATTR_FAILED = "is_failed"
ATTR_PRODUCT_NAME = "product_name"
ATTR_MANUFACTURER_NAME = "manufacturer_name"
ATTR_NODE_NAME = "node_name"
STAGE_COMPLETE = 'Complete'
STAGE_COMPLETE = "Complete"
_REQUIRED_ATTRIBUTES = [
ATTR_QUERY_STAGE, ATTR_AWAKE, ATTR_READY, ATTR_FAILED,
'is_info_received', 'max_baud_rate', 'is_zwave_plus']
_OPTIONAL_ATTRIBUTES = ['capabilities', 'neighbors', 'location']
ATTR_QUERY_STAGE,
ATTR_AWAKE,
ATTR_READY,
ATTR_FAILED,
"is_info_received",
"max_baud_rate",
"is_zwave_plus",
]
_OPTIONAL_ATTRIBUTES = ["capabilities", "neighbors", "location"]
_COMM_ATTRIBUTES = [
'sentCnt', 'sentFailed', 'retries', 'receivedCnt', 'receivedDups',
'receivedUnsolicited', 'sentTS', 'receivedTS', 'lastRequestRTT',
'averageRequestRTT', 'lastResponseRTT', 'averageResponseRTT']
"sentCnt",
"sentFailed",
"retries",
"receivedCnt",
"receivedDups",
"receivedUnsolicited",
"sentTS",
"receivedTS",
"lastRequestRTT",
"averageRequestRTT",
"lastResponseRTT",
"averageResponseRTT",
]
ATTRIBUTES = _REQUIRED_ATTRIBUTES + _OPTIONAL_ATTRIBUTES
@@ -72,10 +93,12 @@ class ZWaveBaseEntity(Entity):
def try_remove_and_add(self):
"""Remove this entity and add it back."""
async def _async_remove_and_add():
await self.async_remove()
self.entity_id = None
await self.platform.async_add_entities([self])
if self.hass and self.platform:
self.hass.add_job(_async_remove_and_add)
@@ -99,6 +122,7 @@ class ZWaveNodeEntity(ZWaveBaseEntity):
super().__init__()
from openzwave.network import ZWaveNetwork
from pydispatch import dispatcher
self._network = network
self.node = node
self.node_id = self.node.node_id
@@ -110,15 +134,13 @@ class ZWaveNodeEntity(ZWaveBaseEntity):
self.wakeup_interval = None
self.location = None
self.battery_level = None
dispatcher.connect(
self.network_node_changed, ZWaveNetwork.SIGNAL_VALUE_CHANGED)
dispatcher.connect(self.network_node_changed, ZWaveNetwork.SIGNAL_VALUE_CHANGED)
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_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)
self.network_scene_activated, ZWaveNetwork.SIGNAL_SCENE_EVENT
)
@property
def unique_id(self):
@@ -130,28 +152,24 @@ class ZWaveNodeEntity(ZWaveBaseEntity):
"""Return device information."""
identifier, name = node_device_id_and_name(self.node)
info = {
'identifiers': {
identifier
},
'manufacturer': self.node.manufacturer_name,
'model': self.node.product_name,
'name': name
"identifiers": {identifier},
"manufacturer": self.node.manufacturer_name,
"model": self.node.product_name,
"name": name,
}
if self.node_id > 1:
info['via_device'] = (DOMAIN, 1)
info["via_device"] = (DOMAIN, 1)
return info
def network_node_changed(self, node=None, value=None, args=None):
"""Handle a changed node on the network."""
if node and node.node_id != self.node_id:
return
if args is not None and 'nodeId' in args and \
args['nodeId'] != self.node_id:
if args is not None and "nodeId" in args and args["nodeId"] != self.node_id:
return
# Process central scene activation
if (value is not None and
value.command_class == COMMAND_CLASS_CENTRAL_SCENE):
if value is not None and value.command_class == COMMAND_CLASS_CENTRAL_SCENE:
self.central_scene_activated(value.index, value.data)
self.node_changed()
@@ -159,7 +177,8 @@ class ZWaveNodeEntity(ZWaveBaseEntity):
def get_node_statistics(self):
"""Retrieve statistics from the node."""
return self._network.manager.getNodeStatistics(
self._network.home_id, self.node_id)
self._network.home_id, self.node_id
)
def node_changed(self):
"""Update node properties."""
@@ -203,16 +222,14 @@ class ZWaveNodeEntity(ZWaveBaseEntity):
# 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={identifier, },
connections=set())
device = dev_reg.async_get_device(identifiers={identifier}, connections=set())
dev_reg.async_update_device(device.id, name=self._name)
# update sub-devices too
for i in count(2):
identifier, new_name = node_device_id_and_name(self.node, i)
device = dev_reg.async_get_device(
identifiers={identifier, },
connections=set())
identifiers={identifier}, connections=set()
)
if not device:
break
dev_reg.async_update_device(device.id, name=new_name)
@@ -221,14 +238,13 @@ class ZWaveNodeEntity(ZWaveBaseEntity):
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})
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)
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()
@@ -243,11 +259,14 @@ class ZWaveNodeEntity(ZWaveBaseEntity):
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
})
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."""
@@ -259,23 +278,29 @@ class ZWaveNodeEntity(ZWaveBaseEntity):
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
})
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,
},
)
def central_scene_activated(self, scene_id, scene_data):
"""Handle an activated central 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_id,
ATTR_SCENE_ID: scene_id,
ATTR_SCENE_DATA: scene_data
})
self.hass.bus.fire(
EVENT_SCENE_ACTIVATED,
{
ATTR_ENTITY_ID: self.entity_id,
ATTR_NODE_ID: self.node_id,
ATTR_SCENE_ID: scene_id,
ATTR_SCENE_DATA: scene_data,
},
)
@property
def state(self):
@@ -284,13 +309,13 @@ class ZWaveNodeEntity(ZWaveBaseEntity):
return None
if self._attributes[ATTR_FAILED]:
return 'dead'
if self._attributes[ATTR_QUERY_STAGE] != 'Complete':
return 'initializing'
return "dead"
if self._attributes[ATTR_QUERY_STAGE] != "Complete":
return "initializing"
if not self._attributes[ATTR_AWAKE]:
return 'sleeping'
return "sleeping"
if self._attributes[ATTR_READY]:
return 'ready'
return "ready"
return None
@@ -323,5 +348,5 @@ class ZWaveNodeEntity(ZWaveBaseEntity):
def _compute_unique_id(self):
if is_node_parsed(self.node) or self.node.is_ready:
return 'node-{}'.format(self.node_id)
return "node-{}".format(self.node_id)
return None