Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit 23200c0

Browse files
Multiple instance (#184)
multiple instance
1 parent 25f5923 commit 23200c0

File tree

8 files changed

+64
-58
lines changed

8 files changed

+64
-58
lines changed

custom_components/multimatic/__init__.py

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from homeassistant.helpers.typing import ConfigType
1010

1111
from .const import (
12+
CONF_SERIAL_NUMBER,
1213
COORDINATOR_LIST,
1314
COORDINATORS,
1415
DEFAULT_SCAN_INTERVAL,
@@ -33,8 +34,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
3334
api: MultimaticApi = MultimaticApi(hass, entry)
3435

3536
hass.data.setdefault(DOMAIN, {})
36-
hass.data[DOMAIN].setdefault(entry.unique_id, {})
37-
hass.data[DOMAIN][entry.unique_id].setdefault(COORDINATORS, {})
37+
hass.data[DOMAIN].setdefault(entry.entry_id, {})
38+
hass.data[DOMAIN][entry.entry_id].setdefault(COORDINATORS, {})
39+
40+
_LOGGER.debug(
41+
"Setting up multimatic for serial %s, id is %s",
42+
entry.data.get(CONF_SERIAL_NUMBER),
43+
entry.entry_id,
44+
)
3845

3946
for coord in COORDINATOR_LIST.items():
4047
update_interval = (
@@ -51,7 +58,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
5158
method="get_" + coord[0],
5259
update_interval=update_interval,
5360
)
54-
hass.data[DOMAIN][entry.unique_id][COORDINATORS][coord[0]] = m_coord
61+
hass.data[DOMAIN][entry.entry_id][COORDINATORS][coord[0]] = m_coord
5562
_LOGGER.debug("Adding %s coordinator", m_coord.name)
5663
await m_coord.async_refresh()
5764

@@ -65,30 +72,41 @@ async def logout(event):
6572

6673
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, logout)
6774

68-
await async_setup_service(api, hass)
75+
await async_setup_service(hass, api, entry)
6976

7077
return True
7178

7279

73-
async def async_setup_service(api: MultimaticApi, hass):
80+
async def async_setup_service(hass, api: MultimaticApi, entry: ConfigEntry):
7481
"""Set up services."""
75-
if not hass.data.get(SERVICES_HANDLER):
82+
serial = api.serial if api.fixed_serial else None
83+
84+
if not hass.data[DOMAIN][entry.entry_id].get(SERVICES_HANDLER):
7685
service_handler = MultimaticServiceHandler(api, hass)
77-
for service_key in SERVICES:
78-
schema = SERVICES[service_key]["schema"]
79-
if not SERVICES[service_key].get("entity", False):
86+
for service_key, data in SERVICES.items():
87+
schema = data["schema"]
88+
if not data.get("entity", False):
89+
key = service_key
90+
if serial:
91+
key += f"_{serial}"
8092
hass.services.async_register(
81-
DOMAIN, service_key, service_handler.service_call, schema=schema
93+
DOMAIN, key, getattr(service_handler, service_key), schema=schema
8294
)
83-
hass.data[DOMAIN][SERVICES_HANDLER] = service_handler
95+
hass.data[DOMAIN][entry.entry_id][SERVICES_HANDLER] = service_handler
8496

8597

86-
async def async_unload_services(hass):
87-
"""Remove service when integration is removed."""
88-
service_handler = hass.data[DOMAIN].get(SERVICES_HANDLER, None)
98+
async def async_unload_services(hass, entry: ConfigEntry):
99+
"""Remove services when integration is removed."""
100+
service_handler = hass.data[DOMAIN][entry.entry_id].get(SERVICES_HANDLER, None)
89101
if service_handler:
90-
for service_name in SERVICES:
91-
hass.services.async_remove(DOMAIN, service_name)
102+
serial = (
103+
service_handler.api.serial if service_handler.api.fixed_serial else None
104+
)
105+
for service_key in SERVICES:
106+
key = service_key
107+
if serial:
108+
key += f"_{serial}"
109+
hass.services.async_remove(DOMAIN, key)
92110

93111

94112
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
@@ -102,15 +120,9 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
102120
)
103121
)
104122
if unload_ok:
105-
hass.data[DOMAIN].pop(entry.unique_id)
123+
await async_unload_services(hass, entry)
124+
hass.data[DOMAIN].pop(entry.entry_id)
106125

