Skip to content

Commit

Permalink
Update nodes internal state data (#996)
Browse files Browse the repository at this point in the history
  • Loading branch information
raman325 authored Jun 15, 2024
1 parent aca0e47 commit e899ba9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
12 changes: 8 additions & 4 deletions test/model/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,11 @@ async def test_last_seen(lock_schlage_be469):
assert lock_schlage_be469.last_seen == datetime(
2023, 7, 18, 15, 42, 34, 701000, UTC
)
assert lock_schlage_be469.last_seen == lock_schlage_be469.statistics.last_seen
assert (
lock_schlage_be469.last_seen
== lock_schlage_be469.statistics.last_seen
== datetime.fromisoformat(lock_schlage_be469.statistics.data.get("lastSeen"))
)


async def test_highest_security_value(lock_schlage_be469, ring_keypad):
Expand Down Expand Up @@ -556,9 +560,7 @@ async def test_get_value_metadata(multisensor_6, uuid4, mock_command):
},
)

value_id = "52-32-0-targetValue"
value = node.values[value_id]
result = await node.async_get_value_metadata(value)
result = await node.async_get_value_metadata("52-32-0-targetValue")

assert result.type == "any"
assert result.readable is True
Expand All @@ -574,6 +576,8 @@ async def test_get_value_metadata(multisensor_6, uuid4, mock_command):
"messageId": uuid4,
}

ack_commands.clear()


async def test_abort_firmware_update(multisensor_6, uuid4, mock_command):
"""Test abort firmware update."""
Expand Down
28 changes: 13 additions & 15 deletions zwave_js_server/model/node/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@
SecurityClass,
)
from ...event import Event, EventBase
from ...exceptions import (
FailedCommand,
NotFoundError,
UnparseableValue,
UnwriteableValue,
)
from ...exceptions import NotFoundError, UnparseableValue, UnwriteableValue
from ..command_class import CommandClassInfo
from ..device_class import DeviceClass
from ..device_config import DeviceConfig
Expand All @@ -50,7 +45,6 @@
ValueMetadata,
ValueNotification,
_get_value_id_str_from_dict,
_init_value,
)
from .data_model import NodeDataType
from .event_model import NODE_EVENT_MODEL_MAP
Expand Down Expand Up @@ -139,6 +133,12 @@ def __eq__(self, other: object) -> bool:
self.client.driver == other.client.driver and self.node_id == other.node_id
)

def _init_value(self, val: ValueDataType) -> Value | ConfigurationValue:
"""Initialize a Value object from ValueDataType."""
if val["commandClass"] == CommandClass.CONFIGURATION:
return ConfigurationValue(self, val)
return Value(self, val)

@property
def node_id(self) -> int:
"""Return node ID property."""
Expand Down Expand Up @@ -429,7 +429,7 @@ def _update_values(self, values: list[ValueDataType]) -> None:
if value_id in self.values:
self.values[value_id].update(val)
else:
self.values[value_id] = _init_value(self, val)
self.values[value_id] = self._init_value(val)
except UnparseableValue:
# If we can't parse the value, don't store it
pass
Expand All @@ -448,8 +448,9 @@ def update(self, data: NodeDataType) -> None:
)
if last_seen := data.get("lastSeen"):
self._last_seen = datetime.fromisoformat(last_seen)
if not self._statistics.last_seen:
if not self._statistics.last_seen and self.last_seen:
self._statistics.last_seen = self.last_seen
self._statistics.data["lastSeen"] = self.last_seen.isoformat()

self._update_values(self.data.pop("values"))
self._update_endpoints(self.data.pop("endpoints"))
Expand Down Expand Up @@ -593,12 +594,9 @@ async def async_get_defined_value_ids(self) -> list[Value]:
data = await self.async_send_command(
"get_defined_value_ids", wait_for_result=True
)

if data is None:
# We should never reach this code
raise FailedCommand("Command failed", "failed_command")
assert data
return [
_init_value(self, cast(ValueDataType, value_id))
self._init_value(cast(ValueDataType, value_id))
for value_id in data["valueIds"]
]

Expand Down Expand Up @@ -1061,7 +1059,7 @@ def handle_value_updated(self, event: Event) -> None:
value_id = _get_value_id_str_from_dict(self, evt_val_data)
value = self.values.get(value_id)
if value is None:
value = _init_value(self, evt_val_data)
value = self._init_value(evt_val_data)
self.values[value.value_id] = event.data["value"] = value
else:
value.receive_event(event)
Expand Down
8 changes: 0 additions & 8 deletions zwave_js_server/model/value.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from ..const import (
VALUE_UNKNOWN,
CommandClass,
CommandStatus,
ConfigurationValueType,
SetValueStatus,
Expand Down Expand Up @@ -75,13 +74,6 @@ class ValueDataType(TypedDict, total=False):
ccVersion: int # required


def _init_value(node: Node, val: ValueDataType) -> Value | ConfigurationValue:
"""Initialize a Value object from ValueDataType."""
if val["commandClass"] == CommandClass.CONFIGURATION:
return ConfigurationValue(node, val)
return Value(node, val)


def _get_value_id_str_from_dict(node: Node, val: ValueDataType) -> str:
"""Return string ID of value from ValueDataType dict."""
return get_value_id_str(
Expand Down

0 comments on commit e899ba9

Please sign in to comment.