forked from kukulich/home-assistant-jablotron100
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
81 lines (62 loc) · 3.05 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
"""The Jablotron integration."""
from homeassistant import config_entries, core
from homeassistant.components.alarm_control_panel import DOMAIN as PLATFORM_ALARM_CONTROL_PANEL
from homeassistant.components.binary_sensor import DOMAIN as PLATFORM_BINARY_SENSOR
from homeassistant.components.sensor import DOMAIN as PLATFORM_SENSOR
from homeassistant.components.switch import DOMAIN as PLATFORM_SWITCH
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers import entity_registry as er
from .const import (
DATA_JABLOTRON,
DATA_OPTIONS_UPDATE_UNSUBSCRIBER,
DOMAIN,
)
from .jablotron import Jablotron
async def async_setup_entry(hass: core.HomeAssistant, config_entry: config_entries.ConfigEntry) -> bool:
hass.data.setdefault(DOMAIN, {})
jablotron_instance: Jablotron = Jablotron(hass, config_entry.data, config_entry.options)
await jablotron_instance.initialize()
hass.data[DOMAIN][config_entry.entry_id] = {
DATA_JABLOTRON: jablotron_instance,
DATA_OPTIONS_UPDATE_UNSUBSCRIBER: config_entry.add_update_listener(options_update_listener),
}
central_unit = jablotron_instance.central_unit()
if central_unit.model in ("JA-103K", "JA-103KRY", "JA-107K"):
entity_registry = await er.async_get_registry(hass)
not_working_gsm_signal_entity_id = entity_registry.async_get_entity_id(
PLATFORM_BINARY_SENSOR,
DOMAIN,
"{}.{}.gsm_signal_sensor".format(DOMAIN, central_unit.serial_port),
)
if not_working_gsm_signal_entity_id is not None:
entity_registry.async_remove(not_working_gsm_signal_entity_id)
not_working_gsm_signal_strength_entity_id = entity_registry.async_get_entity_id(
PLATFORM_SENSOR,
DOMAIN,
"{}.{}.gsm_signal_strength_sensor".format(DOMAIN, central_unit.serial_port),
)
if not_working_gsm_signal_strength_entity_id is not None:
entity_registry.async_remove(not_working_gsm_signal_strength_entity_id)
device_registry = await dr.async_get_registry(hass)
device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
identifiers={(DOMAIN, central_unit.serial_port)},
name="Jablotron 100",
model="{} ({})".format(central_unit.model, central_unit.hardware_version),
manufacturer="Jablotron",
sw_version=central_unit.firmware_version,
)
for platform in (PLATFORM_ALARM_CONTROL_PANEL, PLATFORM_BINARY_SENSOR, PLATFORM_SENSOR, PLATFORM_SWITCH):
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, platform)
)
return True
async def async_unload_entry(hass: core.HomeAssistant, config_entry: config_entries.ConfigEntry) -> bool:
options_update_unsubscriber = hass.data[DOMAIN][config_entry.entry_id][DATA_OPTIONS_UPDATE_UNSUBSCRIBER]
options_update_unsubscriber()
jablotron_instance: Jablotron = hass.data[DOMAIN][config_entry.entry_id][DATA_JABLOTRON]
jablotron_instance.shutdown()
return True
async def options_update_listener(hass: core.HomeAssistant, config_entry: config_entries.ConfigEntry) -> None:
jablotron_instance: Jablotron = hass.data[DOMAIN][config_entry.entry_id][DATA_JABLOTRON]
jablotron_instance.update_options(config_entry.options)