Skip to content

Commit ccec94a

Browse files
committed
Fix some RUFF warnings.
1 parent ad53a49 commit ccec94a

File tree

1 file changed

+67
-33
lines changed

1 file changed

+67
-33
lines changed

custom_components/weishaupt_modbus/entities.py

Lines changed: 67 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
"""Build entitiy List and Update Coordinator."""
2+
3+
import asyncio
14
from datetime import timedelta
25
import logging
36
import warnings
47

5-
import async_timeout
6-
78
from homeassistant.components.number import NumberEntity
89
from homeassistant.components.select import SelectEntity
910
from homeassistant.components.sensor import (
@@ -29,7 +30,8 @@
2930

3031

3132
def BuildEntityList(entries, config_entry, modbusitems, item_type, coordinator=None):
32-
"""Builds the Entity List.
33+
"""Build the entity list.
34+
3335
Function builds a list of entities that can be used as parameter by async_setup_entry()
3436
type of list is defined by the ModbusItem's type flag
3537
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):
8991
self._device = self._modbus_api.get_device()
9092

9193
def calcTemperature(self, val: float, modbus_item):
94+
"""Calculate Temperature with values from the heatpump."""
9295
divider = 1
9396
if modbus_item.resultlist is not None:
9497
divider = modbus_item.getNumberFromText("divider")
9598

96-
if val == None:
99+
if val is None:
97100
return None
98101
if val == -32768:
99102
return -1
@@ -104,19 +107,20 @@ def calcTemperature(self, val: float, modbus_item):
104107
return val / divider
105108

106109
def calcPercentage(self, val: float, modbus_item):
110+
"""Calculate Percentage with value from heatpump."""
107111
divider = 1
108-
if modbus_item.resultlist != None:
112+
if modbus_item.resultlist is not None:
109113
divider = modbus_item.getNumberFromText("divider")
110114

111-
if val == None:
115+
if val is None:
112116
return None
113117
if val == 65535:
114118
return None
115119
return val / divider
116120

117121
# @property
118122
async def translateVal(self, modbus_item):
119-
# reads an translates a value from the modbus
123+
"""Read an translates a value from the modbus."""
120124
mbo = ModbusObject(self._modbus_api, modbus_item)
121125
if mbo is None:
122126
return None
@@ -142,7 +146,7 @@ async def _async_update_data(self):
142146
# try:
143147
# Note: asyncio.TimeoutError and aiohttp.ClientError are already
144148
# handled by the data update coordinator.
145-
async with async_timeout.timeout(10):
149+
async with asyncio.timeout(10):
146150
# Grab active context variables to limit data required to be fetched from API
147151
# Note: using context is not required if there is no need or ability to limit
148152
# data retrieved from API.
@@ -176,8 +180,8 @@ async def _async_update_data(self):
176180

177181

178182
class MyEntity:
179-
"""
180-
An entity using CoordinatorEntity.
183+
"""An entity using CoordinatorEntity.
184+
181185
The CoordinatorEntity class provides:
182186
should_poll
183187
async_update
@@ -196,6 +200,7 @@ class MyEntity:
196200
_dev_device = ""
197201

198202
def __init__(self, config_entry, modbus_item) -> None:
203+
"""Initialize the entity."""
199204
self._config_entry = config_entry
200205
self._modbus_item = modbus_item
201206
self._attr_name = self._modbus_item.name
@@ -212,15 +217,16 @@ def __init__(self, config_entry, modbus_item) -> None:
212217
if self._modbus_item._format == FORMATS.POWER:
213218
self._attr_state_class = SensorStateClass.MEASUREMENT
214219

215-
if self._modbus_item.resultlist != None:
220+
if self._modbus_item.resultlist is not None:
216221
self._attr_native_min_value = self._modbus_item.getNumberFromText("min")
217222
self._attr_native_max_value = self._modbus_item.getNumberFromText("max")
218223
self._attr_native_step = self._modbus_item.getNumberFromText("step")
219224
self._divider = self._modbus_item.getNumberFromText("divider")
220225
self._attr_device_class = self._modbus_item.getTextFromNumber(-1)
221226

222227
def calcTemperature(self, val: float):
223-
if val == None:
228+
"""Calcualte temperature."""
229+
if val is None:
224230
return None
225231
if val == -32768:
226232
return -1
@@ -231,19 +237,20 @@ def calcTemperature(self, val: float):
231237
return val / self._divider
232238

233239
def calcPercentage(self, val: float):
234-
if val == None:
240+
"""Calculate percentage."""
241+
if val is None:
235242
return None
236243
if val == 65535:
237244
return None
238245
return val / self._divider
239246

240247
@property
241248
async def translateVal(self):
242-
# reads an translates a value from the modbus
249+
"""Read an translate a value from the modbus."""
243250
# mbo = ModbusObject(self._config_entry, self._modbus_item)
244251
mbo = None
245252

