forked from AlexxIT/XiaomiGateway3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
143 lines (107 loc) · 4.13 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import logging
import voluptuous as vol
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers import device_registry
from homeassistant.helpers.device_registry import DeviceEntry
from homeassistant.helpers.typing import ConfigType
from .core import logger
from .core.const import DOMAIN
from .core.device import XDevice
from .core.gate.base import EVENT_MQTT_CONNECT
from .core.gateway import MultiGateway
from .hass import hass_utils
from .hass.add_entitites import handle_add_entities
from .hass.entity import XEntity
_LOGGER = logging.getLogger(__name__)
PLATFORMS = [
"alarm_control_panel",
"binary_sensor",
"climate",
"cover",
"light",
"number",
"select",
"sensor",
"switch",
"text",
]
CONF_DEVICES = "devices"
CONF_ATTRIBUTES_TEMPLATE = "attributes_template"
CONF_OPENMIIO = "openmiio"
CONF_LOGGER = "logger"
CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{
CONF_LOGGER: logger.CONFIG_SCHEMA,
vol.Optional(CONF_ATTRIBUTES_TEMPLATE): cv.template,
},
extra=vol.ALLOW_EXTRA,
),
},
extra=vol.ALLOW_EXTRA,
)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
if config := config.get(DOMAIN):
if devices_config := config.get(CONF_DEVICES):
XDevice.configs = hass_utils.fix_yaml_devices_config(devices_config)
if logger_config := config.get(CONF_LOGGER):
_ = hass.async_add_executor_job(
logger.init, __name__, logger_config, hass.config.config_dir
)
if template := config.get(CONF_ATTRIBUTES_TEMPLATE):
template.hass = hass
XEntity.attributes_template = template
hass.data[DOMAIN] = {}
await hass_utils.store_devices(hass)
return True
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
if config_entry.data:
return await hass_utils.setup_cloud(hass, config_entry)
await hass_utils.store_gateway_key(hass, config_entry)
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
gw = MultiGateway(**config_entry.options)
handle_add_entities(hass, config_entry, gw)
gw.start()
hass.data[DOMAIN][config_entry.entry_id] = gw
if not config_entry.update_listeners:
config_entry.add_update_listener(async_update_options)
async def hass_stop(event):
await gw.stop()
config_entry.async_on_unload(
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, hass_stop)
)
return True
async def async_update_options(hass: HomeAssistant, config_entry: ConfigEntry):
await hass.config_entries.async_reload(config_entry.entry_id)
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
if config_entry.data:
return True # skip unload for cloud config entry
# remove all stats entities if disable stats
hass_utils.remove_stats_entities(hass, config_entry)
# important to remove entities before stop gateway
ok = await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
gw: MultiGateway = hass.data[DOMAIN][config_entry.entry_id]
await gw.stop()
gw.remove_all_devices()
return ok
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry):
if config_entry.version == 1:
hass_utils.migrate_legacy_devices_unique_id(hass)
hass_utils.migrate_legacy_entitites_unique_id(hass)
hass_utils.migrate_devices_store()
try:
# fix support Hass 2023.12 and earlier - no version arg
hass.config_entries.async_update_entry(config_entry, version=4)
except TypeError:
pass
return True
async def async_remove_config_entry_device(
hass: HomeAssistant, config_entry: ConfigEntry, device_entry: DeviceEntry
) -> bool:
"""Supported from Hass v2022.3"""
device_registry.async_get(hass).async_remove_device(device_entry.id)
return True