Skip to content

Commit

Permalink
Merge pull request #55 from dwradcliffe/attrs
Browse files Browse the repository at this point in the history
refactor to use modern attr patterns
  • Loading branch information
jlvaillant authored Nov 20, 2022
2 parents 48f505a + 2fb4cdf commit a831d65
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 75 deletions.
48 changes: 12 additions & 36 deletions custom_components/intellicenter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,14 @@ def __init__(
self._entry_id = entry.entry_id
self._controller = controller
self._poolObject = poolObject
self._available = True
self._attr_available = True
self._extra_state_attributes = extraStateAttributes
self._name = name
self._attr_name = name
self._attribute_key = attribute_key
self._enabled_by_default = enabled_by_default
self._unit_of_measurement = unit_of_measurement
self._icon = icon
self._attr_entity_registry_enabled_default = enabled_by_default
self._attr_native_unit_of_measurement = unit_of_measurement
self._attr_icon = icon
self._attr_should_poll = False

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

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

@property
def entity_registry_enabled_default(self):
"""Return True if the entity is enabled by default."""
return self._enabled_by_default

@property
def available(self):
"""Return True is the entity is available."""
return self._available

@property
def name(self):
"""Return the name of the entity."""

if self._name is None:
if self._attr_name is None:
# default is to return the name of the underlying pool object
return self._poolObject.sname
elif self._name.startswith("+"):
elif self._attr_name.startswith("+"):
# name is a suffix
return self._poolObject.sname + self._name[1:]
return self._poolObject.sname + self._attr_name[1:]
else:
return self._name

@property
def icon(self) -> Optional[str]:
"""Return the icon for the entity, if any."""
return self._icon

@property
def unit_of_measurement(self) -> Optional[str]:
"""Return the unit of measurement of this entity, if any."""
return self._unit_of_measurement
return self._attr_name

@property
def unique_id(self):
Expand All @@ -294,11 +275,6 @@ def unique_id(self):
my_id += self._attribute_key
return my_id

@property
def should_poll(self):
"""No polling needed."""
return False

@property
def device_info(self):
"""Return the device info."""
Expand Down Expand Up @@ -353,7 +329,7 @@ def _update_callback(self, updates: Dict[str, Dict[str, str]]):
"""Update the entity if its underlying pool object has changed."""

if self.isUpdated(updates):
self._available = True
self._attr_available = True
_LOGGER.debug(f"updating {self} from {updates}")
self.async_write_ha_state()

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

def pentairTemperatureSettings(self):
Expand Down
10 changes: 9 additions & 1 deletion custom_components/intellicenter/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ async def async_setup_entry(
object: PoolObject
for object in controller.model.objectList:
if object.objtype == CIRCUIT_TYPE and object.subtype == "FRZ":
sensors.append(PoolBinarySensor(entry, controller, object))
sensors.append(
PoolBinarySensor(
entry,
controller,
object,
icon = "mdi:snowflake"
)
)
elif object.objtype == HEATER_TYPE:
sensors.append(
HeaterBinarySensor(
Expand Down Expand Up @@ -99,6 +106,7 @@ def __init__(
"""Initialize."""
super().__init__(entry, controller, poolObject, **kwargs)
self._bodies = set(poolObject[BODY_ATTR].split(" "))
self._attr_icon = "mdi:fire-circle"

@property
def is_on(self) -> bool:
Expand Down
30 changes: 7 additions & 23 deletions custom_components/intellicenter/number.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Pentair Intellicenter numbers."""

import logging
from typing import Optional

from homeassistant.components.number import (
NumberEntity,
Expand Down Expand Up @@ -51,7 +50,6 @@ async def async_setup_entry(
unit_of_measurement=PERCENTAGE,
attribute_key=PRIM_ATTR,
name="+ Output %",
icon="mdi:gauge",
)
)
async_add_entities(numbers)
Expand All @@ -61,7 +59,7 @@ async def async_setup_entry(


class PoolNumber(PoolEntity, NumberEntity):
"""Representation of a number."""
"""Representation of a pool number entity."""

def __init__(
self,
Expand All @@ -75,31 +73,17 @@ def __init__(
):
"""Initialize."""
super().__init__(entry, controller, poolObject, **kwargs)
self._min_value = min_value
self._max_value = max_value
self._step = step
self._attr_native_min_value = min_value
self._attr_native_max_value = max_value
self._attr_native_step = step
self._attr_icon = "mdi:gauge"

@property
def min_value(self) -> float:
"""Return the minimum value."""
return self._min_value

@property
def max_value(self) -> float:
"""Return the maximum value."""
return self._max_value

@property
def step(self) -> float:
"""Return the increment/decrement step."""
return self._step

@property
def value(self) -> float:
def native_value(self) -> float:
"""Return the current value."""
return self._poolObject[self._attribute_key]

def set_value(self, value: float) -> None:
def set_native_value(self, value: float) -> None:
"""Update the current value."""
changes = {self._attribute_key: str(int(value))}
self.requestChanges(changes)
4 changes: 2 additions & 2 deletions custom_components/intellicenter/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ def state(self) -> str:
return value

@property
def unit_of_measurement(self) -> Optional[str]:
def native_unit_of_measurement(self) -> Optional[str]:
"""Return the unit of measurement of this entity, if any."""
if self._attr_device_class == SensorDeviceClass.TEMPERATURE:
return self.pentairTemperatureSettings()
return self._unit_of_measurement
return self._attr_native_unit_of_measurement
12 changes: 4 additions & 8 deletions custom_components/intellicenter/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

_LOGGER = logging.getLogger(__name__)

# FIXME: for freeze swtch use icon mdi:snowflake

# -------------------------------------------------------------------------------------


Expand Down Expand Up @@ -62,7 +60,8 @@ async def async_setup_entry(
and not (object.isALight or object.isALightShow)
and object.isFeatured
):
switches.append(PoolCircuit(entry, controller, object))
switches.append(
PoolCircuit(entry, controller, object, icon="mdi:alpha-f-box-outline"))
elif object.objtype == SYSTEM_TYPE:
switches.append(
PoolCircuit(
Expand All @@ -71,6 +70,7 @@ async def async_setup_entry(
object,
VACFLO_ATTR,
name="Vacation mode",
icon="mdi:palm-tree",
enabled_by_default=False,
)
)
Expand Down Expand Up @@ -108,8 +108,4 @@ def __init__(self, entry: ConfigEntry, controller, poolObject):
"""Initialize a Pool body from the underlying circuit."""
super().__init__(entry, controller, poolObject)
self._extra_state_attributes = [VOL_ATTR, HEATER_ATTR, HTMODE_ATTR]

@property
def icon(self):
"""Return the icon for the entity."""
return "mdi:pool"
self._attr_icon = "mdi:pool"
6 changes: 1 addition & 5 deletions custom_components/intellicenter/water_heater.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def __init__(
)
self._heater_list = heater_list
self._lastHeater = self._poolObject[HEATER_ATTR]
self._attr_icon = "mdi:thermometer"

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

@property
def icon(self):
"""Return the entity icon."""
return "mdi:thermometer"

@property
def temperature_unit(self):
"""Return the unit of measurement used by the platform."""
Expand Down

0 comments on commit a831d65

Please sign in to comment.