Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Steamist integration."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 from typing import Any
7 
8 from aiosteamist import Steamist
9 
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import CONF_HOST, CONF_NAME, Platform
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers import config_validation as cv
14 from homeassistant.helpers.aiohttp_client import async_get_clientsession
15 from homeassistant.helpers.event import async_track_time_interval
16 from homeassistant.helpers.typing import ConfigType
17 
18 from .const import DISCOVER_SCAN_TIMEOUT, DISCOVERY, DOMAIN
19 from .coordinator import SteamistDataUpdateCoordinator
20 from .discovery import (
21  async_discover_device,
22  async_discover_devices,
23  async_get_discovery,
24  async_trigger_discovery,
25  async_update_entry_from_discovery,
26 )
27 
28 PLATFORMS: list[str] = [Platform.SENSOR, Platform.SWITCH]
29 DISCOVERY_INTERVAL = timedelta(minutes=15)
30 CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
31 
32 
33 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
34  """Set up the steamist component."""
35  domain_data = hass.data.setdefault(DOMAIN, {})
36  domain_data[DISCOVERY] = []
37 
38  async def _async_discovery(*_: Any) -> None:
40  hass, await async_discover_devices(hass, DISCOVER_SCAN_TIMEOUT)
41  )
42 
43  hass.async_create_background_task(
44  _async_discovery(), "steamist-discovery", eager_start=True
45  )
46  async_track_time_interval(hass, _async_discovery, DISCOVERY_INTERVAL)
47  return True
48 
49 
50 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
51  """Set up Steamist from a config entry."""
52  host = entry.data[CONF_HOST]
53  coordinator = SteamistDataUpdateCoordinator(
54  hass,
55  Steamist(host, async_get_clientsession(hass)),
56  host,
57  entry.data.get(CONF_NAME), # Only found from discovery
58  )
59  await coordinator.async_config_entry_first_refresh()
60  if not async_get_discovery(hass, host):
61  if discovery := await async_discover_device(hass, host):
62  async_update_entry_from_discovery(hass, entry, discovery)
63  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
64  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
65  return True
66 
67 
68 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
69  """Unload a config entry."""
70  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
71  hass.data[DOMAIN].pop(entry.entry_id)
72  return unload_ok
ElkSystem|None async_discover_device(HomeAssistant hass, str host)
Definition: discovery.py:78
bool async_update_entry_from_discovery(HomeAssistant hass, config_entries.ConfigEntry entry, ElkSystem device)
Definition: discovery.py:30
FluxLEDDiscovery|None async_get_discovery(HomeAssistant hass, str host)
Definition: discovery.py:154
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:33
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:68
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:50
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)
CALLBACK_TYPE async_track_time_interval(HomeAssistant hass, Callable[[datetime], Coroutine[Any, Any, None]|None] action, timedelta interval, *str|None name=None, bool|None cancel_on_shutdown=None)
Definition: event.py:1679