Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Vilfo Router integration."""
2 
3 from datetime import timedelta
4 import logging
5 
6 from vilfo import Client as VilfoClient
7 from vilfo.exceptions import VilfoException
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST, Platform
11 from homeassistant.core import HomeAssistant
12 from homeassistant.exceptions import ConfigEntryNotReady
13 from homeassistant.util import Throttle
14 
15 from .const import ATTR_BOOT_TIME, ATTR_LOAD, DOMAIN, ROUTER_DEFAULT_HOST
16 
17 PLATFORMS = [Platform.SENSOR]
18 
19 DEFAULT_SCAN_INTERVAL = timedelta(seconds=30)
20 
21 _LOGGER = logging.getLogger(__name__)
22 
23 
24 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
25  """Set up Vilfo Router from a config entry."""
26  host = entry.data[CONF_HOST]
27  access_token = entry.data[CONF_ACCESS_TOKEN]
28 
29  vilfo_router = VilfoRouterData(hass, host, access_token)
30 
31  await vilfo_router.async_update()
32 
33  if not vilfo_router.available:
34  raise ConfigEntryNotReady
35 
36  hass.data.setdefault(DOMAIN, {})
37  hass.data[DOMAIN][entry.entry_id] = vilfo_router
38 
39  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
40 
41  return True
42 
43 
44 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
45  """Unload a config entry."""
46  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
47  if unload_ok:
48  hass.data[DOMAIN].pop(entry.entry_id)
49 
50  return unload_ok
51 
52 
54  """Define an object to hold sensor data."""
55 
56  def __init__(self, hass, host, access_token):
57  """Initialize."""
58  self._vilfo_vilfo = VilfoClient(host, access_token)
59  self.hasshass = hass
60  self.hosthost = host
61  self.availableavailable = False
62  self.firmware_versionfirmware_version = None
63  self.mac_addressmac_address = self._vilfo_vilfo.mac
64  self.datadata = {}
65  self._unavailable_logged_unavailable_logged = False
66 
67  @property
68  def unique_id(self):
69  """Get the unique_id for the Vilfo Router."""
70  if self.mac_addressmac_address:
71  return self.mac_addressmac_address
72 
73  if self.hosthost == ROUTER_DEFAULT_HOST:
74  return self.hosthost
75 
76  return self.hosthost
77 
78  def _fetch_data(self):
79  board_information = self._vilfo_vilfo.get_board_information()
80  load = self._vilfo_vilfo.get_load()
81 
82  return {
83  "board_information": board_information,
84  "load": load,
85  }
86 
87  @Throttle(DEFAULT_SCAN_INTERVAL)
88  async def async_update(self):
89  """Update data using calls to VilfoClient library."""
90  try:
91  data = await self.hasshass.async_add_executor_job(self._fetch_data_fetch_data)
92 
93  self.firmware_versionfirmware_version = data["board_information"]["version"]
94  self.datadata[ATTR_BOOT_TIME] = data["board_information"]["bootTime"]
95  self.datadata[ATTR_LOAD] = data["load"]
96 
97  self.availableavailable = True
98  except VilfoException as error:
99  if not self._unavailable_logged_unavailable_logged:
100  _LOGGER.error(
101  "Could not fetch data from %s, error: %s", self.hosthost, error
102  )
103  self._unavailable_logged_unavailable_logged = True
104  self.availableavailable = False
105  return
106 
107  if self.availableavailable and self._unavailable_logged_unavailable_logged:
108  _LOGGER.warning("Vilfo Router %s is available again", self.hosthost)
109  self._unavailable_logged_unavailable_logged = False
def __init__(self, hass, host, access_token)
Definition: __init__.py:56
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:44
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:24