Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Modern Forms integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable, Coroutine
6 import logging
7 from typing import Any, Concatenate
8 
9 from aiomodernforms import ModernFormsConnectionError, ModernFormsError
10 
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.const import CONF_HOST, Platform
13 from homeassistant.core import HomeAssistant
14 
15 from .const import DOMAIN
16 from .coordinator import ModernFormsDataUpdateCoordinator
17 from .entity import ModernFormsDeviceEntity
18 
19 PLATFORMS = [
20  Platform.BINARY_SENSOR,
21  Platform.FAN,
22  Platform.LIGHT,
23  Platform.SENSOR,
24  Platform.SWITCH,
25 ]
26 _LOGGER = logging.getLogger(__name__)
27 
28 
29 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
30  """Set up a Modern Forms device from a config entry."""
31 
32  # Create Modern Forms instance for this entry
33  coordinator = ModernFormsDataUpdateCoordinator(hass, host=entry.data[CONF_HOST])
34  await coordinator.async_config_entry_first_refresh()
35 
36  hass.data.setdefault(DOMAIN, {})
37  hass.data[DOMAIN][entry.entry_id] = coordinator
38 
39  # Set up all platforms for this device/entry.
40  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
41 
42  return True
43 
44 
45 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
46  """Unload Modern Forms config entry."""
47  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
48 
49  if unload_ok:
50  del hass.data[DOMAIN][entry.entry_id]
51 
52  if not hass.data[DOMAIN]:
53  del hass.data[DOMAIN]
54 
55  return unload_ok
56 
57 
58 def modernforms_exception_handler[
59  _ModernFormsDeviceEntityT: ModernFormsDeviceEntity,
60  **_P,
61 ](
62  func: Callable[Concatenate[_ModernFormsDeviceEntityT, _P], Any],
63 ) -> Callable[Concatenate[_ModernFormsDeviceEntityT, _P], Coroutine[Any, Any, None]]:
64  """Decorate Modern Forms calls to handle Modern Forms exceptions.
65 
66  A decorator that wraps the passed in function, catches Modern Forms errors,
67  and handles the availability of the device in the data coordinator.
68  """
69 
70  async def handler(
71  self: _ModernFormsDeviceEntityT, *args: _P.args, **kwargs: _P.kwargs
72  ) -> None:
73  try:
74  await func(self, *args, **kwargs)
75  self.coordinator.async_update_listeners()
76 
77  except ModernFormsConnectionError as error:
78  _LOGGER.error("Error communicating with API: %s", error)
79  self.coordinator.last_update_success = False
80  self.coordinator.async_update_listeners()
81 
82  except ModernFormsError as error:
83  _LOGGER.error("Invalid response from API: %s", error)
84 
85  return handler
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:45
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:29