Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """Coordinator for the scrape component."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 import logging
7 
8 from bs4 import BeautifulSoup
9 
10 from homeassistant.components.rest import RestData
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
14 
15 _LOGGER = logging.getLogger(__name__)
16 
17 
19  """Scrape Coordinator."""
20 
21  def __init__(
22  self,
23  hass: HomeAssistant,
24  config_entry: ConfigEntry | None,
25  rest: RestData,
26  update_interval: timedelta,
27  ) -> None:
28  """Initialize Scrape coordinator."""
29  super().__init__(
30  hass,
31  _LOGGER,
32  config_entry=config_entry,
33  name="Scrape Coordinator",
34  update_interval=update_interval,
35  )
36  self._rest_rest = rest
37 
38  async def _async_update_data(self) -> BeautifulSoup:
39  """Fetch data from Rest."""
40  await self._rest_rest.async_update()
41  if (data := self._rest_rest.data) is None:
42  raise UpdateFailed("REST data is not available")
43  soup = await self.hasshass.async_add_executor_job(BeautifulSoup, data, "lxml")
44  _LOGGER.debug("Raw beautiful soup: %s", soup)
45  return soup
None __init__(self, HomeAssistant hass, ConfigEntry|None config_entry, RestData rest, timedelta update_interval)
Definition: coordinator.py:27