66from collections .abc import Awaitable , Callable
77from datetime import datetime
88import logging
9- from typing import Any , Final
9+ from typing import Any
1010
11- from ..api import NodeEvent , NodeFeature
11+ from ..api import NodeEvent , NodeFeature , SwitchGroup
1212from ..connection import StickController
1313from ..exceptions import MessageError , NodeError
1414from ..messages .responses import (
2222
2323_LOGGER = logging .getLogger (__name__ )
2424
25- CACHE_SWITCH_STATE : Final = "switch_state"
26- CACHE_SWITCH_TIMESTAMP : Final = "switch_timestamp"
27-
2825
2926class PlugwiseSwitch (NodeSED ):
3027 """Plugwise Switch node."""
@@ -39,7 +36,8 @@ def __init__(
3936 """Initialize Scan Device."""
4037 super ().__init__ (mac , address , controller , loaded_callback )
4138 self ._switch_subscription : Callable [[], None ] | None = None
42- self ._switch_state : bool | None = None
39+ self ._switch_group_1 = SwitchGroup ()
40+ self ._switch_group_2 = SwitchGroup ()
4341
4442 async def load (self ) -> bool :
4543 """Load and activate Switch node features."""
@@ -57,7 +55,8 @@ async def load(self) -> bool:
5755 NodeFeature .BATTERY ,
5856 NodeFeature .INFO ,
5957 NodeFeature .PING ,
60- NodeFeature .SWITCH ,
58+ NodeFeature .SWITCH_GROUP_1 ,
59+ NodeFeature .SWITCH_GROUP_2 ,
6160 ),
6261 )
6362 if await self .initialize ():
@@ -73,7 +72,7 @@ async def initialize(self) -> bool:
7372 return True
7473
7574 self ._switch_subscription = await self ._message_subscribe (
76- self ._switch_group ,
75+ self ._switch_response ,
7776 self ._mac_in_bytes ,
7877 (NODE_SWITCH_GROUP_ID ,),
7978 )
@@ -92,49 +91,48 @@ async def unload(self) -> None:
9291 @raise_not_loaded
9392 def switch (self ) -> bool :
9493 """Current state of switch."""
95- return bool (self ._switch_state )
94+ return bool (self ._switch_group_1 . state )
9695
9796 # endregion
9897
99- async def _switch_group (self , response : PlugwiseResponse ) -> bool :
98+ async def _switch_response (self , response : PlugwiseResponse ) -> bool :
10099 """Switch group request from Switch."""
101100 if not isinstance (response , NodeSwitchGroupResponse ):
102101 raise MessageError (
103102 f"Invalid response message type ({ response .__class__ .__name__ } ) received, expected NodeSwitchGroupResponse"
104103 )
105104 await gather (
106105 self ._available_update_state (True , response .timestamp ),
107- self ._switch_state_update (response .switch_state , response .timestamp ),
106+ self ._switch_state_update (
107+ response .switch_state , response .switch_group , response .timestamp
108+ ),
108109 )
109110 return True
110111
111112 async def _switch_state_update (
112- self , switch_state : bool , timestamp : datetime
113+ self , switch_state : bool , switch_group : int , timestamp : datetime
113114 ) -> None :
114115 """Process switch state update."""
115116 _LOGGER .debug (
116- "_switch_state_update for %s: %s -> %s " ,
117+ "_switch_state_update for %s: %s" ,
117118 self .name ,
118- self ._switch_state ,
119119 switch_state ,
120120 )
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- ]
121+ if switch_group == 1 :
122+ self ._switch_group_1 .state = switch_state
123+ self ._switch_group_1 .group = switch_group
124+ self ._switch_group_1 .timestampe = timestamp
125+
126+ await self .publish_feature_update_to_subscribers (
127+ NodeFeature .SWITCH_GROUP_1 , self ._switch_group_1
128+ )
129+ elif switch_group == 2 :
130+ self ._switch_group_2 .state = switch_state
131+ self ._switch_group_2 .group = switch_group
132+ self ._switch_group_2 .timestampe = timestamp
133+
134+ await self .publish_feature_update_to_subscribers (
135+ NodeFeature .SWITCH_GROUP_2 , self ._switch_group_2
138136 )
139137
140138 @raise_not_loaded
@@ -154,8 +152,10 @@ async def get_state(self, features: tuple[NodeFeature]) -> dict[NodeFeature, Any
154152 )
155153
156154 match feature :
157- case NodeFeature .SWITCH :
158- states [NodeFeature .SWITCH ] = self ._switch_state
155+ case NodeFeature .SWITCH_GROUP_1 :
156+ states [NodeFeature .SWITCH_GROUP_1 ] = self ._switch_group_1
157+ case NodeFeature .SWITCH_GROUP_2 :
158+ states [NodeFeature .SWITCH_GROUP_2 ] = self ._switch_group_2
159159 case _:
160160 state_result = await super ().get_state ((feature ,))
161161 states [feature ] = state_result [feature ]
0 commit comments