1 """DataUpdateCoordinator for solarlog integration."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from datetime
import timedelta
8 from typing
import TYPE_CHECKING
9 from urllib.parse
import ParseResult, urlparse
11 from solarlog_cli.solarlog_connector
import SolarLogConnector
12 from solarlog_cli.solarlog_exceptions
import (
13 SolarLogAuthenticationError,
14 SolarLogConnectionError,
17 from solarlog_cli.solarlog_models
import SolarlogData
27 from .const
import DOMAIN
29 _LOGGER = logging.getLogger(__name__)
32 from .
import SolarlogConfigEntry
36 """Get and update the latest data."""
38 def __init__(self, hass: HomeAssistant, entry: SolarlogConfigEntry) ->
None:
39 """Initialize the data object."""
41 hass, _LOGGER, name=
"SolarLog", update_interval=
timedelta(seconds=60)
44 self.new_device_callbacks: list[Callable[[int],
None]] = []
47 host_entry = entry.data[CONF_HOST]
48 password = entry.data.get(
"password",
"")
50 url = urlparse(host_entry,
"http")
51 netloc = url.netloc
or url.path
52 path = url.path
if url.netloc
else ""
53 url = ParseResult(
"http", netloc, path, *url[3:])
56 self.
hosthost = url.geturl()
60 tz=hass.config.time_zone,
66 """Do initialization logic."""
67 _LOGGER.debug(
"Start async_setup")
69 if self.
solarlogsolarlog.password !=
"":
71 await self.
solarlogsolarlog.test_extended_data_available()
72 if logged_in
or await self.
solarlogsolarlog.test_extended_data_available():
73 device_list = await self.
solarlogsolarlog.update_device_list()
74 self.
solarlogsolarlog.set_enabled_devices({key:
True for key
in device_list})
77 """Update the data from the SolarLog device."""
78 _LOGGER.debug(
"Start data update")
81 data = await self.
solarlogsolarlog.update_data()
82 if self.
solarlogsolarlog.extended_data:
83 await self.
solarlogsolarlog.update_device_list()
84 data.inverter_data = await self.
solarlogsolarlog.update_inverter_data()
85 except SolarLogConnectionError
as ex:
87 translation_domain=DOMAIN,
88 translation_key=
"config_entry_not_ready",
90 except SolarLogAuthenticationError
as ex:
93 await self.
solarlogsolarlog.test_extended_data_available()
95 translation_domain=DOMAIN,
96 translation_key=
"config_entry_not_ready",
99 translation_domain=DOMAIN,
100 translation_key=
"auth_failed",
102 except SolarLogUpdateError
as ex:
104 translation_domain=DOMAIN,
105 translation_key=
"update_failed",
108 _LOGGER.debug(
"Data successfully updated")
110 if self.
solarlogsolarlog.extended_data:
112 _LOGGER.debug(
"Add_remove_devices finished")
117 """Add new devices, remove non-existing devices."""
127 _LOGGER.debug(
"Removed device(s): %s",
", ".join(map(str, removed_devices)))
128 device_registry = dr.async_get(self.
hasshass)
130 for removed_device
in removed_devices:
133 if did == removed_device[0]:
136 if device := device_registry.async_get_device(
140 f
"{self.unique_id}_{slugify(device_name)}",
144 device_registry.async_update_device(
146 remove_config_entry_id=self.
unique_idunique_id,
148 _LOGGER.debug(
"Device removed from device registry: %s", device.id)
152 _LOGGER.debug(
"New device(s) found: %s",
", ".join(map(str, new_devices)))
153 for device_id
in new_devices:
154 for callback
in self.new_device_callbacks:
155 callback(device_id[0])
160 """Renew access token for SolarLog API."""
163 logged_in = await self.
solarlogsolarlog.login()
164 except SolarLogAuthenticationError
as ex:
166 translation_domain=DOMAIN,
167 translation_key=
"auth_failed",
169 except (SolarLogConnectionError, SolarLogUpdateError)
as ex:
171 translation_domain=DOMAIN,
172 translation_key=
"config_entry_not_ready",
175 _LOGGER.debug(
"Credentials successfully updated? %s", logged_in)
None __init__(self, HomeAssistant hass, SolarlogConfigEntry entry)
None _async_add_remove_devices(self, SolarlogData data)
bool renew_authentication(self)
SolarlogData _async_update_data(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)