forked from redmcg/fbee_ha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
55 lines (42 loc) · 1.52 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
"""The fbee integration."""
from __future__ import annotations
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from .const import DOMAIN
from .fbee import FBee
# List of the platforms to support.
PLATFORMS: list[str] = ["switch"]
def handle_disconnect(d):
global reconnect
if reconnect:
d.connect()
d.start_async_read(disconnect_callback = handle_disconnect)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
global reconnect
hass.data.setdefault(DOMAIN, {})
"""Set up fbee from a config entry."""
# Store an API object for the platforms to access
d = FBee(entry.data["host"], entry.data["port"], entry.data["serialnumber"])
try:
hass.data[DOMAIN][entry.entry_id] = d
except KeyError as exc:
raise ConfigEntryNotReady() from exc
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
d.connect()
if "pollinterval" in entry.data:
i = entry.data["pollinterval"]
else:
i = 60
reconnect = True
d.start_async_read(i, handle_disconnect)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
global reconnect
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
d = hass.data[DOMAIN].pop(entry.entry_id)
reconnect = False
d.close()
return unload_ok