From 9594cc3326b4f2dcb3a7fbd97635bb1a1fafbd7a Mon Sep 17 00:00:00 2001 From: David Radcliffe Date: Thu, 6 May 2021 18:00:44 -0400 Subject: [PATCH] add number entity for chlorinator output --- custom_components/intellicenter/__init__.py | 15 ++- custom_components/intellicenter/number.py | 105 ++++++++++++++++++ .../intellicenter/pyintellicenter/__init__.py | 2 + .../pyintellicenter/attributes.py | 3 +- custom_components/intellicenter/sensor.py | 2 - 5 files changed, 121 insertions(+), 6 deletions(-) create mode 100644 custom_components/intellicenter/number.py diff --git a/custom_components/intellicenter/__init__.py b/custom_components/intellicenter/__init__.py index 3eaee13..0a1a8a6 100644 --- a/custom_components/intellicenter/__init__.py +++ b/custom_components/intellicenter/__init__.py @@ -5,6 +5,7 @@ from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN +from homeassistant.components.number import DOMAIN as NUMBER_DOMAIN from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN from homeassistant.components.water_heater import DOMAIN as WATER_HEATER_DOMAIN @@ -67,6 +68,7 @@ SWITCH_DOMAIN, BINARY_SENSOR_DOMAIN, WATER_HEATER_DOMAIN, + NUMBER_DOMAIN, ] # ------------------------------------------------------------------------------------- @@ -214,7 +216,8 @@ def __init__( name=None, enabled_by_default=True, extraStateAttributes=set(), - icon=None, + icon: str = None, + unit_of_measurement: str = None, ): """Initialize a Pool entity.""" self._entry_id = entry.entry_id @@ -225,6 +228,7 @@ def __init__( self._name = name self._attribute_key = attribute_key self._enabled_by_default = enabled_by_default + self._unit_of_measurement = unit_of_measurement self._icon = icon _LOGGER.debug(f"mapping {poolObject}") @@ -273,10 +277,15 @@ def name(self): return self._name @property - def icon(self): - """Return the icon for the entity.""" + 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 + @property def unique_id(self): """Return a unique ID.""" diff --git a/custom_components/intellicenter/number.py b/custom_components/intellicenter/number.py new file mode 100644 index 0000000..752f2b9 --- /dev/null +++ b/custom_components/intellicenter/number.py @@ -0,0 +1,105 @@ +"""Pentair Intellicenter numbers.""" + +import logging +from typing import Optional + +from homeassistant.components.number import ( + NumberEntity, + DEFAULT_MIN_VALUE, + DEFAULT_MAX_VALUE, + DEFAULT_STEP, +) +from homeassistant.config_entries import ConfigEntry +from homeassistant.helpers.typing import HomeAssistantType + +from . import PoolEntity +from .const import CONST_RPM, DOMAIN +from homeassistant.const import PERCENTAGE +from .pyintellicenter import ( + CHEM_TYPE, + PRIM_ATTR, + ModelController, + PoolObject, +) + +_LOGGER = logging.getLogger(__name__) + +# ------------------------------------------------------------------------------------- + + +async def async_setup_entry( + hass: HomeAssistantType, entry: ConfigEntry, async_add_entities +): + """Load pool numbers based on a config entry.""" + + controller: ModelController = hass.data[DOMAIN][entry.entry_id].controller + + numbers = [] + + object: PoolObject + for object in controller.model.objectList: + if ( + object.objtype == CHEM_TYPE + and object.subtype == "ICHLOR" + and PRIM_ATTR in object.attributes + ): + numbers.append( + PoolNumber( + entry, + controller, + object, + unit_of_measurement=PERCENTAGE, + attribute_key=PRIM_ATTR, + name="+ Output %", + icon="mdi:gauge", + ) + ) + async_add_entities(numbers) + + +# ------------------------------------------------------------------------------------- + + +class PoolNumber(PoolEntity, NumberEntity): + """Representation of a number.""" + + def __init__( + self, + entry: ConfigEntry, + controller: ModelController, + poolObject: PoolObject, + min_value: float = DEFAULT_MIN_VALUE, + max_value: float = DEFAULT_MAX_VALUE, + step: float = DEFAULT_STEP, + **kwargs, + ): + """Initialize.""" + super().__init__(entry, controller, poolObject, **kwargs) + self._min_value = min_value + self._max_value = max_value + self._step = step + + @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: + """Return the current value.""" + return self._poolObject[self._attribute_key] + + def set_value(self, value: float) -> None: + """Update the current value.""" + changes = {self._attribute_key: str(int(value))} + self.requestChanges(changes) diff --git a/custom_components/intellicenter/pyintellicenter/__init__.py b/custom_components/intellicenter/pyintellicenter/__init__.py index 11a7b74..477ab94 100644 --- a/custom_components/intellicenter/pyintellicenter/__init__.py +++ b/custom_components/intellicenter/pyintellicenter/__init__.py @@ -30,6 +30,7 @@ PHTNK_ATTR, PHVAL_ATTR, PMPCIRC_TYPE, + PRIM_ATTR, PROPNAME_ATTR, PUMP_TYPE, PWR_ATTR, @@ -109,6 +110,7 @@ PARENT_ATTR, PHTNK_ATTR, PHVAL_ATTR, + PRIM_ATTR, PROPNAME_ATTR, PWR_ATTR, QUALTY_ATTR, diff --git a/custom_components/intellicenter/pyintellicenter/attributes.py b/custom_components/intellicenter/pyintellicenter/attributes.py index 143194d..4d47eca 100644 --- a/custom_components/intellicenter/pyintellicenter/attributes.py +++ b/custom_components/intellicenter/pyintellicenter/attributes.py @@ -37,6 +37,7 @@ PARENT_ATTR = "PARENT" PHVAL_ATTR = "PHVAL" PHTNK_ATTR = "PHTNK" +PRIM_ATTR = "PRIM" PROPNAME_ATTR = "PROPNAME" PWR_ATTR = "PWR" QUALTY_ATTR = "QUALTY" @@ -132,7 +133,7 @@ "PHSET", # (float) IntelliChem Ph level setting PHTNK_ATTR, # (int) IntelliChem: Ph Tank Level PHVAL_ATTR, # (float) IntelliChem: Ph Level - "PRIM", # (int) Intellichor: output setting in % + PRIM_ATTR, # (int) Intellichor: output setting in % QUALTY_ATTR, # (float) Intellichem: Water Quality (Saturation Index) SALT_ATTR, # (int) Salt level "SEC", # (int) IntelliChlor ?? diff --git a/custom_components/intellicenter/sensor.py b/custom_components/intellicenter/sensor.py index ed45523..33c83ba 100644 --- a/custom_components/intellicenter/sensor.py +++ b/custom_components/intellicenter/sensor.py @@ -205,14 +205,12 @@ def __init__( controller: ModelController, poolObject: PoolObject, device_class: str, - unit_of_measurement: str = None, rounding_factor: int = 0, **kwargs, ): """Initialize.""" super().__init__(entry, controller, poolObject, **kwargs) self._device_class = device_class - self._unit_of_measurement = unit_of_measurement self._rounding_factor = rounding_factor @property