|
| 1 | +"""The corona_hessen component.""" |
| 2 | + |
| 3 | +from datetime import timedelta |
| 4 | +import logging |
| 5 | + |
| 6 | +import async_timeout |
| 7 | +import asyncio |
| 8 | +import bs4 |
| 9 | + |
| 10 | +from homeassistant.config_entries import ConfigEntry |
| 11 | +from homeassistant.core import HomeAssistant, callback |
| 12 | + |
| 13 | +from homeassistant.helpers import aiohttp_client |
| 14 | +from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed |
| 15 | + |
| 16 | +from .const import DOMAIN, ENDPOINT, OPTION_TOTAL |
| 17 | + |
| 18 | +_LOGGER = logging.getLogger(__name__) |
| 19 | + |
| 20 | +PLATFORMS = ["sensor"] |
| 21 | + |
| 22 | +async def async_setup(hass: HomeAssistant, config: dict): |
| 23 | + """Set up the Coronavirus Hessen component.""" |
| 24 | + # Make sure coordinator is initialized. |
| 25 | + await get_coordinator(hass) |
| 26 | + return True |
| 27 | + |
| 28 | + |
| 29 | +async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): |
| 30 | + """Set up Coronavirus Hessen from a config entry.""" |
| 31 | + |
| 32 | + for component in PLATFORMS: |
| 33 | + hass.async_create_task( |
| 34 | + hass.config_entries.async_forward_entry_setup(entry, component) |
| 35 | + ) |
| 36 | + |
| 37 | + return True |
| 38 | + |
| 39 | + |
| 40 | +async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): |
| 41 | + """Unload a config entry.""" |
| 42 | + unload_ok = all( |
| 43 | + await asyncio.gather( |
| 44 | + *[ |
| 45 | + hass.config_entries.async_forward_entry_unload(entry, component) |
| 46 | + for component in PLATFORMS |
| 47 | + ] |
| 48 | + ) |
| 49 | + ) |
| 50 | + |
| 51 | + return unload_ok |
| 52 | + |
| 53 | + |
| 54 | +async def get_coordinator(hass): |
| 55 | + """Get the data update coordinator.""" |
| 56 | + if DOMAIN in hass.data: |
| 57 | + return hass.data[DOMAIN] |
| 58 | + |
| 59 | + async def async_get_data(): |
| 60 | + with async_timeout.timeout(10): |
| 61 | + response = await aiohttp_client.async_get_clientsession(hass).get(ENDPOINT) |
| 62 | + raw_html = await response.text() |
| 63 | + |
| 64 | + data = bs4.BeautifulSoup(raw_html, "html.parser") |
| 65 | + |
| 66 | + result = dict() |
| 67 | + rows = data.select("article table:first-of-type tr") |
| 68 | + |
| 69 | + # Counties |
| 70 | + for row in rows[1:-1]: |
| 71 | + line = row.select("td") |
| 72 | + if len(line) != 4: |
| 73 | + continue |
| 74 | + |
| 75 | + try: |
| 76 | + county = line[0].text.strip() |
| 77 | + cases_str = line[3].text.strip() |
| 78 | + if len(cases_str) and cases_str != "-": |
| 79 | + cases = int(cases_str) |
| 80 | + else: |
| 81 | + cases = 0 |
| 82 | + except ValueError: |
| 83 | + _LOGGER.error("Error processing line {}, skipping".format(line)) |
| 84 | + continue |
| 85 | + result[county] = cases |
| 86 | + |
| 87 | + # Total |
| 88 | + line = rows[-1].select("td") |
| 89 | + try: |
| 90 | + result[OPTION_TOTAL] = int(line[-1].select("p strong")[0].text.strip()) |
| 91 | + except ValueError: |
| 92 | + _LOGGER.error("Error processing total value from {}, skipping".format(line)) |
| 93 | + |
| 94 | + _LOGGER.debug("Corona Hessen: {!r}".format(result)) |
| 95 | + return result |
| 96 | + |
| 97 | + hass.data[DOMAIN] = DataUpdateCoordinator( |
| 98 | + hass, |
| 99 | + logging.getLogger(__name__), |
| 100 | + name=DOMAIN, |
| 101 | + update_method=async_get_data, |
| 102 | + update_interval=timedelta(hours=12), # 12h as the data apparently only updates once per day anyhow |
| 103 | + ) |
| 104 | + await hass.data[DOMAIN].async_refresh() |
| 105 | + return hass.data[DOMAIN] |
0 commit comments