107126
_LOGGER.debug("Remaining data for multimatic %s", hass.data[DOMAIN])
108127

109-
if (
110-
len(hass.data[DOMAIN]) == 1
111-
and hass.data[DOMAIN].get(SERVICES_HANDLER, None) is not None
112-
):
113-
await async_unload_services(hass)
114-
hass.data[DOMAIN].pop(SERVICES_HANDLER)
115-
116128
return unload_ok

custom_components/multimatic/binary_sensor.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ async def async_setup_entry(
4343
"""Set up the multimatic binary sensor platform."""
4444
sensors: list[MultimaticEntity] = []
4545

46-
dhw_coo = get_coordinator(hass, DHW, entry.unique_id)
46+
dhw_coo = get_coordinator(hass, DHW, entry.entry_id)
4747
if dhw_coo.data and dhw_coo.data.circulation:
4848
sensors.append(CirculationSensor(dhw_coo))
4949

50-
hvac_coo = get_coordinator(hass, HVAC_STATUS, entry.unique_id)
51-
detail_coo = get_coordinator(hass, FACILITY_DETAIL, entry.unique_id)
52-
gw_coo = get_coordinator(hass, GATEWAY, entry.unique_id)
50+
hvac_coo = get_coordinator(hass, HVAC_STATUS, entry.entry_id)
51+
detail_coo = get_coordinator(hass, FACILITY_DETAIL, entry.entry_id)
52+
gw_coo = get_coordinator(hass, GATEWAY, entry.entry_id)
5353
if hvac_coo.data:
5454
sensors.append(BoxOnline(hvac_coo, detail_coo, gw_coo))
5555
sensors.append(BoxUpdate(hvac_coo, detail_coo, gw_coo))
@@ -58,7 +58,7 @@ async def async_setup_entry(
5858
if hvac_coo.data.boiler_status:
5959
sensors.append(BoilerStatus(hvac_coo))
6060

61-
rooms_coo = get_coordinator(hass, ROOMS, entry.unique_id)
61+
rooms_coo = get_coordinator(hass, ROOMS, entry.entry_id)
6262
if rooms_coo.data:
6363
for room in rooms_coo.data:
6464
sensors.append(RoomWindow(rooms_coo, room))
@@ -70,8 +70,8 @@ async def async_setup_entry(
7070

7171
sensors.extend(
7272
[
73-
HolidayModeSensor(get_coordinator(hass, HOLIDAY_MODE, entry.unique_id)),
74-
QuickModeSensor(get_coordinator(hass, QUICK_MODE, entry.unique_id)),
73+
HolidayModeSensor(get_coordinator(hass, HOLIDAY_MODE, entry.entry_id)),
74+
QuickModeSensor(get_coordinator(hass, QUICK_MODE, entry.entry_id)),
7575
]
7676
)
7777

custom_components/multimatic/climate.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ async def async_setup_entry(
7171
) -> None:
7272
"""Set up the multimatic climate platform."""
7373
climates: list[MultimaticClimate] = []
74-
zones_coo = get_coordinator(hass, ZONES, entry.unique_id)
75-
rooms_coo = get_coordinator(hass, ROOMS, entry.unique_id)
76-
ventilation_coo = get_coordinator(hass, VENTILATION, entry.unique_id)
74+
zones_coo = get_coordinator(hass, ZONES, entry.entry_id)
75+
rooms_coo = get_coordinator(hass, ROOMS, entry.entry_id)
76+
ventilation_coo = get_coordinator(hass, VENTILATION, entry.entry_id)
7777

7878
if zones_coo.data:
7979
for zone in zones_coo.data:

custom_components/multimatic/fan.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ async def async_setup_entry(
3232
) -> None:
3333
"""Set up the multimatic fan platform."""
3434

35-
coordinator = get_coordinator(hass, VENTILATION, entry.unique_id)
35+
coordinator = get_coordinator(hass, VENTILATION, entry.entry_id)
3636

3737
if coordinator.data:
3838
_LOGGER.debug("Adding fan entity")

custom_components/multimatic/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
"homekit": {},
1313
"dependencies": [],
1414
"codeowners": ["@thomasgermain"],
15-
"version": "1.12.12",
15+
"version": "1.13.0",
1616
"iot_class": "cloud_polling"
1717
}

custom_components/multimatic/sensor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ async def async_setup_entry(
3939
) -> None:
4040
"""Set up the multimatic sensors."""
4141
sensors: list[MultimaticEntity] = []
42-
outdoor_temp_coo = get_coordinator(hass, OUTDOOR_TEMP, entry.unique_id)
43-
reports_coo = get_coordinator(hass, REPORTS, entry.unique_id)
44-
emf_reports_coo = get_coordinator(hass, EMF_REPORTS, entry.unique_id)
42+
outdoor_temp_coo = get_coordinator(hass, OUTDOOR_TEMP, entry.entry_id)
43+
reports_coo = get_coordinator(hass, REPORTS, entry.entry_id)
44+
emf_reports_coo = get_coordinator(hass, EMF_REPORTS, entry.entry_id)
4545

4646
if outdoor_temp_coo.data:
4747
sensors.append(OutdoorTemperatureSensor(outdoor_temp_coo))

custom_components/multimatic/service.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -128,42 +128,36 @@ def __init__(self, hub: MultimaticApi, hass) -> None:
128128
self.api = hub
129129
self._hass = hass
130130

131-
async def service_call(self, call):
132-
"""Handle service calls."""
133-
service = call.service
134-
method = getattr(self, service)
135-
await method(data=call.data)
136-
137-
async def remove_quick_mode(self, data):
131+
async def remove_quick_mode(self, call):
138132
"""Remove quick mode. It has impact on all components."""
139133
await self.api.remove_quick_mode()
140134

141-
async def set_holiday_mode(self, data):
135+
async def set_holiday_mode(self, call):
142136
"""Set holiday mode."""
143-
start_str = data.get(ATTR_START_DATE, None)
144-
end_str = data.get(ATTR_END_DATE, None)
145-
temp = data.get(ATTR_TEMPERATURE)
137+
start_str = call.data.get(ATTR_START_DATE, None)
138+
end_str = call.data.get(ATTR_END_DATE, None)
139+
temp = call.data.get(ATTR_TEMPERATURE)
146140
start = parse_date(start_str.split("T")[0])
147141
end = parse_date(end_str.split("T")[0])
148142
if end is None or start is None:
149143
raise ValueError(f"dates are incorrect {start_str} {end_str}")
150144
await self.api.set_holiday_mode(start, end, temp)
151145

152-
async def remove_holiday_mode(self, data):
146+
async def remove_holiday_mode(self, call):
153147
"""Remove holiday mode."""
154148
await self.api.remove_holiday_mode()
155149

156-
async def set_quick_mode(self, data):
150+
async def set_quick_mode(self, call):
157151
"""Set quick mode, it may impact the whole system."""
158-
quick_mode = data.get(ATTR_QUICK_MODE, None)
159-
duration = data.get(ATTR_DURATION, None)
152+
quick_mode = call.data.get(ATTR_QUICK_MODE, None)
153+
duration = call.data.get(ATTR_DURATION, None)
160154
await self.api.set_quick_mode(quick_mode, duration)
161155

162-
async def request_hvac_update(self, data):
156+
async def request_hvac_update(self, call):
163157
"""Ask multimatic API to get data from the installation."""
164158
await self.api.request_hvac_update()
165159

166-
async def set_datetime(self, data):
160+
async def set_datetime(self, call):
167161
"""Set date time."""
168-
date_t: datetime = data.get(ATTR_DATE_TIME, datetime.datetime.now())
162+
date_t: datetime = call.data.get(ATTR_DATE_TIME, datetime.datetime.now())
169163
await self.api.set_datetime(date_t)

custom_components/multimatic/water_heater.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ async def async_setup_entry(
4343
) -> None:
4444
"""Set up water_heater platform."""
4545
entities = []
46-
coordinator = get_coordinator(hass, DHW, entry.unique_id)
46+
coordinator = get_coordinator(hass, DHW, entry.entry_id)
4747

4848
if coordinator.data and coordinator.data.hotwater:
4949
entities.append(MultimaticWaterHeater(coordinator))

0 commit comments

Comments
 (0)