diff --git a/matter_server/common/helpers/util.py b/matter_server/common/helpers/util.py index 949c23cb..217207ca 100644 --- a/matter_server/common/helpers/util.py +++ b/matter_server/common/helpers/util.py @@ -1,4 +1,5 @@ """Utils for Matter server (and client).""" + from __future__ import annotations import base64 @@ -257,6 +258,7 @@ def dataclass_from_dict(cls: type[_T], dict_obj: dict, strict: bool = False) -> dict_obj.get(field.name), type_hints[field.name], field.default, + allow_none=not strict, ) for field in dc_fields if field.init diff --git a/matter_server/server/device_controller.py b/matter_server/server/device_controller.py index 2f35027d..761128ea 100644 --- a/matter_server/server/device_controller.py +++ b/matter_server/server/device_controller.py @@ -24,7 +24,6 @@ from matter_server.server.helpers.attributes import parse_attributes_from_read_result from matter_server.server.helpers.utils import ping_ip -from ..common.const import SCHEMA_VERSION from ..common.errors import ( NodeCommissionFailed, NodeInterviewFailed, @@ -156,11 +155,23 @@ async def start(self) -> None: # as this can no longer happen. orphaned_nodes.add(node_id_str) continue - if node_dict.get("interview_version") != SCHEMA_VERSION: - # Invalidate node attributes data if schema mismatch, - # the node will automatically be scheduled for re-interview. - node_dict["attributes"] = {} - node = dataclass_from_dict(MatterNodeData, node_dict) + try: + node = dataclass_from_dict(MatterNodeData, node_dict, strict=True) + except (KeyError, ValueError): + # constructing MatterNodeData from the cached dict is not possible, + # revert to a fallback object and the node will be re-interviewed + node = MatterNodeData( + node_id=node_id, + date_commissioned=node_dict.get( + "date_commissioned", + datetime(1970, 1, 1), + ), + last_interview=node_dict.get( + "last_interview", + datetime(1970, 1, 1), + ), + interview_version=0, + ) # always mark node as unavailable at startup until subscriptions are ready node.available = False self._nodes[node_id] = node