44
55from asyncio import gather
66from collections .abc import Awaitable , Callable
7+ from dataclasses import replace
78from datetime import datetime
89import logging
9- from typing import Any , Final
10+ from typing import Any
1011
11- from ..api import NodeEvent , NodeFeature
12+ from ..api import NodeEvent , NodeFeature , SwitchGroup
1213from ..connection import StickController
1314from ..exceptions import MessageError , NodeError
1415from ..messages .responses import (
2223
2324_LOGGER = logging .getLogger (__name__ )
2425
25- CACHE_SWITCH_STATE : Final = "switch_state"
26- CACHE_SWITCH_TIMESTAMP : Final = "switch_timestamp"
27-
2826
2927class PlugwiseSwitch (NodeSED ):
3028 """Plugwise Switch node."""
@@ -39,7 +37,7 @@ def __init__(
3937 """Initialize Scan Device."""
4038 super ().__init__ (mac , address , controller , loaded_callback )
4139 self ._switch_subscription : Callable [[], None ] | None = None
42- self ._switch_state : bool | None = None
40+ self ._switch = SwitchGroup ()
4341
4442 async def load (self ) -> bool :
4543 """Load and activate Switch node features."""
@@ -73,7 +71,7 @@ async def initialize(self) -> bool:
7371 return True
7472
7573 self ._switch_subscription = await self ._message_subscribe (
76- self ._switch_group ,
74+ self ._switch_response ,
7775 self ._mac_in_bytes ,
7876 (NODE_SWITCH_GROUP_ID ,),
7977 )
@@ -92,50 +90,40 @@ async def unload(self) -> None:
9290 @raise_not_loaded
9391 def switch (self ) -> bool :
9492 """Current state of switch."""
95- return bool (self ._switch_state )
93+ return bool (self ._switch . state )
9694
9795 # endregion
9896
99- async def _switch_group (self , response : PlugwiseResponse ) -> bool :
97+ async def _switch_response (self , response : PlugwiseResponse ) -> bool :
10098 """Switch group request from Switch."""
10199 if not isinstance (response , NodeSwitchGroupResponse ):
102100 raise MessageError (
103101 f"Invalid response message type ({ response .__class__ .__name__ } ) received, expected NodeSwitchGroupResponse"
104102 )
105103 await gather (
106104 self ._available_update_state (True , response .timestamp ),
107- self ._switch_state_update (response .switch_state , response .timestamp ),
105+ self ._switch_state_update (
106+ response .switch_state , response .switch_group , response .timestamp
107+ ),
108108 )
109109 return True
110110
111111 async def _switch_state_update (
112- self , switch_state : bool , timestamp : datetime
112+ self , switch_state : bool , switch_group : int , timestamp : datetime
113113 ) -> None :
114114 """Process switch state update."""
115115 _LOGGER .debug (
116- "_switch_state_update for %s: %s -> %s " ,
116+ "_switch_state_update for %s: %s" ,
117117 self .name ,
118- self ._switch_state ,
119118 switch_state ,
120119 )
121- state_update = False
122- # Update cache
123- self ._set_cache (CACHE_SWITCH_STATE , str (switch_state ))
124- # Check for a state change
125- if self ._switch_state != switch_state :
126- self ._switch_state = switch_state
127- state_update = True
128-
129- self ._set_cache (CACHE_SWITCH_TIMESTAMP , timestamp )
130- if state_update :
131- await gather (
132- * [
133- self .publish_feature_update_to_subscribers (
134- NodeFeature .SWITCH , self ._switch_state
135- ),
136- self .save_cache (),
137- ]
138- )
120+ self ._switch = replace (
121+ self ._switch , state = switch_state , group = switch_group , timestamp = timestamp
122+ )
123+
124+ await self .publish_feature_update_to_subscribers (
125+ NodeFeature .SWITCH , self ._switch
126+ )
139127
140128 @raise_not_loaded
141129 async def get_state (self , features : tuple [NodeFeature ]) -> dict [NodeFeature , Any ]:
@@ -155,7 +143,7 @@ async def get_state(self, features: tuple[NodeFeature]) -> dict[NodeFeature, Any
155143
156144 match feature :
157145 case NodeFeature .SWITCH :
158- states [NodeFeature .SWITCH ] = self ._switch_state
146+ states [NodeFeature .SWITCH ] = self ._switch
159147 case _:
160148 state_result = await super ().get_state ((feature ,))
161149 states [feature ] = state_result [feature ]
0 commit comments