1
+ """Build entitiy List and Update Coordinator."""
2
+
3
+ import asyncio
1
4
from datetime import timedelta
2
5
import logging
3
6
import warnings
4
7
5
- import async_timeout
6
-
7
8
from homeassistant .components .number import NumberEntity
8
9
from homeassistant .components .select import SelectEntity
9
10
from homeassistant .components .sensor import (
29
30
30
31
31
32
def BuildEntityList (entries , config_entry , modbusitems , item_type , coordinator = None ):
32
- """Builds the Entity List.
33
+ """Build the entity list.
34
+
33
35
Function builds a list of entities that can be used as parameter by async_setup_entry()
34
36
type of list is defined by the ModbusItem's type flag
35
37
so the app only holds one list of entities that is build from a list of ModbusItem
@@ -89,11 +91,12 @@ async def _async_setup(self):
89
91
self ._device = self ._modbus_api .get_device ()
90
92
91
93
def calcTemperature (self , val : float , modbus_item ):
94
+ """Calculate Temperature with values from the heatpump."""
92
95
divider = 1
93
96
if modbus_item .resultlist is not None :
94
97
divider = modbus_item .getNumberFromText ("divider" )
95
98
96
- if val == None :
99
+ if val is None :
97
100
return None
98
101
if val == - 32768 :
99
102
return - 1
@@ -104,19 +107,20 @@ def calcTemperature(self, val: float, modbus_item):
104
107
return val / divider
105
108
106
109
def calcPercentage (self , val : float , modbus_item ):
110
+ """Calculate Percentage with value from heatpump."""
107
111
divider = 1
108
- if modbus_item .resultlist != None :
112
+ if modbus_item .resultlist is not None :
109
113
divider = modbus_item .getNumberFromText ("divider" )
110
114
111
- if val == None :
115
+ if val is None :
112
116
return None
113
117
if val == 65535 :
114
118
return None
115
119
return val / divider
116
120
117
121
# @property
118
122
async def translateVal (self , modbus_item ):
119
- # reads an translates a value from the modbus
123
+ """Read an translates a value from the modbus."""
120
124
mbo = ModbusObject (self ._modbus_api , modbus_item )
121
125
if mbo is None :
122
126
return None
@@ -142,7 +146,7 @@ async def _async_update_data(self):
142
146
# try:
143
147
# Note: asyncio.TimeoutError and aiohttp.ClientError are already
144
148
# handled by the data update coordinator.
145
- async with async_timeout .timeout (10 ):
149
+ async with asyncio .timeout (10 ):
146
150
# Grab active context variables to limit data required to be fetched from API
147
151
# Note: using context is not required if there is no need or ability to limit
148
152
# data retrieved from API.
@@ -176,8 +180,8 @@ async def _async_update_data(self):
176
180
177
181
178
182
class MyEntity :
179
- """
180
- An entity using CoordinatorEntity.
183
+ """An entity using CoordinatorEntity.
184
+
181
185
The CoordinatorEntity class provides:
182
186
should_poll
183
187
async_update
@@ -196,6 +200,7 @@ class MyEntity:
196
200
_dev_device = ""
197
201
198
202
def __init__ (self , config_entry , modbus_item ) -> None :
203
+ """Initialize the entity."""
199
204
self ._config_entry = config_entry
200
205
self ._modbus_item = modbus_item
201
206
self ._attr_name = self ._modbus_item .name
@@ -212,15 +217,16 @@ def __init__(self, config_entry, modbus_item) -> None:
212
217
if self ._modbus_item ._format == FORMATS .POWER :
213
218
self ._attr_state_class = SensorStateClass .MEASUREMENT
214
219
215
- if self ._modbus_item .resultlist != None :
220
+ if self ._modbus_item .resultlist is not None :
216
221
self ._attr_native_min_value = self ._modbus_item .getNumberFromText ("min" )
217
222
self ._attr_native_max_value = self ._modbus_item .getNumberFromText ("max" )
218
223
self ._attr_native_step = self ._modbus_item .getNumberFromText ("step" )
219
224
self ._divider = self ._modbus_item .getNumberFromText ("divider" )
220
225
self ._attr_device_class = self ._modbus_item .getTextFromNumber (- 1 )
221
226
222
227
def calcTemperature (self , val : float ):
223
- if val == None :
228
+ """Calcualte temperature."""
229
+ if val is None :
224
230
return None
225
231
if val == - 32768 :
226
232
return - 1
@@ -231,19 +237,20 @@ def calcTemperature(self, val: float):
231
237
return val / self ._divider
232
238
233
239
def calcPercentage (self , val : float ):
234
- if val == None :
240
+ """Calculate percentage."""
241
+ if val is None :
235
242
return None
236
243
if val == 65535 :
237
244
return None
238
245
return val / self ._divider
239
246
240
247
@property
241
248
async def translateVal (self ):
242
- # reads an translates a value from the modbus
249
+ """Read an translate a value from the modbus."""
243
250
# mbo = ModbusObject(self._config_entry, self._modbus_item)
244
251
mbo = None
245
252
246
- if mbo == None :
253
+ if mbo is None :
247
254
return None
248
255
val = await mbo .value
249
256
match self ._modbus_item .format :
@@ -254,17 +261,17 @@ async def translateVal(self):
254
261
case FORMATS .STATUS :
255
262
return self ._modbus_item .getTextFromNumber (val )
256
263
case _:
257
- if val == None :
264
+ if val is None :
258
265
return val
259
266
return val / self ._divider
260
267
261
268
# @translateVal.setter
262
269
async def settranslateVal (self , value ):
263
- # translates and writes a value to the modbus
270
+ """Translate and write a value to the modbus."""
264
271
# mbo = ModbusObject(self._config_entry, self._modbus_item)
265
272
mbo = None
266
273
267
- if mbo == None :
274
+ if mbo is None :
268
275
return
269
276
val = None
270
277
match self ._modbus_item .format :
@@ -276,7 +283,7 @@ async def settranslateVal(self, value):
276
283
await mbo .setvalue (val ) # = val
277
284
278
285
def my_device_info (self ) -> DeviceInfo :
279
- # helper to build the device info
286
+ """Build the device info with this helper."""
280
287
return {
281
288
"identifiers" : {(CONST .DOMAIN , self ._dev_device )},
282
289
"name" : self ._dev_device ,
@@ -287,13 +294,18 @@ def my_device_info(self) -> DeviceInfo:
287
294
288
295
289
296
class MySensorEntity (CoordinatorEntity , SensorEntity , MyEntity ):
290
- # class that represents a sensor entity derived from Sensorentity
291
- # and decorated with general parameters from MyEntity
297
+ """Class that represents a sensor entity.
298
+
299
+ Derived from Sensorentity
300
+ and decorated with general parameters from MyEntity
301
+ """
302
+
292
303
_attr_native_unit_of_measurement = None
293
304
_attr_device_class = None
294
305
_attr_state_class = None
295
306
296
307
def __init__ (self , config_entry , modbus_item , coordinator = None , idx = None ) -> None :
308
+ """Initialize sensor entitiy."""
297
309
super ().__init__ (coordinator , context = idx )
298
310
self ._idx = idx
299
311
MyEntity .__init__ (self , config_entry , modbus_item )
@@ -310,29 +322,37 @@ def _handle_coordinator_update(self) -> None:
310
322
311
323
@property
312
324
def device_info (self ) -> DeviceInfo :
325
+ """Return device info."""
313
326
return MyEntity .my_device_info (self )
314
327
315
328
316
329
class MyCalcSensorEntity (MySensorEntity ):
317
- # class that represents a sensor entity derived from Sensorentity
318
- # and decorated with general parameters from MyEntity
319
- # calculates output from map
330
+ """class that represents a sensor entity.
331
+
332
+ Derived from Sensorentity
333
+ and decorated with general parameters from MyEntity
334
+ calculates output from map
335
+ """
336
+
320
337
my_map = PowerMap ()
321
338
322
339
def __init__ (self , config_entry , modbus_item ) -> None :
340
+ """Initialize Sensor Entity."""
323
341
MySensorEntity .__init__ (self , config_entry , modbus_item )
324
342
325
343
async def async_update (self ) -> None :
326
- # the synching is done by the ModbusObject of the entity
344
+ """Sync by the ModbusObject of the entity."""
327
345
self ._attr_native_value = await self .translateVal
328
346
329
347
def calcPower (self , val , x , y ):
348
+ """Calculate power."""
330
349
if val is None :
331
350
return val
332
351
return (val / 100 ) * self .my_map .map (x , y )
333
352
334
353
@property
335
354
async def translateVal (self ):
355
+ """Not sure."""
336
356
# reads an translates a value from the modbus
337
357
# mbo = ModbusObject(self._config_entry, self._modbus_item)
338
358
# val = self.calcPercentage(await mbo.value)
@@ -373,47 +393,60 @@ async def translateVal(self):
373
393
374
394
@property
375
395
def device_info (self ) -> DeviceInfo :
396
+ """Return device info."""
376
397
return MySensorEntity .my_device_info (self )
377
398
378
399
379
400
class MyNumberEntity (NumberEntity , MyEntity ):
380
- # class that represents a sensor entity derived from Sensorentity
381
- # and decorated with general parameters from MyEntity
401
+ """Class that represents a sensor entity.
402
+
403
+ derived from Sensorentity
404
+ and decorated with general parameters from MyEntity
405
+ """
406
+
382
407
_attr_native_unit_of_measurement = None
383
408
_attr_device_class = None
384
409
_attr_state_class = None
385
410
_attr_native_min_value = 10
386
411
_attr_native_max_value = 60
387
412
388
413
def __init__ (self , config_entry , modbus_item ) -> None :
414
+ """Initialize the number entity."""
389
415
MyEntity .__init__ (self , config_entry , modbus_item )
390
416
391
- if self ._modbus_item .resultlist != None :
417
+ if self ._modbus_item .resultlist is not None :
392
418
self ._attr_native_min_value = self ._modbus_item .getNumberFromText ("min" )
393
419
self ._attr_native_max_value = self ._modbus_item .getNumberFromText ("max" )
394
420
self ._attr_native_step = self ._modbus_item .getNumberFromText ("step" )
395
421
396
422
async def async_set_native_value (self , value : float ) -> None :
423
+ """Set the value."""
397
424
await self .settranslateVal (value )
398
425
self ._attr_native_value = await self .translateVal
399
426
self .async_write_ha_state ()
400
427
401
428
async def async_update (self ) -> None :
402
- # the synching is done by the ModbusObject of the entity
429
+ """Sync by the ModbusObject of the entity."""
403
430
self ._attr_native_value = await self .translateVal
404
431
405
432
@property
406
433
def device_info (self ) -> DeviceInfo :
434
+ """Return device info."""
407
435
return MyEntity .my_device_info (self )
408
436
409
437
410
438
class MySelectEntity (SelectEntity , MyEntity ):
411
- # class that represents a sensor entity derived from Sensorentity
412
- # and decorated with general parameters from MyEntity
439
+ """class that represents a select entity.
440
+
441
+ Derived from SelectEntity
442
+ and decorated with general parameters from MyEntity
443
+ """
444
+
413
445
options = []
414
446
_attr_current_option = "FEHLER"
415
447
416
448
def __init__ (self , config_entry , modbus_item ) -> None :
449
+ """Initialize select entity."""
417
450
MyEntity .__init__ (self , config_entry , modbus_item )
418
451
self .async_internal_will_remove_from_hass_port = self ._config_entry .data [
419
452
CONF_PORT
@@ -424,16 +457,17 @@ def __init__(self, config_entry, modbus_item) -> None:
424
457
self .options .append (item .text )
425
458
426
459
async def async_select_option (self , option : str ) -> None :
427
- # the synching is done by the ModbusObject of the entity
460
+ """Sync is done by the ModbusObject of the entity."""
428
461
await self .settranslateVal (option )
429
462
self ._attr_current_option = await self .translateVal
430
463
self .async_write_ha_state ()
431
464
432
465
async def async_update (self ) -> None :
433
- # the synching is done by the ModbusObject of the entity
466
+ """Sync is done by the ModbusObject of the entity."""
434
467
# await self.coordinator.async_request_refresh()
435
468
self ._attr_current_option = await self .translateVal
436
469
437
470
@property
438
471
def device_info (self ) -> DeviceInfo :
472
+ """Return device info."""
439
473
return MyEntity .my_device_info (self )
0 commit comments