1 """The AirVisual Pro integration."""
3 from __future__
import annotations
6 from contextlib
import suppress
7 from dataclasses
import dataclass
8 from datetime
import timedelta
11 from pyairvisual.node
import (
12 InvalidAuthenticationError,
22 EVENT_HOMEASSISTANT_STOP,
29 from .const
import LOGGER
31 PLATFORMS = [Platform.SENSOR]
35 type AirVisualProConfigEntry = ConfigEntry[AirVisualProData]
40 """Define a data class."""
42 coordinator: DataUpdateCoordinator
47 hass: HomeAssistant, entry: AirVisualProConfigEntry
49 """Set up AirVisual Pro from a config entry."""
50 node = NodeSamba(entry.data[CONF_IP_ADDRESS], entry.data[CONF_PASSWORD])
53 await node.async_connect()
54 except NodeProError
as err:
55 raise ConfigEntryNotReady
from err
57 reload_task: asyncio.Task |
None =
None
60 """Get data from the device."""
62 data = await node.async_get_latest_measurements()
64 if data[
"settings"].
get(
"follow_mode") ==
"device":
65 history = await node.async_get_history(include_trends=
False)
66 data[
"history"] = history.get(
"measurements", [])[-1]
67 except InvalidAuthenticationError
as err:
69 except NodeConnectionError
as err:
72 reload_task = hass.async_create_task(
73 hass.config_entries.async_reload(entry.entry_id)
75 raise UpdateFailed(f
"Connection to Pro unit lost: {err}")
from err
76 except NodeProError
as err:
77 raise UpdateFailed(f
"Error while retrieving data: {err}")
from err
86 update_interval=UPDATE_INTERVAL,
87 update_method=async_get_data,
90 await coordinator.async_config_entry_first_refresh()
93 async
def async_shutdown(_: Event) ->
None:
94 """Define an event handler to disconnect from the websocket."""
97 with suppress(asyncio.CancelledError):
99 await node.async_disconnect()
101 entry.async_on_unload(
102 hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_shutdown)
105 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
111 hass: HomeAssistant, entry: AirVisualProConfigEntry
113 """Unload a config entry."""
114 if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
115 await entry.runtime_data.node.async_disconnect()
bool async_unload_entry(HomeAssistant hass, AirVisualProConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, AirVisualProConfigEntry entry)
web.Response get(self, web.Request request, str config_key)
RadioThermUpdate async_get_data(HomeAssistant hass, CommonThermostat device)