Skip to content

Commit 434eb8c

Browse files
committed
Support for mutiple heaters
1 parent 778a50c commit 434eb8c

File tree

1 file changed

+45
-27
lines changed

1 file changed

+45
-27
lines changed

custom_components/intellicenter/water_heater.py

+45-27
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
from .const import DOMAIN
1818
from .pyintellicenter import (
1919
BODY_ATTR,
20+
BODY_TYPE,
2021
HEATER_ATTR,
2122
HEATER_TYPE,
2223
HTMODE_ATTR,
24+
LISTORD_ATTR,
2325
LOTMP_ATTR,
2426
LSTTMP_ATTR,
2527
NULL_OBJNAM,
@@ -43,21 +45,25 @@ async def async_setup_entry(
4345
# body of water
4446

4547
# first find all heaters
46-
heaters = [object for object in controller.model if object.objtype == HEATER_TYPE]
48+
# and sort them by their UI order (if they don't have one, use 100 and place them last)
49+
heaters = sorted(
50+
controller.model.getByType(HEATER_TYPE),
51+
key=lambda h: h[LISTORD_ATTR] if h[LISTORD_ATTR] else 100,
52+
)
4753

48-
# then for each heater, find which bodies it handles
49-
body_to_heater_map = {}
50-
for heater in heaters:
51-
bodies = heater[BODY_ATTR].split(" ")
52-
for body_id in bodies:
53-
body_to_heater_map[body_id] = heater.objnam
54+
bodies = controller.model.getByType(BODY_TYPE)
5455

5556
water_heaters = []
56-
57-
for (body_id, heater_id) in body_to_heater_map.items():
58-
body = controller.model[body_id]
59-
if body:
60-
water_heaters.append(PoolWaterHeater(entry, controller, body, heater_id))
57+
body: PoolObject
58+
for body in bodies:
59+
heater_list = []
60+
heater: PoolObject
61+
for heater in heaters:
62+
# if the heater supports this body, add it to the list
63+
if body.objnam in heater[BODY_ATTR].split(" "):
64+
heater_list.append(heater.objnam)
65+
if heater_list:
66+
water_heaters.append(PoolWaterHeater(entry, controller, body, heater_list))
6167

6268
async_add_entities(water_heaters)
6369

@@ -75,7 +81,7 @@ def __init__(
7581
entry: ConfigEntry,
7682
controller: ModelController,
7783
poolObject: PoolObject,
78-
heater_id,
84+
heater_list,
7985
):
8086
"""Initialize."""
8187
super().__init__(
@@ -84,7 +90,7 @@ def __init__(
8490
poolObject,
8591
extraStateAttributes=[HEATER_ATTR, HTMODE_ATTR],
8692
)
87-
self._heater_id = heater_id
93+
self._heater_list = heater_list
8894
self._lastHeater = self._poolObject[HEATER_ATTR]
8995

9096
@property
@@ -103,11 +109,10 @@ def state(self) -> str:
103109
"""Return the current state."""
104110
status = self._poolObject[STATUS_ATTR]
105111
heater = self._poolObject[HEATER_ATTR]
106-
htmode = self._poolObject[HTMODE_ATTR]
107112
if status == "OFF" or heater == NULL_OBJNAM:
108113
return STATE_OFF
109-
if heater == self._heater_id:
110-
return STATE_ON if htmode != "0" else STATE_IDLE
114+
htmode = self._poolObject[HTMODE_ATTR]
115+
return STATE_ON if htmode != "0" else STATE_IDLE
111116

112117
@property
113118
def unique_id(self):
@@ -158,46 +163,59 @@ def set_temperature(self, **kwargs):
158163
def current_operation(self):
159164
"""Return current operation."""
160165
heater = self._poolObject[HEATER_ATTR]
161-
if heater == self._heater_id:
162-
return self._controller.model[self._heater_id].sname
166+
if heater in self._heater_list:
167+
return self._controller.model[heater].sname
163168
return STATE_OFF
164169

165170
@property
166171
def operation_list(self):
167172
"""Return the list of available operation modes."""
168-
return [STATE_OFF, self._controller.model[self._heater_id].sname]
173+
return [STATE_OFF] + [
174+
self._controller.model[heater].sname for heater in self._heater_list
175+
]
169176

170177
def set_operation_mode(self, operation_mode):
171178
"""Set new target operation mode."""
172179
if operation_mode == STATE_OFF:
173180
self._turnOff()
174-
elif operation_mode == self._controller.model[self._heater_id].sname:
175-
self.requestChanges({HEATER_ATTR: self._heater_id})
181+
else:
182+
for heater in self._heater_list:
183+
if operation_mode == self._controller.model[heater].sname:
184+
self.requestChanges({HEATER_ATTR: heater})
185+
break
176186

177187
async def async_turn_on(self) -> None:
178188
"""Turn the entity on."""
179-
if self._lastHeater:
180-
self.requestChanges({HEATER_ATTR: self._lastHeater})
189+
heater = (
190+
self._lastHeater
191+
if self._lastHeater != NULL_OBJNAM
192+
else self._heater_list[0]
193+
)
194+
self.requestChanges({HEATER_ATTR: heater})
181195

182196
async def async_turn_off(self) -> None:
183197
"""Turn the entity off."""
184198
self._turnOff()
185199

186200
def _turnOff(self):
187-
self._lastHeater = self._poolObject[HEATER_ATTR]
188201
self.requestChanges({HEATER_ATTR: NULL_OBJNAM})
189202

190203
def isUpdated(self, updates: Dict[str, Dict[str, str]]) -> bool:
191204
"""Return true if the entity is updated by the updates from Intellicenter."""
192205

193206
myUpdates = updates.get(self._poolObject.objnam, {})
194207

195-
return (
208+
updated = (
196209
myUpdates
197210
and {STATUS_ATTR, HEATER_ATTR, HTMODE_ATTR, LOTMP_ATTR, LSTTMP_ATTR}
198211
& myUpdates.keys()
199212
)
200213

214+
if updated and self._poolObject[HEATER_ATTR] != NULL_OBJNAM:
215+
self._lastHeater = self._poolObject[HEATER_ATTR]
216+
217+
return updated
218+
201219
async def async_added_to_hass(self):
202220
"""Entity is added to Home Assistant."""
203221

@@ -210,5 +228,5 @@ async def async_added_to_hass(self):
210228

211229
if last_state:
212230
value = last_state.attributes.get(self.LAST_HEATER_ATTR)
213-
if value:
231+
if value != NULL_OBJNAM:
214232
self._lastHeater = value

0 commit comments

Comments
 (0)