Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """Data update coordinator for the Dremel 3D Printer integration."""
2 
3 from datetime import timedelta
4 
5 from dremel3dpy import Dremel3DPrinter
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.core import HomeAssistant
9 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
10 
11 from .const import DOMAIN, LOGGER
12 
13 type DremelConfigEntry = ConfigEntry[Dremel3DPrinterDataUpdateCoordinator]
14 
15 
17  """Class to manage fetching Dremel 3D Printer data."""
18 
19  config_entry: DremelConfigEntry
20 
21  def __init__(self, hass: HomeAssistant, api: Dremel3DPrinter) -> None:
22  """Initialize Dremel 3D Printer data update coordinator."""
23  super().__init__(
24  hass=hass,
25  logger=LOGGER,
26  name=DOMAIN,
27  update_interval=timedelta(seconds=10),
28  )
29  self.apiapi = api
30 
31  async def _async_update_data(self) -> None:
32  """Update data via APIs."""
33  try:
34  await self.hasshass.async_add_executor_job(self.apiapi.refresh)
35  except RuntimeError as ex:
36  raise UpdateFailed(
37  f"Unable to refresh printer information: Printer offline: {ex}"
38  ) from ex