Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """Suez water update coordinator."""
2 
3 from collections.abc import Mapping
4 from dataclasses import dataclass
5 from datetime import date
6 from typing import Any
7 
8 from pysuez import PySuezError, SuezClient
9 
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
12 from homeassistant.core import _LOGGER, HomeAssistant
13 from homeassistant.exceptions import ConfigEntryError
14 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
15 
16 from .const import CONF_COUNTER_ID, DATA_REFRESH_INTERVAL, DOMAIN
17 
18 
19 @dataclass
21  """Class containing aggregated sensor extra attributes."""
22 
23  this_month_consumption: dict[date, float]
24  previous_month_consumption: dict[date, float]
25  last_year_overall: dict[str, float]
26  this_year_overall: dict[str, float]
27  history: dict[date, float]
28  highest_monthly_consumption: float
29 
30 
31 @dataclass
33  """Class used to hold all fetch data from suez api."""
34 
35  aggregated_value: float
36  aggregated_attr: Mapping[str, Any]
37  price: float
38 
39 
41  """Suez water coordinator."""
42 
43  _suez_client: SuezClient
44  config_entry: ConfigEntry
45 
46  def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None:
47  """Initialize suez water coordinator."""
48  super().__init__(
49  hass,
50  _LOGGER,
51  name=DOMAIN,
52  update_interval=DATA_REFRESH_INTERVAL,
53  always_update=True,
54  config_entry=config_entry,
55  )
56 
57  async def _async_setup(self) -> None:
58  self._suez_client_suez_client = SuezClient(
59  username=self.config_entryconfig_entry.data[CONF_USERNAME],
60  password=self.config_entryconfig_entry.data[CONF_PASSWORD],
61  counter_id=self.config_entryconfig_entry.data[CONF_COUNTER_ID],
62  )
63  if not await self._suez_client_suez_client.check_credentials():
64  raise ConfigEntryError("Invalid credentials for suez water")
65 
66  async def _async_update_data(self) -> SuezWaterData:
67  """Fetch data from API endpoint."""
68  try:
69  aggregated = await self._suez_client_suez_client.fetch_aggregated_data()
70  data = SuezWaterData(
71  aggregated_value=aggregated.value,
72  aggregated_attr={
73  "this_month_consumption": aggregated.current_month,
74  "previous_month_consumption": aggregated.previous_month,
75  "highest_monthly_consumption": aggregated.highest_monthly_consumption,
76  "last_year_overall": aggregated.previous_year,
77  "this_year_overall": aggregated.current_year,
78  "history": aggregated.history,
79  },
80  price=(await self._suez_client_suez_client.get_price()).price,
81  )
82  except PySuezError as err:
83  _LOGGER.exception(err)
84  raise UpdateFailed(
85  f"Suez coordinator error communicating with API: {err}"
86  ) from err
87  _LOGGER.debug("Successfully fetched suez data")
88  return data
None __init__(self, HomeAssistant hass, ConfigEntry config_entry)
Definition: coordinator.py:46