Skip to content

Commit a831d65

Browse files
authored
Merge pull request #55 from dwradcliffe/attrs
refactor to use modern attr patterns
2 parents 48f505a + 2fb4cdf commit a831d65

File tree

6 files changed

+35
-75
lines changed

6 files changed

+35
-75
lines changed

custom_components/intellicenter/__init__.py

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,14 @@ def __init__(
223223
self._entry_id = entry.entry_id
224224
self._controller = controller
225225
self._poolObject = poolObject
226-
self._available = True
226+
self._attr_available = True
227227
self._extra_state_attributes = extraStateAttributes
228-
self._name = name
228+
self._attr_name = name
229229
self._attribute_key = attribute_key
230-
self._enabled_by_default = enabled_by_default
231-
self._unit_of_measurement = unit_of_measurement
232-
self._icon = icon
230+
self._attr_entity_registry_enabled_default = enabled_by_default
231+
self._attr_native_unit_of_measurement = unit_of_measurement
232+
self._attr_icon = icon
233+
self._attr_should_poll = False
233234

234235
_LOGGER.debug(f"mapping {poolObject}")
235236

@@ -253,38 +254,18 @@ async def async_will_remove_from_hass(self) -> None:
253254
"""Entity is removed from Home Assistant."""
254255
_LOGGER.debug(f"removing entity: {self.unique_id}")
255256

256-
@property
257-
def entity_registry_enabled_default(self):
258-
"""Return True if the entity is enabled by default."""
259-
return self._enabled_by_default
260-
261-
@property
262-
def available(self):
263-
"""Return True is the entity is available."""
264-
return self._available
265-
266257
@property
267258
def name(self):
268259
"""Return the name of the entity."""
269260

270-
if self._name is None:
261+
if self._attr_name is None:
271262
# default is to return the name of the underlying pool object
272263
return self._poolObject.sname
273-
elif self._name.startswith("+"):
264+
elif self._attr_name.startswith("+"):
274265
# name is a suffix
275-
return self._poolObject.sname + self._name[1:]
266+
return self._poolObject.sname + self._attr_name[1:]
276267
else:
277-
return self._name
278-
279-
@property
280-
def icon(self) -> Optional[str]:
281-
"""Return the icon for the entity, if any."""
282-
return self._icon
283-
284-
@property
285-
def unit_of_measurement(self) -> Optional[str]:
286-
"""Return the unit of measurement of this entity, if any."""
287-
return self._unit_of_measurement
268+
return self._attr_name
288269

289270
@property
290271
def unique_id(self):
@@ -294,11 +275,6 @@ def unique_id(self):
294275
my_id += self._attribute_key
295276
return my_id
296277

297-
@property
298-
def should_poll(self):
299-
"""No polling needed."""
300-
return False
301-
302278
@property
303279
def device_info(self):
304280
"""Return the device info."""
@@ -353,7 +329,7 @@ def _update_callback(self, updates: Dict[str, Dict[str, str]]):
353329
"""Update the entity if its underlying pool object has changed."""
354330

355331
if self.isUpdated(updates):
356-
self._available = True
332+
self._attr_available = True
357333
_LOGGER.debug(f"updating {self} from {updates}")
358334
self.async_write_ha_state()
359335

@@ -366,7 +342,7 @@ def _connection_callback(self, is_connected):
366342
# this is for the rare case where the object the entity is mapped to
367343
# had been removed from the Pentair system while we were disconnected
368344
return
369-
self._available = is_connected
345+
self._attr_available = is_connected
370346
self.async_write_ha_state()
371347

372348
def pentairTemperatureSettings(self):

custom_components/intellicenter/binary_sensor.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ async def async_setup_entry(
3333
object: PoolObject
3434
for object in controller.model.objectList:
3535
if object.objtype == CIRCUIT_TYPE and object.subtype == "FRZ":
36-
sensors.append(PoolBinarySensor(entry, controller, object))
36+
sensors.append(
37+
PoolBinarySensor(
38+
entry,
39+
controller,
40+
object,
41+
icon = "mdi:snowflake"
42+
)
43+
)
3744
elif object.objtype == HEATER_TYPE:
3845
sensors.append(
3946
HeaterBinarySensor(
@@ -99,6 +106,7 @@ def __init__(
99106
"""Initialize."""
100107
super().__init__(entry, controller, poolObject, **kwargs)
101108
self._bodies = set(poolObject[BODY_ATTR].split(" "))
109+
self._attr_icon = "mdi:fire-circle"
102110

103111
@property
104112
def is_on(self) -> bool:

custom_components/intellicenter/number.py

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Pentair Intellicenter numbers."""
22

33
import logging
4-
from typing import Optional
54

65
from homeassistant.components.number import (
76
NumberEntity,
@@ -51,7 +50,6 @@ async def async_setup_entry(
5150
unit_of_measurement=PERCENTAGE,
5251
attribute_key=PRIM_ATTR,
5352
name="+ Output %",
54-
icon="mdi:gauge",
5553
)
5654
)
5755
async_add_entities(numbers)
@@ -61,7 +59,7 @@ async def async_setup_entry(
6159

6260

6361
class PoolNumber(PoolEntity, NumberEntity):
64-
"""Representation of a number."""
62+
"""Representation of a pool number entity."""
6563

6664
def __init__(
6765
self,
@@ -75,31 +73,17 @@ def __init__(
7573
):
7674
"""Initialize."""
7775
super().__init__(entry, controller, poolObject, **kwargs)
78-
self._min_value = min_value
79-
self._max_value = max_value
80-
self._step = step
76+
self._attr_native_min_value = min_value
77+
self._attr_native_max_value = max_value
78+
self._attr_native_step = step
79+
self._attr_icon = "mdi:gauge"
8180

8281
@property
83-
def min_value(self) -> float:
84-
"""Return the minimum value."""
85-
return self._min_value
86-
87-
@property
88-
def max_value(self) -> float:
89-
"""Return the maximum value."""
90-
return self._max_value
91-
92-
@property
93-
def step(self) -> float:
94-
"""Return the increment/decrement step."""
95-
return self._step
96-
97-
@property
98-
def value(self) -> float:
82+
def native_value(self) -> float:
9983
"""Return the current value."""
10084
return self._poolObject[self._attribute_key]
10185

102-
def set_value(self, value: float) -> None:
86+
def set_native_value(self, value: float) -> None:
10387
"""Update the current value."""
10488
changes = {self._attribute_key: str(int(value))}
10589
self.requestChanges(changes)

custom_components/intellicenter/sensor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ def state(self) -> str:
233233
return value
234234

235235
@property
236-
def unit_of_measurement(self) -> Optional[str]:
236+
def native_unit_of_measurement(self) -> Optional[str]:
237237
"""Return the unit of measurement of this entity, if any."""
238238
if self._attr_device_class == SensorDeviceClass.TEMPERATURE:
239239
return self.pentairTemperatureSettings()
240-
return self._unit_of_measurement
240+
return self._attr_native_unit_of_measurement

custom_components/intellicenter/switch.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525

2626
_LOGGER = logging.getLogger(__name__)
2727

28-
# FIXME: for freeze swtch use icon mdi:snowflake
29-
3028
# -------------------------------------------------------------------------------------
3129

3230

@@ -62,7 +60,8 @@ async def async_setup_entry(
6260
and not (object.isALight or object.isALightShow)
6361
and object.isFeatured
6462
):
65-
switches.append(PoolCircuit(entry, controller, object))
63+
switches.append(
64+
PoolCircuit(entry, controller, object, icon="mdi:alpha-f-box-outline"))
6665
elif object.objtype == SYSTEM_TYPE:
6766
switches.append(
6867
PoolCircuit(
@@ -71,6 +70,7 @@ async def async_setup_entry(
7170
object,
7271
VACFLO_ATTR,
7372
name="Vacation mode",
73+
icon="mdi:palm-tree",
7474
enabled_by_default=False,
7575
)
7676
)
@@ -108,8 +108,4 @@ def __init__(self, entry: ConfigEntry, controller, poolObject):
108108
"""Initialize a Pool body from the underlying circuit."""
109109
super().__init__(entry, controller, poolObject)
110110
self._extra_state_attributes = [VOL_ATTR, HEATER_ATTR, HTMODE_ATTR]
111-
112-
@property
113-
def icon(self):
114-
"""Return the icon for the entity."""
115-
return "mdi:pool"
111+
self._attr_icon = "mdi:pool"

custom_components/intellicenter/water_heater.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def __init__(
9292
)
9393
self._heater_list = heater_list
9494
self._lastHeater = self._poolObject[HEATER_ATTR]
95+
self._attr_icon = "mdi:thermometer"
9596

9697
@property
9798
def extra_state_attributes(self) -> Optional[Dict[str, Any]]:
@@ -124,11 +125,6 @@ def supported_features(self):
124125
"""Return the list of supported features."""
125126
return SUPPORT_TARGET_TEMPERATURE | SUPPORT_OPERATION_MODE
126127

127-
@property
128-
def icon(self):
129-
"""Return the entity icon."""
130-
return "mdi:thermometer"
131-
132128
@property
133129
def temperature_unit(self):
134130
"""Return the unit of measurement used by the platform."""

0 commit comments

Comments
 (0)