1 """DataUpdateCoordinator for System Bridge."""
3 from __future__
import annotations
5 from asyncio
import Task
6 from collections.abc
import Callable
7 from datetime
import timedelta
11 from systembridgeconnector.exceptions
import (
12 AuthenticationException,
13 ConnectionClosedException,
14 ConnectionErrorException,
16 from systembridgeconnector.websocket_client
import WebSocketClient
17 from systembridgemodels.modules
import (
29 EVENT_HOMEASSISTANT_STOP,
36 from .const
import DOMAIN, MODULES
37 from .data
import SystemBridgeData
41 """Class to manage fetching System Bridge data from single endpoint."""
46 LOGGER: logging.Logger,
50 """Initialize global System Bridge data updater."""
52 self.
unsubunsub: Callable |
None =
None
56 api_host=entry.data[CONF_HOST],
57 api_port=entry.data[CONF_PORT],
58 token=entry.data[CONF_TOKEN],
60 can_close_session=
False,
63 self.
_host_host = entry.data[CONF_HOST]
75 """Check if WebSocket is connected."""
77 "[check_websocket_connected] WebSocket connected: %s",
84 except ConnectionErrorException
as exception:
86 "[check_websocket_connected] Connection error occurred for %s: %s",
93 """Close WebSocket connection."""
97 msg=
"WebSocket closed on Home Assistant shutdown",
101 """Clean disconnect WebSocket."""
109 msg=
"WebSocket disconnected",
114 modules: list[Module],
116 """Get data from WebSocket."""
119 modules_data = await self.
websocket_clientwebsocket_client.get_data(GetData(modules=modules))
122 for module
in MODULES:
123 if hasattr(modules_data, module):
124 self.
loggerlogger.debug(
"[async_get_data] Set new data for: %s", module)
125 setattr(self.
datadatadata, module, getattr(modules_data, module))
134 """Handle data from the WebSocket client."""
135 self.
loggerlogger.debug(
"[async_handle_module] Set new data for: %s", module_name)
136 setattr(self.
datadatadata, module_name, module)
140 """Listen for events from the WebSocket."""
143 except AuthenticationException
as exception:
145 "Authentication failed while listening for %s: %s",
150 except (ConnectionClosedException, ConnectionResetError)
as exception:
152 "[_listen_for_data] Websocket connection closed for %s: %s",
157 except ConnectionErrorException
as exception:
159 "[_listen_for_data] Connection error occurred for %s: %s",
166 """Update System Bridge data from WebSocket."""
170 self.
loggerlogger.debug(
"Create listener task for %s", self.
titletitle)
173 name=
"System Bridge WebSocket Listener",
176 self.
loggerlogger.debug(
"Listening for data from %s", self.
titletitle)
180 RegisterDataListener(modules=MODULES)
182 except AuthenticationException
as exception:
184 "Authentication failed at setup for %s: %s", self.
titletitle, exception
187 raise ConfigEntryAuthFailed
from exception
188 except (ConnectionClosedException, ConnectionErrorException)
as exception:
189 self.
loggerlogger.warning(
190 "[register] Connection error occurred for %s: %s",
197 self.
loggerlogger.debug(
"Registered data listener for %s", self.
titletitle)
203 self.
unsubunsub = self.
hasshass.bus.async_listen_once(
204 EVENT_HOMEASSISTANT_STOP,
208 self.
loggerlogger.debug(
"[_async_update_data] Done")
None close_websocket(self)
None clean_disconnect(self)
None async_handle_module(self, str module_name, Any module)
ModulesData async_get_data(self, list[Module] modules)
None check_websocket_connected(self)
None _listen_for_data(self)
None __init__(self, HomeAssistant hass, logging.Logger LOGGER, *ConfigEntry entry)
SystemBridgeData _async_update_data(self)
None async_set_updated_data(self, _DataT data)
None async_update_listeners(self)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)