1 """The Modern Forms integration."""
3 from __future__
import annotations
5 from collections.abc
import Callable, Coroutine
7 from typing
import Any, Concatenate
9 from aiomodernforms
import ModernFormsConnectionError, ModernFormsError
15 from .const
import DOMAIN
16 from .coordinator
import ModernFormsDataUpdateCoordinator
17 from .entity
import ModernFormsDeviceEntity
20 Platform.BINARY_SENSOR,
26 _LOGGER = logging.getLogger(__name__)
30 """Set up a Modern Forms device from a config entry."""
34 await coordinator.async_config_entry_first_refresh()
36 hass.data.setdefault(DOMAIN, {})
37 hass.data[DOMAIN][entry.entry_id] = coordinator
40 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
46 """Unload Modern Forms config entry."""
47 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
50 del hass.data[DOMAIN][entry.entry_id]
52 if not hass.data[DOMAIN]:
58 def modernforms_exception_handler[
59 _ModernFormsDeviceEntityT: ModernFormsDeviceEntity,
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.
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.
71 self: _ModernFormsDeviceEntityT, *args: _P.args, **kwargs: _P.kwargs
74 await func(self, *args, **kwargs)
75 self.coordinator.async_update_listeners()
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()
82 except ModernFormsError
as error:
83 _LOGGER.error(
"Invalid response from API: %s", error)