Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The SiteSage Emonitor integration."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 import logging
7 
8 from aioemonitor import Emonitor
9 from aioemonitor.monitor import EmonitorStatus
10 
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.const import CONF_HOST, Platform
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers import aiohttp_client
15 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
16 
17 type EmonitorConfigEntry = ConfigEntry[DataUpdateCoordinator[EmonitorStatus]]
18 
19 _LOGGER = logging.getLogger(__name__)
20 
21 DEFAULT_UPDATE_RATE = 60
22 
23 PLATFORMS = [Platform.SENSOR]
24 
25 
26 async def async_setup_entry(hass: HomeAssistant, entry: EmonitorConfigEntry) -> bool:
27  """Set up SiteSage Emonitor from a config entry."""
28  session = aiohttp_client.async_get_clientsession(hass)
29  emonitor = Emonitor(entry.data[CONF_HOST], session)
30 
31  coordinator = DataUpdateCoordinator[EmonitorStatus](
32  hass,
33  _LOGGER,
34  config_entry=entry,
35  name=entry.title,
36  update_method=emonitor.async_get_status,
37  update_interval=timedelta(seconds=DEFAULT_UPDATE_RATE),
38  always_update=False,
39  )
40 
41  await coordinator.async_config_entry_first_refresh()
42  entry.runtime_data = coordinator
43 
44  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
45  return True
46 
47 
48 async def async_unload_entry(hass: HomeAssistant, entry: EmonitorConfigEntry) -> bool:
49  """Unload a config entry."""
50  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
51 
52 
53 def name_short_mac(short_mac):
54  """Name from short mac."""
55  return f"Emonitor {short_mac}"
bool async_unload_entry(HomeAssistant hass, EmonitorConfigEntry entry)
Definition: __init__.py:48
bool async_setup_entry(HomeAssistant hass, EmonitorConfigEntry entry)
Definition: __init__.py:26