88from datetime import UTC , datetime
99from functools import wraps
1010import logging
11- from typing import Any , TypeVar , cast
11+ from typing import Any , Final , TypeVar , cast
1212
1313from ..api import (
1414 EnergyStatistics ,
1515 NodeEvent ,
1616 NodeFeature ,
1717 NodeInfo ,
1818 NodeInfoMessage ,
19+ NodeType ,
1920 PowerStatistics ,
2021 RelayConfig ,
2122 RelayLock ,
5859CACHE_RELAY_INIT = "relay_init"
5960CACHE_RELAY_LOCK = "relay_lock"
6061
62+ CIRCLE_FEATURES : Final = (
63+ NodeFeature .CIRCLE ,
64+ NodeFeature .RELAY ,
65+ NodeFeature .RELAY_INIT ,
66+ NodeFeature .RELAY_LOCK ,
67+ NodeFeature .ENERGY ,
68+ NodeFeature .POWER ,
69+ )
70+
71+
72+ # Default firmware if not known
73+ DEFAULT_FIRMWARE : Final = datetime (2008 , 8 , 26 , 15 , 46 , tzinfo = UTC )
74+
6175FuncT = TypeVar ("FuncT" , bound = Callable [..., Any ])
6276_LOGGER = logging .getLogger (__name__ )
6377
@@ -81,11 +95,12 @@ def __init__(
8195 self ,
8296 mac : str ,
8397 address : int ,
98+ node_type : NodeType ,
8499 controller : StickController ,
85100 loaded_callback : Callable [[NodeEvent , str ], Awaitable [None ]],
86101 ):
87102 """Initialize base class for Sleeping End Device."""
88- super ().__init__ (mac , address , controller , loaded_callback )
103+ super ().__init__ (mac , address , node_type , controller , loaded_callback )
89104
90105 # Relay
91106 self ._relay_lock : RelayLock = RelayLock ()
@@ -882,10 +897,10 @@ async def clock_synchronize(self) -> bool:
882897 return True
883898 return False
884899
885- async def load (self ) -> bool :
900+ async def load (self ) -> None :
886901 """Load and activate Circle node features."""
887902 if self ._loaded :
888- return True
903+ return
889904
890905 if self ._cache_enabled :
891906 _LOGGER .debug ("Loading Circle node %s from cache" , self ._mac_in_str )
@@ -895,37 +910,20 @@ async def load(self) -> bool:
895910 _LOGGER .debug ("Retrieving Info For Circle node %s" , self ._mac_in_str )
896911
897912 # Check if node is online
898- if not self ._available and not await self .is_online ():
899- _LOGGER .debug (
900- "Failed to load Circle node %s because it is not online" ,
901- self ._mac_in_str ,
902- )
903- return False
904-
905- # Get node info
906- if await self .node_info_update () is None :
913+ if (
914+ not self ._available and not await self .is_online ()
915+ ) or await self .node_info_update () is None :
907916 _LOGGER .debug (
908- "Failed to load Circle node %s because it is not responding to information request " ,
917+ "Failed to retrieve NodeInfo for %s, loading defaults " ,
909918 self ._mac_in_str ,
910919 )
911- return False
920+ await self . _load_defaults ()
912921
913- self ._loaded = True
922+ self ._loaded = True
914923
915- self ._setup_protocol (
916- CIRCLE_FIRMWARE_SUPPORT ,
917- (
918- NodeFeature .CIRCLE ,
919- NodeFeature .RELAY ,
920- NodeFeature .RELAY_INIT ,
921- NodeFeature .RELAY_LOCK ,
922- NodeFeature .ENERGY ,
923- NodeFeature .POWER ,
924- ),
925- )
924+ self ._setup_protocol (CIRCLE_FIRMWARE_SUPPORT , CIRCLE_FEATURES )
926925 await self ._loaded_callback (NodeEvent .LOADED , self .mac )
927926 await self .initialize ()
928- return True
929927
930928 async def _load_from_cache (self ) -> bool :
931929 """Load states from previous cached information. Returns True if successful."""
@@ -972,6 +970,15 @@ async def _load_from_cache(self) -> bool:
972970
973971 return result
974972
973+ async def _load_defaults (self ) -> None :
974+ """Load default configuration settings."""
975+ if self ._node_info .model is None :
976+ self ._node_info .model = "Circle"
977+ if self ._node_info .name is None :
978+ self ._node_info .name = f"Circle { self ._node_info .mac [- 5 :]} "
979+ if self ._node_info .firmware is None :
980+ self ._node_info .firmware = DEFAULT_FIRMWARE
981+
975982 @raise_not_loaded
976983 async def initialize (self ) -> bool :
977984 """Initialize node."""
0 commit comments