Skip to content

Commit

Permalink
Merge pull request #16 from dwradcliffe/chlorine_output
Browse files Browse the repository at this point in the history
add number entity for chlorinator output
  • Loading branch information
jlvaillant committed Jun 5, 2021
2 parents 1085ebc + 9594cc3 commit 4564405
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 6 deletions.
15 changes: 12 additions & 3 deletions custom_components/intellicenter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -67,6 +68,7 @@
SWITCH_DOMAIN,
BINARY_SENSOR_DOMAIN,
WATER_HEATER_DOMAIN,
NUMBER_DOMAIN,
]

# -------------------------------------------------------------------------------------
Expand Down Expand Up @@ -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
Expand All @@ -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}")
Expand Down Expand Up @@ -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."""
Expand Down
105 changes: 105 additions & 0 deletions custom_components/intellicenter/number.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 2 additions & 0 deletions custom_components/intellicenter/pyintellicenter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
PHTNK_ATTR,
PHVAL_ATTR,
PMPCIRC_TYPE,
PRIM_ATTR,
PROPNAME_ATTR,
PUMP_TYPE,
PWR_ATTR,
Expand Down Expand Up @@ -109,6 +110,7 @@
PARENT_ATTR,
PHTNK_ATTR,
PHVAL_ATTR,
PRIM_ATTR,
PROPNAME_ATTR,
PWR_ATTR,
QUALTY_ATTR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 ??
Expand Down
2 changes: 0 additions & 2 deletions custom_components/intellicenter/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4564405

Please sign in to comment.