Repository navigation
Expand file tree
/
Copy path__init__.py
More file actions
126 lines (105 loc) · 3.86 KB
/
Copy path__init__.py
File metadata and controls
126 lines (105 loc) · 3.86 KB
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
"""Dimplex WPM integration."""
from __future__ import annotations
import logging
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PORT, Platform
from homeassistant.core import HomeAssistant
from .const import (
CAPABILITY_CONF_MAP,
CONF_ENABLE_WRITE_ENTITIES,
CONF_ENABLED_MODULES,
CONF_FLOW_SENSOR_ENTITY,
CONF_INCLUDE_RE_REGISTERS,
CONF_PROFILE,
CONF_SCAN_INTERVAL,
CONF_SOFTWARE_VERSION,
CONF_TIMEOUT,
CONF_UNIT_ID,
DEFAULT_ENABLE_WRITE,
DEFAULT_PROFILE,
DEFAULT_SCAN_INTERVAL,
DEFAULT_SOFTWARE_VERSION,
DEFAULT_TIMEOUT,
DEFAULT_UNIT_ID,
DOMAIN,
resolve_capabilities,
resolve_enabled_modules,
)
from .coordinator import DimplexDataUpdateCoordinator
from .modbus_client import DimplexModbusClient
from .profiles import get_profile
from .registers import CAP_INVERTER_FREQ, CORE_MODULES
LOGGER = logging.getLogger(__name__)
PLATFORMS = [
Platform.SENSOR,
Platform.BINARY_SENSOR,
Platform.NUMBER,
Platform.SELECT,
Platform.CLIMATE,
]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Dimplex WPM from a config entry."""
hass.data.setdefault(DOMAIN, {})
source = {**entry.data, **entry.options}
host = source[CONF_HOST]
port = source.get(CONF_PORT, 502)
unit_id = source.get(CONF_UNIT_ID, DEFAULT_UNIT_ID)
timeout = source.get(CONF_TIMEOUT, DEFAULT_TIMEOUT)
scan_interval = source.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
software_version = source.get(CONF_SOFTWARE_VERSION, DEFAULT_SOFTWARE_VERSION)
profile = get_profile(source.get(CONF_PROFILE, DEFAULT_PROFILE))
if CONF_ENABLED_MODULES in source:
enabled_modules = resolve_enabled_modules(source)
else:
enabled_modules = frozenset(CORE_MODULES) | profile.default_modules
if any(key in source for key in CAPABILITY_CONF_MAP):
capabilities = resolve_capabilities(source)
else:
capabilities = profile.default_capabilities
include_re = source.get(
CONF_INCLUDE_RE_REGISTERS, CAP_INVERTER_FREQ in capabilities
)
enable_control = entry.options.get(CONF_ENABLE_WRITE_ENTITIES, DEFAULT_ENABLE_WRITE)
client = DimplexModbusClient(host, port, unit_id, timeout)
coordinator = DimplexDataUpdateCoordinator(
hass,
client,
scan_interval=scan_interval,
software_version=software_version,
enabled_modules=enabled_modules,
capabilities=capabilities,
include_re=include_re,
profile=profile,
flow_sensor_entity=source.get(CONF_FLOW_SENSOR_ENTITY) or None,
enable_control=enable_control,
host=host,
port=port,
unit_id=unit_id,
)
await coordinator.async_config_entry_first_refresh()
hass.data[DOMAIN][entry.entry_id] = {
"coordinator": coordinator,
"client": client,
"host": host,
"port": port,
"unit_id": unit_id,
"software_version": software_version,
"profile": profile.key,
"model": profile.name,
CONF_ENABLE_WRITE_ENTITIES: entry.options.get(
CONF_ENABLE_WRITE_ENTITIES, DEFAULT_ENABLE_WRITE
),
}
entry.async_on_unload(entry.add_update_listener(_async_update_listener))
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
data = hass.data[DOMAIN].pop(entry.entry_id)
await data["client"].close()
return unload_ok
async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Reload the entry when options change."""
await hass.config_entries.async_reload(entry.entry_id)