zwave refactor, don't use ozw values directly in properties. (#5961)

* First round of not using values directly

* Round two

* lint

* Round four

* Conflict

* Round five

* Update zwave.py

* round six

* Docstring

* flakywakie

* Fetch values in constructor

* Blank line removal

* Set attributes in callback

* Docstring

* Round seven

* Ughgit add homeassistant/components/lock/zwave.py!

* Sloppy code
This commit is contained in:
John Arild Berentsen authored and Paulus Schoutsen committed 2017-02-17 23:56:05 -08:00
1 parent c1eed148cc
commit 799fbe42f8
8 files changed
+78 -76

No files matched your search

+26 -35
View File
@@ -262,22 +262,6 @@ def get_config_value(node, value_index, tries=5):
return None
def _get_wakeup(node, tries=5):
"""Return wakeup interval of the node or None if node is not wakable."""
try:
if node.can_wake_up():
for value_id in node.get_values(
class_id=const.COMMAND_CLASS_WAKE_UP):
return node.values[value_id].data
except RuntimeError:
# If we get an runtime error the dict has changed while
# we was looking for a value, just do it again
return None if tries <= 0 else _get_wakeup(
node, tries=tries - 1)
return None
# pylint: disable=R0914
def setup(hass, config):
"""Setup Z-Wave.
@@ -692,6 +676,7 @@ class ZWaveDeviceEntity(Entity):
from pydispatch import dispatcher
self._value = value
self.entity_id = "{}.{}".format(domain, self._object_id())
self._update_attributes()
dispatcher.connect(
self.network_value_changed, ZWaveNetwork.SIGNAL_VALUE_CHANGED)
@@ -705,12 +690,28 @@ class ZWaveDeviceEntity(Entity):
def value_changed(self, value):
"""Called when a value for this entity's node has changed."""
self._update_attributes()
self.update_properties()
self.schedule_update_ha_state()
def _update_attributes(self):
"""Update the node attributes. May only be used inside callback."""
self.node_id = self._value.node.node_id
self.location = self._value.node.location
self.battery_level = self._value.node.get_battery_level()
self.wakeup_interval = None
if self._value.node.can_wake_up():
self.wakeup_interval = self.get_value(
class_id=const.COMMAND_CLASS_WAKE_UP,
member='data')
def _value_handler(self, method=None, class_id=None, index=None,
label=None, data=None, member=None, **kwargs):
"""Get the values for a given command_class with arguments."""
"""Get the values for a given command_class with arguments.
May only be used inside callback.
"""
if class_id is not None:
kwargs[CLASS_ID] = class_id
_LOGGER.debug('method=%s, class_id=%s, index=%s, label=%s, data=%s,'
@@ -746,7 +747,7 @@ class ZWaveDeviceEntity(Entity):
return results
def get_value(self, **kwargs):
"""Simplifyer to get values."""
"""Simplifyer to get values. May only be used inside callback."""
return self._value_handler(method='get', **kwargs)
def set_value(self, **kwargs):
@@ -785,26 +786,16 @@ class ZWaveDeviceEntity(Entity):
def device_state_attributes(self):
"""Return the device specific state attributes."""
attrs = {
const.ATTR_NODE_ID: self._value.node.node_id,
const.ATTR_NODE_ID: self.node_id,
}
try:
battery_level = self._value.node.get_battery_level()
except RuntimeError:
# If we get an runtime error the dict has changed while
# we was looking for a value, just do it again
battery_level = self._value.node.get_battery_level()
if self.battery_level is not None:
attrs[ATTR_BATTERY_LEVEL] = self.battery_level
if battery_level is not None:
attrs[ATTR_BATTERY_LEVEL] = battery_level
if self.location:
attrs[ATTR_LOCATION] = self.location
location = self._value.node.location
if location:
attrs[ATTR_LOCATION] = location
wakeup = _get_wakeup(self._value.node)
if wakeup:
attrs[ATTR_WAKEUP] = wakeup
if self.wakeup_interval is not None:
attrs[ATTR_WAKEUP] = self.wakeup_interval
return attrs