246-
if mbo == None:
253+
if mbo is None:
247254
return None
248255
val = await mbo.value
249256
match self._modbus_item.format:
@@ -254,17 +261,17 @@ async def translateVal(self):
254261
case FORMATS.STATUS:
255262
return self._modbus_item.getTextFromNumber(val)
256263
case _:
257-
if val == None:
264+
if val is None:
258265
return val
259266
return val / self._divider
260267

261268
# @translateVal.setter
262269
async def settranslateVal(self, value):
263-
# translates and writes a value to the modbus
270+
"""Translate and write a value to the modbus."""
264271
# mbo = ModbusObject(self._config_entry, self._modbus_item)
265272
mbo = None
266273

267-
if mbo == None:
274+
if mbo is None:
268275
return
269276
val = None
270277
match self._modbus_item.format:
@@ -276,7 +283,7 @@ async def settranslateVal(self, value):
276283
await mbo.setvalue(val) # = val
277284

278285
def my_device_info(self) -> DeviceInfo:
279-
# helper to build the device info
286+
"""Build the device info with this helper."""
280287
return {
281288
"identifiers": {(CONST.DOMAIN, self._dev_device)},
282289
"name": self._dev_device,
@@ -287,13 +294,18 @@ def my_device_info(self) -> DeviceInfo:
287294

288295

289296
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+
292303
_attr_native_unit_of_measurement = None
293304
_attr_device_class = None
294305
_attr_state_class = None
295306

296307
def __init__(self, config_entry, modbus_item, coordinator=None, idx=None) -> None:
308+
"""Initialize sensor entitiy."""
297309
super().__init__(coordinator, context=idx)
298310
self._idx = idx
299311
MyEntity.__init__(self, config_entry, modbus_item)
@@ -310,29 +322,37 @@ def _handle_coordinator_update(self) -> None:
310322

311323
@property
312324
def device_info(self) -> DeviceInfo:
325+
"""Return device info."""
313326
return MyEntity.my_device_info(self)
314327

315328

316329
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+
320337
my_map = PowerMap()
321338

322339
def __init__(self, config_entry, modbus_item) -> None:
340+
"""Initialize Sensor Entity."""
323341
MySensorEntity.__init__(self, config_entry, modbus_item)
324342

325343
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."""
327345
self._attr_native_value = await self.translateVal
328346

329347
def calcPower(self, val, x, y):
348+
"""Calculate power."""
330349
if val is None:
331350
return val
332351
return (val / 100) * self.my_map.map(x, y)
333352

334353
@property
335354
async def translateVal(self):
355+
"""Not sure."""
336356
# reads an translates a value from the modbus
337357
# mbo = ModbusObject(self._config_entry, self._modbus_item)
338358
# val = self.calcPercentage(await mbo.value)
@@ -373,47 +393,60 @@ async def translateVal(self):
373393

374394
@property
375395
def device_info(self) -> DeviceInfo:
396+
"""Return device info."""
376397
return MySensorEntity.my_device_info(self)
377398

378399

379400
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+
382407
_attr_native_unit_of_measurement = None
383408
_attr_device_class = None
384409
_attr_state_class = None
385410
_attr_native_min_value = 10
386411
_attr_native_max_value = 60
387412

388413
def __init__(self, config_entry, modbus_item) -> None:
414+
"""Initialize the number entity."""
389415
MyEntity.__init__(self, config_entry, modbus_item)
390416

391-
if self._modbus_item.resultlist != None:
417+
if self._modbus_item.resultlist is not None:
392418
self._attr_native_min_value = self._modbus_item.getNumberFromText("min")
393419
self._attr_native_max_value = self._modbus_item.getNumberFromText("max")
394420
self._attr_native_step = self._modbus_item.getNumberFromText("step")
395421

396422
async def async_set_native_value(self, value: float) -> None:
423+
"""Set the value."""
397424
await self.settranslateVal(value)
398425
self._attr_native_value = await self.translateVal
399426
self.async_write_ha_state()
400427

401428
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."""
403430
self._attr_native_value = await self.translateVal
404431

405432
@property
406433
def device_info(self) -> DeviceInfo:
434+
"""Return device info."""
407435
return MyEntity.my_device_info(self)
408436

409437

410438
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+
413445
options = []
414446
_attr_current_option = "FEHLER"
415447

416448
def __init__(self, config_entry, modbus_item) -> None:
449+
"""Initialize select entity."""
417450
MyEntity.__init__(self, config_entry, modbus_item)
418451
self.async_internal_will_remove_from_hass_port = self._config_entry.data[
419452
CONF_PORT
@@ -424,16 +457,17 @@ def __init__(self, config_entry, modbus_item) -> None:
424457
self.options.append(item.text)
425458

426459
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."""
428461
await self.settranslateVal(option)
429462
self._attr_current_option = await self.translateVal
430463
self.async_write_ha_state()
431464

432465
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."""
434467
# await self.coordinator.async_request_refresh()
435468
self._attr_current_option = await self.translateVal
436469

437470
@property
438471
def device_info(self) -> DeviceInfo:
472+
"""Return device info."""
439473
return MyEntity.my_device_info(self)

0 commit comments

Comments
 (0)