Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """Coordinator for the vizio component."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 import logging
7 from typing import Any
8 
9 from pyvizio.const import APPS
10 from pyvizio.util import gen_apps_list_from_url
11 
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.aiohttp_client import async_get_clientsession
14 from homeassistant.helpers.storage import Store
15 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
16 
17 from .const import DOMAIN
18 
19 _LOGGER = logging.getLogger(__name__)
20 
21 
23  """Define an object to hold Vizio app config data."""
24 
25  def __init__(self, hass: HomeAssistant, store: Store[list[dict[str, Any]]]) -> None:
26  """Initialize."""
27  super().__init__(
28  hass,
29  _LOGGER,
30  name=DOMAIN,
31  update_interval=timedelta(days=1),
32  )
33  self.fail_countfail_count = 0
34  self.fail_thresholdfail_threshold = 10
35  self.storestore = store
36 
37  async def _async_setup(self) -> None:
38  """Refresh data for the first time when a config entry is setup."""
39  self.datadatadata = await self.storestore.async_load() or APPS
40 
41  async def _async_update_data(self) -> list[dict[str, Any]]:
42  """Update data via library."""
43  if data := await gen_apps_list_from_url(
44  session=async_get_clientsession(self.hasshass)
45  ):
46  # Reset the fail count and threshold when the data is successfully retrieved
47  self.fail_countfail_count = 0
48  self.fail_thresholdfail_threshold = 10
49  # Store the new data if it has changed so we have it for the next restart
50  if data != self.datadatadata:
51  await self.storestore.async_save(data)
52  return data
53  # For every failure, increase the fail count until we reach the threshold.
54  # We then log a warning, increase the threshold, and reset the fail count.
55  # This is here to prevent silent failures but to reduce repeat logs.
56  if self.fail_countfail_count == self.fail_thresholdfail_threshold:
57  _LOGGER.warning(
58  (
59  "Unable to retrieve the apps list from the external server for the "
60  "last %s days"
61  ),
62  self.fail_thresholdfail_threshold,
63  )
64  self.fail_countfail_count = 0
65  self.fail_thresholdfail_threshold += 10
66  else:
67  self.fail_countfail_count += 1
68  return self.datadatadata
None __init__(self, HomeAssistant hass, Store[list[dict[str, Any]]] store)
Definition: coordinator.py:25
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)
None async_load(HomeAssistant hass)
None async_save(self, _T data)
Definition: storage.py:424