Move screenlogic SCG levels to number platform (#60872)

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Kevin Worrel 2021-12-02 18:36:31 -08:00 committed by GitHub
parent 4758a4fdc8
commit 9d1985ab03
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 80 additions and 3 deletions

View file

@ -915,6 +915,7 @@ omit =
homeassistant/components/screenlogic/binary_sensor.py
homeassistant/components/screenlogic/climate.py
homeassistant/components/screenlogic/light.py
homeassistant/components/screenlogic/number.py
homeassistant/components/screenlogic/sensor.py
homeassistant/components/screenlogic/services.py
homeassistant/components/screenlogic/switch.py

View file

@ -40,7 +40,7 @@ HEATER_COOLDOWN_DELAY = 6
# These seem to be constant across all controller models
PRIMARY_CIRCUIT_IDS = [500, 505] # [Spa, Pool]
PLATFORMS = ["binary_sensor", "climate", "light", "sensor", "switch"]
PLATFORMS = ["binary_sensor", "climate", "light", "number", "sensor", "switch"]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

View file

@ -0,0 +1,78 @@
"""Support for a ScreenLogic number entity."""
import logging
from screenlogicpy.const import BODY_TYPE, DATA as SL_DATA, EQUIPMENT, SCG
from homeassistant.components.number import NumberEntity
from . import ScreenlogicEntity
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
SUPPORTED_SCG_NUMBERS = (
"scg_level1",
"scg_level2",
)
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up entry."""
coordinator = hass.data[DOMAIN][config_entry.entry_id]
equipment_flags = coordinator.data[SL_DATA.KEY_CONFIG]["equipment_flags"]
if equipment_flags & EQUIPMENT.FLAG_CHLORINATOR:
async_add_entities(
[
ScreenLogicNumber(coordinator, scg_level)
for scg_level in coordinator.data[SL_DATA.KEY_SCG]
if scg_level in SUPPORTED_SCG_NUMBERS
]
)
class ScreenLogicNumber(ScreenlogicEntity, NumberEntity):
"""Class to represent a ScreenLogic Number."""
def __init__(self, coordinator, data_key, enabled=True):
"""Initialize of the entity."""
super().__init__(coordinator, data_key, enabled)
self._body_type = SUPPORTED_SCG_NUMBERS.index(self._data_key)
self._attr_max_value = SCG.LIMIT_FOR_BODY[self._body_type]
self._attr_name = f"{self.gateway_name} {self.sensor['name']}"
self._attr_unit_of_measurement = self.sensor["unit"]
@property
def value(self) -> float:
"""Return the current value."""
return self.sensor["value"]
async def async_set_value(self, value: float) -> None:
"""Update the current value."""
# Need to set both levels at the same time, so we gather
# both existing level values and override the one that changed.
levels = {}
for level in SUPPORTED_SCG_NUMBERS:
levels[level] = self.coordinator.data[SL_DATA.KEY_SCG][level]["value"]
levels[self._data_key] = int(value)
if await self.coordinator.gateway.async_set_scg_config(
levels[SUPPORTED_SCG_NUMBERS[BODY_TYPE.POOL]],
levels[SUPPORTED_SCG_NUMBERS[BODY_TYPE.SPA]],
):
_LOGGER.debug(
"Set SCG to %i, %i",
levels[SUPPORTED_SCG_NUMBERS[BODY_TYPE.POOL]],
levels[SUPPORTED_SCG_NUMBERS[BODY_TYPE.SPA]],
)
await self._async_refresh()
else:
_LOGGER.warning(
"Failed to set_scg to %i, %i",
levels[SUPPORTED_SCG_NUMBERS[BODY_TYPE.POOL]],
levels[SUPPORTED_SCG_NUMBERS[BODY_TYPE.SPA]],
)
@property
def sensor(self) -> dict:
"""Shortcut to access the level sensor data."""
return self.coordinator.data[SL_DATA.KEY_SCG][self._data_key]

View file

@ -34,8 +34,6 @@ SUPPORTED_CHEM_SENSORS = (
)
SUPPORTED_SCG_SENSORS = (
"scg_level1",
"scg_level2",
"scg_salt_ppm",
"scg_super_chlor_timer",
)