30
30
class BaseWeatherFlowCoordinator [T ](DataUpdateCoordinator [dict [int , T ]]):
31
31
"""Base class for WeatherFlow coordinators."""
32
32
33
- # Define static variables
34
- # _session: ClientSession
35
- # _ssl_context: SSLContext
36
- # _rest_api: WeatherFlowRestAPI
37
- # stations: StationsResponseREST | None = None
38
- # device_to_station_map: dict[int, int] = {}
39
- # device_ids: list[int] = []
40
-
41
33
def __init__ (
42
34
self ,
43
35
hass : HomeAssistant ,
@@ -50,28 +42,12 @@ def __init__(
50
42
self ._token = rest_api .api_token
51
43
self ._rest_api = rest_api
52
44
self .stations = stations
53
- LOGGER .error (f"🌮️ { self } " )
54
- LOGGER .error (f"🌮️ { dir (stations )} " )
55
- LOGGER .error (f"🌮️ { stations } " )
56
- LOGGER .error (f"🌮️ { stations } " )
57
45
self .device_to_station_map = stations .device_station_map
58
46
59
47
self .device_ids = list (stations .device_station_map .keys ())
60
48
61
49
self ._ssl_context = client_context ()
62
50
63
- # # Use these variables as static
64
- # if not hasattr(BaseWeatherFlowCoordinator, "_session"):
65
- # BaseWeatherFlowCoordinator._session = async_get_clientsession(hass)
66
- # if not hasattr(BaseWeatherFlowCoordinator, "_ssl_context"):
67
- # BaseWeatherFlowCoordinator._ssl_context = client_context()
68
- #
69
- # # Initialize the API once
70
- # if not hasattr(BaseWeatherFlowCoordinator, "_rest_api"):
71
- # BaseWeatherFlowCoordinator._rest_api = WeatherFlowRestAPI(
72
- # api_token=self._token, session=BaseWeatherFlowCoordinator._session
73
- # )
74
-
75
51
super ().__init__ (
76
52
hass ,
77
53
LOGGER ,
@@ -130,7 +106,13 @@ class WeatherFlowCloudDataCallbackCoordinator[
130
106
M : RapidWindListenStartMessage | ListenStartMessage ,
131
107
C : RapidWindWS | ObservationTempestWS ,
132
108
](BaseWeatherFlowCoordinator [dict [int , T | None ]]):
133
- """A Generic coordinator to handle Websocket connections."""
109
+ """A Generic coordinator to handle Websocket connections.
110
+
111
+ This class takes 3 generics - T, M, and C.
112
+ T - The type of data that will be stored in the coordinator.
113
+ M - The type of message that will be sent to the websocket API.
114
+ C - The type of message that will be received from the websocket API.
115
+ """
134
116
135
117
def __init__ (
136
118
self ,
@@ -149,7 +131,7 @@ def __init__(
149
131
self .websocket_api = websocket_api
150
132
self ._listen_request_type = listen_request_type
151
133
152
- # pre-initialize ws data
134
+ # configure the websocket data structure
153
135
self ._ws_data : dict [int , dict [int , T | None ]] = {
154
136
station : {device : None for device in devices }
155
137
for station , devices in self .stations .station_device_map .items ()
@@ -162,7 +144,7 @@ async def _generic_callback(self, data: C):
162
144
self .async_set_updated_data (self ._ws_data )
163
145
164
146
async def _async_setup (self ) -> None :
165
- LOGGER . error ( f"Setup { self . __class__ . __name__ } with token: { self . _token } " )
147
+ # Open the websocket connection
166
148
await self .websocket_api .connect (self ._ssl_context )
167
149
# Register callback
168
150
self .websocket_api ._register_callback ( # noqa:SLF001
@@ -174,7 +156,6 @@ async def _async_setup(self) -> None:
174
156
await self .websocket_api .send_message (
175
157
self ._listen_request_type (device_id = str (device_id ))
176
158
)
177
- LOGGER .error (f"Sending listen request for { self .__class__ .__name__ } " )
178
159
179
160
def get_station (self , station_id : int ):
180
161
"""Return station for id."""
@@ -185,97 +166,3 @@ def get_station_name(self, station_id: int) -> str:
185
166
if name := self .stations .station_map [station_id ].name :
186
167
return name
187
168
return ""
188
-
189
- #
190
- # class WeatherFlowCloudDataUpdateCoordinatorObservation(
191
- # BaseWeatherFlowCoordinator[dict[int, WebsocketObservation]]
192
- # ):
193
- # """A Generic coordinator to handle Websocket connections."""
194
- #
195
- # def __init__(
196
- # self,
197
- # hass: HomeAssistant,
198
- # token: str,
199
- # stations: StationsResponseREST,
200
- # websocket_api: WeatherFlowWebsocketAPI,
201
- # ) -> None:
202
- # """Initialize Coordinator."""
203
- # super().__init__(hass=hass, token=token)
204
- # self.stations = stations
205
- # self.websocket_api = websocket_api
206
- #
207
- # self._ws_data: dict[int, dict[int, WebsocketObservation]] = {
208
- # station: {device: None for device in devices}
209
- # for station, devices in self.stations.station_device_map.items()
210
- # }
211
- #
212
- #
213
- #
214
- # async def _observation_callback(self, data: WebsocketObservation):
215
- # """Define callback for observation events."""
216
- # device_id = data.device_id
217
- # station_id = self.device_to_station_map[device_id]
218
- # self._ws_data[station_id][device_id] = data
219
- # LOGGER.debug(f"Updated Observation Data for: {station_id}:{device_id} = {data}")
220
- # self.async_set_updated_data(self._ws_data)
221
- #
222
- # # async def _async_setup(self) -> None:
223
- # # """Set up the coordinator."""
224
- # # LOGGER.debug(f"Setup {self.__class__.__name__} with token: {self.token}")
225
- # #
226
- #
227
- # async def _async_setup(self) -> None:
228
- # await self.api.connect(self._ssl_context)
229
- #
230
- # # Register callback
231
- # self.api.register_observation_callback(self._observation_callback)
232
-
233
-
234
- # # Subscribe to messages
235
- # for device_id in self.device_ids:
236
- # await self.api.send_message(ListenStartMessage(device_id=str(device_id)))
237
- #
238
- #
239
- # class WeatherFlowCloudDataCoordinatorWind(
240
- # BaseWeatherFlowCoordinator[dict[int, EventDataRapidWind]]
241
- # ):
242
- # """Websocket coordinator for wind."""
243
- #
244
- # def __init__(
245
- # self,
246
- # hass: HomeAssistant,
247
- # token: str,
248
- # # Optional Fields
249
- # stations: StationsResponseREST,
250
- # websocket_api: WeatherFlowWebsocketAPI,
251
- # ) -> None:
252
- # """Initialize Coordinator."""
253
- # super().__init__(hass=hass, token=token)
254
- #
255
- # self.stations = stations
256
- # self.websocket_api = websocket_api
257
- #
258
- # self._ws_data: dict[int, dict[int, EventDataRapidWind]] = {
259
- # station: {device: None for device in devices}
260
- # for station, devices in self.stations.station_device_map.items()
261
- # }
262
- #
263
- # async def _rapid_wind_callback(self, data: RapidWindWS):
264
- # """Define callback for wind events."""
265
- # device_id = data.device_id
266
- # station_id = self.device_to_station_map[device_id]
267
- # self._ws_data[station_id][device_id] = data.ob
268
- # LOGGER.debug(f"Updated Wind Data for: {station_id}:{device_id} = {data.ob}")
269
- # self.async_set_updated_data(self._ws_data)
270
- #
271
- # async def _async_setup(self) -> None:
272
- # # Connect the socket -> this is likely duplicate code we should fix
273
- # await self.api.connect(self._ssl_context)
274
- # # Register callback
275
- # self.api.register_wind_callback(self._rapid_wind_callback)
276
- # # Send listen Request
277
- # for device_id in self.device_ids:
278
- # await self.api.send_message(
279
- # RapidWindListenStartMessage(device_id=str(device_id))
280
- # )
281
- #
0 commit comments