-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
119 lines (92 loc) · 3.75 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""
Custom integration to integrate Rituals Genie with Home Assistant.
For more details about this integration, please refer to
https://github.com/fred-oranje/rituals-genie
"""
import asyncio
import logging
from datetime import timedelta
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import Config
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.helpers.update_coordinator import UpdateFailed
from .api import RitualsGenieApiClient
from .const import CONF_FILL_SENSOR_ENABLED
from .const import CONF_HUB_HASH
from .const import CONF_PERFUME_SENSOR_ENABLED
from .const import CONF_SWITCH_ENABLED
from .const import CONF_WIFI_SENSOR_ENABLED
from .const import DOMAIN
from .const import PLATFORMS
from .const import SENSOR
from .const import STARTUP_MESSAGE
from .const import SWITCH
SCAN_INTERVAL = timedelta(seconds=60)
_LOGGER: logging.Logger = logging.getLogger(__package__)
async def async_setup(hass: HomeAssistant, config: Config):
"""Set up this integration using YAML is not supported."""
return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Set up this integration using UI."""
if hass.data.get(DOMAIN) is None:
hass.data.setdefault(DOMAIN, {})
_LOGGER.info(STARTUP_MESSAGE)
hub_hash = entry.data.get(CONF_HUB_HASH)
session = async_get_clientsession(hass)
client = RitualsGenieApiClient(hub_hash, session)
coordinator = RitualsGenieDataUpdateCoordinator(hass, client=client)
await coordinator.async_refresh()
if not coordinator.last_update_success:
raise ConfigEntryNotReady
hass.data[DOMAIN][entry.entry_id] = coordinator
if entry.options.get(CONF_SWITCH_ENABLED, True):
coordinator.platforms.append(SWITCH)
hass.async_add_job(hass.config_entries.async_forward_entry_setup(entry, SWITCH))
if (
entry.options.get(CONF_FILL_SENSOR_ENABLED, True)
or entry.options.get(CONF_PERFUME_SENSOR_ENABLED, True)
or entry.options.get(CONF_WIFI_SENSOR_ENABLED, True)
):
coordinator.platforms.append(SENSOR)
hass.async_add_job(hass.config_entries.async_forward_entry_setup(entry, SENSOR))
entry.add_update_listener(async_reload_entry)
return True
class RitualsGenieDataUpdateCoordinator(DataUpdateCoordinator):
"""Class to manage fetching data from the API."""
def __init__(
self,
hass: HomeAssistant,
client: RitualsGenieApiClient,
) -> None:
"""Initialize."""
self.api = client
self.platforms = []
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL)
async def _async_update_data(self):
"""Update data via library."""
try:
return await self.api.async_get_data()
except Exception as exception:
raise UpdateFailed() from exception
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Handle removal of an entry."""
coordinator = hass.data[DOMAIN][entry.entry_id]
unloaded = all(
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_unload(entry, platform)
for platform in PLATFORMS
if platform in coordinator.platforms
]
)
)
if unloaded:
hass.data[DOMAIN].pop(entry.entry_id)
return unloaded
async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Reload config entry."""
await async_unload_entry(hass, entry)
await async_setup_entry(hass, entry)