2727from ..command_class import CommandClassInfo
2828from ..device_class import DeviceClass
2929from ..device_config import DeviceConfig
30- from ..endpoint import Endpoint
30+ from ..endpoint import Endpoint , EndpointDataType
3131from ..notification import (
3232 EntryControlNotification ,
3333 EntryControlNotificationDataType ,
@@ -115,6 +115,7 @@ def __init__(self, client: Client, data: NodeDataType) -> None:
115115 client , data .get ("statistics" , DEFAULT_NODE_STATISTICS )
116116 )
117117 self ._firmware_update_progress : NodeFirmwareUpdateProgress | None = None
118+ self ._device_class : DeviceClass | None = None
118119 self ._last_seen : datetime | None = None
119120 self .values : dict [str , ConfigurationValue | Value ] = {}
120121 self .endpoints : dict [int , Endpoint ] = {}
@@ -150,9 +151,7 @@ def index(self) -> int:
150151 @property
151152 def device_class (self ) -> DeviceClass | None :
152153 """Return the device_class."""
153- if (device_class := self .data .get ("deviceClass" )) is None :
154- return None
155- return DeviceClass (device_class )
154+ return self ._device_class
156155
157156 @property
158157 def installer_icon (self ) -> int | None :
@@ -372,21 +371,35 @@ def default_transition_duration(self) -> int | float | None:
372371 """Return the default transition duration."""
373372 return self .data .get ("defaultTransitionDuration" )
374373
375- def update (self , data : NodeDataType ) -> None :
376- """Update the internal state data."""
377- self .data = copy .deepcopy (data )
378- self ._device_config = DeviceConfig (self .data .get ("deviceConfig" , {}))
379- self ._statistics = NodeStatistics (
380- self .client , self .data .get ("statistics" , DEFAULT_NODE_STATISTICS )
381- )
382- if last_seen := data .get ("lastSeen" ):
383- self ._last_seen = datetime .fromisoformat (last_seen )
384- if not self ._statistics .last_seen :
385- self ._statistics .last_seen = self .last_seen
374+ def _update_endpoints (self , endpoints : list [EndpointDataType ]) -> None :
375+ """Update the endpoints data."""
376+ new_endpoints_data = {endpoint ["index" ]: endpoint for endpoint in endpoints }
377+ new_endpoint_idxs = set (new_endpoints_data )
378+ stale_endpoint_idxs = set (self .endpoints ) - new_endpoint_idxs
379+
380+ # Remove stale endpoints
381+ for endpoint_idx in stale_endpoint_idxs :
382+ self .endpoints .pop (endpoint_idx )
383+
384+ # Add new endpoints or update existing ones
385+ for endpoint_idx in new_endpoint_idxs :
386+ endpoint = new_endpoints_data [endpoint_idx ]
387+ values = {
388+ value_id : value
389+ for value_id , value in self .values .items ()
390+ if self .index == value .endpoint
391+ }
392+ if endpoint_idx in self .endpoints :
393+ self .endpoints [endpoint_idx ].update (endpoint , values )
394+ else :
395+ self .endpoints [endpoint_idx ] = Endpoint (
396+ self .client , self , endpoint , values
397+ )
386398
399+ def _update_values (self , values : list [ValueDataType ]) -> None :
400+ """Update the values data."""
387401 new_values_data = {
388- _get_value_id_str_from_dict (self , val ): val
389- for val in self .data .pop ("values" )
402+ _get_value_id_str_from_dict (self , val ): val for val in values
390403 }
391404 new_value_ids = set (new_values_data )
392405 stale_value_ids = set (self .values ) - new_value_ids
@@ -413,30 +426,25 @@ def update(self, data: NodeDataType) -> None:
413426 # If we can't parse the value, don't store it
414427 pass
415428
416- new_endpoints_data = {
417- endpoint ["index" ]: endpoint for endpoint in self .data .pop ("endpoints" )
418- }
419- new_endpoint_idxs = set (new_endpoints_data )
420- stale_endpoint_idxs = set (self .endpoints ) - new_endpoint_idxs
429+ def update (self , data : NodeDataType ) -> None :
430+ """Update the internal state data."""
431+ self .data = copy .deepcopy (data )
432+ self ._device_config = DeviceConfig (self .data .get ("deviceConfig" , {}))
433+ if (device_class := self .data .get ("deviceClass" )) is None :
434+ self ._device_class = None
435+ else :
436+ self ._device_class = DeviceClass (device_class )
421437
422- # Remove stale endpoints
423- for endpoint_idx in stale_endpoint_idxs :
424- self .endpoints .pop (endpoint_idx )
438+ self ._statistics = NodeStatistics (
439+ self .client , self .data .get ("statistics" , DEFAULT_NODE_STATISTICS )
440+ )
441+ if last_seen := data .get ("lastSeen" ):
442+ self ._last_seen = datetime .fromisoformat (last_seen )
443+ if not self ._statistics .last_seen :
444+ self ._statistics .last_seen = self .last_seen
425445
426- # Add new endpoints or update existing ones
427- for endpoint_idx in new_endpoint_idxs - stale_endpoint_idxs :
428- endpoint = new_endpoints_data [endpoint_idx ]
429- values = {
430- value_id : value
431- for value_id , value in self .values .items ()
432- if self .index == value .endpoint
433- }
434- if endpoint_idx in self .endpoints :
435- self .endpoints [endpoint_idx ].update (endpoint , values )
436- else :
437- self .endpoints [endpoint_idx ] = Endpoint (
438- self .client , self , endpoint , values
439- )
446+ self ._update_values (self .data .pop ("values" ))
447+ self ._update_endpoints (self .data .pop ("endpoints" ))
440448
441449 def get_command_class_values (
442450 self , command_class : CommandClass , endpoint : int | None = None
0 commit comments