Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """Smarty Coordinator."""
2 
3 from datetime import timedelta
4 import logging
5 
6 from pysmarty2 import Smarty
7 
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.const import CONF_HOST
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
12 
13 _LOGGER = logging.getLogger(__name__)
14 
15 type SmartyConfigEntry = ConfigEntry[SmartyCoordinator]
16 
17 
19  """Smarty Coordinator."""
20 
21  config_entry: SmartyConfigEntry
22  software_version: str
23  configuration_version: str
24 
25  def __init__(self, hass: HomeAssistant) -> None:
26  """Initialize."""
27  super().__init__(
28  hass,
29  logger=_LOGGER,
30  name="Smarty",
31  update_interval=timedelta(seconds=30),
32  )
33  self.clientclient = Smarty(host=self.config_entryconfig_entry.data[CONF_HOST])
34 
35  async def _async_setup(self) -> None:
36  if not await self.hasshass.async_add_executor_job(self.clientclient.update):
37  raise UpdateFailed("Failed to update Smarty data")
38  self.software_versionsoftware_version = self.clientclient.get_software_version()
39  self.configuration_versionconfiguration_version = self.clientclient.get_configuration_version()
40 
41  async def _async_update_data(self) -> None:
42  """Fetch data from Smarty."""
43  if not await self.hasshass.async_add_executor_job(self.clientclient.update):
44  raise UpdateFailed("Failed to update Smarty data")