Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The radiotherm component."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Coroutine
6 from typing import Any
7 from urllib.error import URLError
8 
9 from radiotherm.validate import RadiothermTstatError
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.exceptions import ConfigEntryNotReady
15 
16 from .const import DOMAIN
17 from .coordinator import RadioThermUpdateCoordinator
18 from .data import async_get_init_data
19 from .util import async_set_time
20 
21 PLATFORMS: list[Platform] = [Platform.CLIMATE, Platform.SWITCH]
22 
23 
24 async def _async_call_or_raise_not_ready[_T](
25  coro: Coroutine[Any, Any, _T], host: str
26 ) -> _T:
27  """Call a coro or raise ConfigEntryNotReady."""
28  try:
29  return await coro
30  except RadiothermTstatError as ex:
31  msg = f"{host} was busy (invalid value returned): {ex}"
32  raise ConfigEntryNotReady(msg) from ex
33  except TimeoutError as ex:
34  msg = f"{host} timed out waiting for a response: {ex}"
35  raise ConfigEntryNotReady(msg) from ex
36  except (OSError, URLError) as ex:
37  msg = f"{host} connection error: {ex}"
38  raise ConfigEntryNotReady(msg) from ex
39 
40 
41 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
42  """Set up Radio Thermostat from a config entry."""
43  host = entry.data[CONF_HOST]
44  init_coro = async_get_init_data(hass, host)
45  init_data = await _async_call_or_raise_not_ready(init_coro, host)
46  coordinator = RadioThermUpdateCoordinator(hass, init_data)
47  await coordinator.async_config_entry_first_refresh()
48 
49  # Only set the time if the thermostat is
50  # not in hold mode since setting the time
51  # clears the hold for some strange design
52  # choice
53  if not coordinator.data.tstat["hold"]:
54  time_coro = async_set_time(hass, init_data.tstat)
55  await _async_call_or_raise_not_ready(time_coro, host)
56 
57  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
58  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
59  entry.async_on_unload(entry.add_update_listener(_async_update_listener))
60 
61  return True
62 
63 
64 async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
65  """Handle options update."""
66  await hass.config_entries.async_reload(entry.entry_id)
67 
68 
69 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
70  """Unload a config entry."""
71  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
72  hass.data[DOMAIN].pop(entry.entry_id)
73 
74  return unload_ok
RadioThermInitData async_get_init_data(HomeAssistant hass, str host)
Definition: data.py:50
None async_set_time(HomeAssistant hass, CommonThermostat device)
Definition: util.py:11
None _async_update_listener(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:64
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:41
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:69