17
17
from .const import DOMAIN
18
18
from .pyintellicenter import (
19
19
BODY_ATTR ,
20
+ BODY_TYPE ,
20
21
HEATER_ATTR ,
21
22
HEATER_TYPE ,
22
23
HTMODE_ATTR ,
24
+ LISTORD_ATTR ,
23
25
LOTMP_ATTR ,
24
26
LSTTMP_ATTR ,
25
27
NULL_OBJNAM ,
@@ -43,21 +45,25 @@ async def async_setup_entry(
43
45
# body of water
44
46
45
47
# 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
+ )
47
53
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 )
54
55
55
56
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 ))
61
67
62
68
async_add_entities (water_heaters )
63
69
@@ -75,7 +81,7 @@ def __init__(
75
81
entry : ConfigEntry ,
76
82
controller : ModelController ,
77
83
poolObject : PoolObject ,
78
- heater_id ,
84
+ heater_list ,
79
85
):
80
86
"""Initialize."""
81
87
super ().__init__ (
@@ -84,7 +90,7 @@ def __init__(
84
90
poolObject ,
85
91
extraStateAttributes = [HEATER_ATTR , HTMODE_ATTR ],
86
92
)
87
- self ._heater_id = heater_id
93
+ self ._heater_list = heater_list
88
94
self ._lastHeater = self ._poolObject [HEATER_ATTR ]
89
95
90
96
@property
@@ -103,11 +109,10 @@ def state(self) -> str:
103
109
"""Return the current state."""
104
110
status = self ._poolObject [STATUS_ATTR ]
105
111
heater = self ._poolObject [HEATER_ATTR ]
106
- htmode = self ._poolObject [HTMODE_ATTR ]
107
112
if status == "OFF" or heater == NULL_OBJNAM :
108
113
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
111
116
112
117
@property
113
118
def unique_id (self ):
@@ -158,46 +163,59 @@ def set_temperature(self, **kwargs):
158
163
def current_operation (self ):
159
164
"""Return current operation."""
160
165
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
163
168
return STATE_OFF
164
169
165
170
@property
166
171
def operation_list (self ):
167
172
"""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
+ ]
169
176
170
177
def set_operation_mode (self , operation_mode ):
171
178
"""Set new target operation mode."""
172
179
if operation_mode == STATE_OFF :
173
180
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
176
186
177
187
async def async_turn_on (self ) -> None :
178
188
"""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 })
181
195
182
196
async def async_turn_off (self ) -> None :
183
197
"""Turn the entity off."""
184
198
self ._turnOff ()
185
199
186
200
def _turnOff (self ):
187
- self ._lastHeater = self ._poolObject [HEATER_ATTR ]
188
201
self .requestChanges ({HEATER_ATTR : NULL_OBJNAM })
189
202
190
203
def isUpdated (self , updates : Dict [str , Dict [str , str ]]) -> bool :
191
204
"""Return true if the entity is updated by the updates from Intellicenter."""
192
205
193
206
myUpdates = updates .get (self ._poolObject .objnam , {})
194
207
195
- return (
208
+ updated = (
196
209
myUpdates
197
210
and {STATUS_ATTR , HEATER_ATTR , HTMODE_ATTR , LOTMP_ATTR , LSTTMP_ATTR }
198
211
& myUpdates .keys ()
199
212
)
200
213
214
+ if updated and self ._poolObject [HEATER_ATTR ] != NULL_OBJNAM :
215
+ self ._lastHeater = self ._poolObject [HEATER_ATTR ]
216
+
217
+ return updated
218
+
201
219
async def async_added_to_hass (self ):
202
220
"""Entity is added to Home Assistant."""
203
221
@@ -210,5 +228,5 @@ async def async_added_to_hass(self):
210
228
211
229
if last_state :
212
230
value = last_state .attributes .get (self .LAST_HEATER_ATTR )
213
- if value :
231
+ if value != NULL_OBJNAM :
214
232
self ._lastHeater = value
0 commit comments