Bump screenlogicpy to v0.9.0 (#92475)

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Kevin Worrel 2023-09-09 15:39:54 -07:00 committed by GitHub
parent 8de3945bd4
commit 092580a3ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 3821 additions and 652 deletions

View file

@ -1,76 +1,148 @@
"""Support for a ScreenLogic Sensor."""
from typing import Any
from collections.abc import Callable
from dataclasses import dataclass
import logging
from screenlogicpy.const import (
CHEM_DOSING_STATE,
CODE,
DATA as SL_DATA,
DEVICE_TYPE,
EQUIPMENT,
STATE_TYPE,
UNIT,
)
from screenlogicpy.const.common import DEVICE_TYPE, STATE_TYPE
from screenlogicpy.const.data import ATTR, DEVICE, GROUP, VALUE
from screenlogicpy.device_const.chemistry import DOSE_STATE
from screenlogicpy.device_const.pump import PUMP_TYPE
from screenlogicpy.device_const.system import EQUIPMENT_FLAG
from homeassistant.components.sensor import (
DOMAIN,
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONCENTRATION_PARTS_PER_MILLION,
PERCENTAGE,
REVOLUTIONS_PER_MINUTE,
EntityCategory,
UnitOfElectricPotential,
UnitOfPower,
UnitOfTemperature,
UnitOfTime,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import ScreenlogicDataUpdateCoordinator
from .const import DOMAIN
from .entity import ScreenlogicEntity, ScreenLogicPushEntity
SUPPORTED_BASIC_SENSORS = (
"air_temperature",
"saturation",
from .const import DOMAIN as SL_DOMAIN, ScreenLogicDataPath
from .coordinator import ScreenlogicDataUpdateCoordinator
from .data import (
DEVICE_INCLUSION_RULES,
DEVICE_SUBSCRIPTION,
PathPart,
ScreenLogicDataRule,
ScreenLogicEquipmentRule,
SupportedValueParameters,
build_base_entity_description,
get_ha_unit,
iterate_expand_group_wildcard,
preprocess_supported_values,
)
SUPPORTED_BASIC_CHEM_SENSORS = (
"orp",
"ph",
from .entity import (
ScreenlogicEntity,
ScreenLogicEntityDescription,
ScreenLogicPushEntity,
ScreenLogicPushEntityDescription,
)
from .util import cleanup_excluded_entity, generate_unique_id
SUPPORTED_CHEM_SENSORS = (
"calcium_harness",
"current_orp",
"current_ph",
"cya",
"orp_dosing_state",
"orp_last_dose_time",
"orp_last_dose_volume",
"orp_setpoint",
"orp_supply_level",
"ph_dosing_state",
"ph_last_dose_time",
"ph_last_dose_volume",
"ph_probe_water_temp",
"ph_setpoint",
"ph_supply_level",
"salt_tds_ppm",
"total_alkalinity",
_LOGGER = logging.getLogger(__name__)
@dataclass
class SupportedSensorValueParameters(SupportedValueParameters):
"""Supported predefined data for a ScreenLogic sensor entity."""
device_class: SensorDeviceClass | None = None
value_modification: Callable[[int], int | str] | None = lambda val: val
SUPPORTED_DATA: list[
tuple[ScreenLogicDataPath, SupportedValueParameters]
] = preprocess_supported_values(
{
DEVICE.CONTROLLER: {
GROUP.SENSOR: {
VALUE.AIR_TEMPERATURE: SupportedSensorValueParameters(
device_class=SensorDeviceClass.TEMPERATURE, entity_category=None
),
VALUE.ORP: SupportedSensorValueParameters(
included=ScreenLogicEquipmentRule(
lambda flags: EQUIPMENT_FLAG.INTELLICHEM in flags
)
),
VALUE.PH: SupportedSensorValueParameters(
included=ScreenLogicEquipmentRule(
lambda flags: EQUIPMENT_FLAG.INTELLICHEM in flags
)
),
},
},
DEVICE.PUMP: {
"*": {
VALUE.WATTS_NOW: SupportedSensorValueParameters(),
VALUE.GPM_NOW: SupportedSensorValueParameters(
enabled=ScreenLogicDataRule(
lambda pump_data: pump_data[VALUE.TYPE]
!= PUMP_TYPE.INTELLIFLO_VS,
(PathPart.DEVICE, PathPart.INDEX),
)
),
VALUE.RPM_NOW: SupportedSensorValueParameters(
enabled=ScreenLogicDataRule(
lambda pump_data: pump_data[VALUE.TYPE]
!= PUMP_TYPE.INTELLIFLO_VF,
(PathPart.DEVICE, PathPart.INDEX),
)
),
},
},
DEVICE.INTELLICHEM: {
GROUP.SENSOR: {
VALUE.ORP_NOW: SupportedSensorValueParameters(),
VALUE.ORP_SUPPLY_LEVEL: SupportedSensorValueParameters(
value_modification=lambda val: val - 1
),
VALUE.PH_NOW: SupportedSensorValueParameters(),
VALUE.PH_PROBE_WATER_TEMP: SupportedSensorValueParameters(),
VALUE.PH_SUPPLY_LEVEL: SupportedSensorValueParameters(
value_modification=lambda val: val - 1
),
VALUE.SATURATION: SupportedSensorValueParameters(),
},
GROUP.CONFIGURATION: {
VALUE.CALCIUM_HARNESS: SupportedSensorValueParameters(),
VALUE.CYA: SupportedSensorValueParameters(),
VALUE.ORP_SETPOINT: SupportedSensorValueParameters(),
VALUE.PH_SETPOINT: SupportedSensorValueParameters(),
VALUE.SALT_TDS_PPM: SupportedSensorValueParameters(
included=ScreenLogicEquipmentRule(
lambda flags: EQUIPMENT_FLAG.INTELLICHEM in flags
and EQUIPMENT_FLAG.CHLORINATOR not in flags,
)
),
VALUE.TOTAL_ALKALINITY: SupportedSensorValueParameters(),
},
GROUP.DOSE_STATUS: {
VALUE.ORP_DOSING_STATE: SupportedSensorValueParameters(
value_modification=lambda val: DOSE_STATE(val).title,
),
VALUE.ORP_LAST_DOSE_TIME: SupportedSensorValueParameters(),
VALUE.ORP_LAST_DOSE_VOLUME: SupportedSensorValueParameters(),
VALUE.PH_DOSING_STATE: SupportedSensorValueParameters(
value_modification=lambda val: DOSE_STATE(val).title,
),
VALUE.PH_LAST_DOSE_TIME: SupportedSensorValueParameters(),
VALUE.PH_LAST_DOSE_VOLUME: SupportedSensorValueParameters(),
},
},
DEVICE.SCG: {
GROUP.SENSOR: {
VALUE.SALT_PPM: SupportedSensorValueParameters(),
},
GROUP.CONFIGURATION: {
VALUE.SUPER_CHLOR_TIMER: SupportedSensorValueParameters(),
},
},
}
)
SUPPORTED_SCG_SENSORS = (
"scg_salt_ppm",
"scg_super_chlor_timer",
)
SUPPORTED_PUMP_SENSORS = ("currentWatts", "currentRPM", "currentGPM")
SL_DEVICE_TYPE_TO_HA_DEVICE_CLASS = {
DEVICE_TYPE.DURATION: SensorDeviceClass.DURATION,
DEVICE_TYPE.ENUM: SensorDeviceClass.ENUM,
@ -85,18 +157,6 @@ SL_STATE_TYPE_TO_HA_STATE_CLASS = {
STATE_TYPE.TOTAL_INCREASING: SensorStateClass.TOTAL_INCREASING,
}
SL_UNIT_TO_HA_UNIT = {
UNIT.CELSIUS: UnitOfTemperature.CELSIUS,
UNIT.FAHRENHEIT: UnitOfTemperature.FAHRENHEIT,
UNIT.MILLIVOLT: UnitOfElectricPotential.MILLIVOLT,
UNIT.WATT: UnitOfPower.WATT,
UNIT.HOUR: UnitOfTime.HOURS,
UNIT.SECOND: UnitOfTime.SECONDS,
UNIT.REVOLUTIONS_PER_MINUTE: REVOLUTIONS_PER_MINUTE,
UNIT.PARTS_PER_MILLION: CONCENTRATION_PARTS_PER_MILLION,
UNIT.PERCENT: PERCENTAGE,
}
async def async_setup_entry(
hass: HomeAssistant,
@ -104,171 +164,110 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up entry."""
entities: list[ScreenLogicSensorEntity] = []
coordinator: ScreenlogicDataUpdateCoordinator = hass.data[DOMAIN][
entities: list[ScreenLogicSensor] = []
coordinator: ScreenlogicDataUpdateCoordinator = hass.data[SL_DOMAIN][
config_entry.entry_id
]
equipment_flags = coordinator.gateway_data[SL_DATA.KEY_CONFIG]["equipment_flags"]
gateway = coordinator.gateway
data_path: ScreenLogicDataPath
value_params: SupportedSensorValueParameters
for data_path, value_params in iterate_expand_group_wildcard(
gateway, SUPPORTED_DATA
):
entity_key = generate_unique_id(*data_path)
# Generic push sensors
for sensor_name in coordinator.gateway_data[SL_DATA.KEY_SENSORS]:
if sensor_name in SUPPORTED_BASIC_SENSORS:
entities.append(
ScreenLogicStatusSensor(coordinator, sensor_name, CODE.STATUS_CHANGED)
)
device = data_path[0]
# While these values exist in the chemistry data, their last value doesn't
# persist there when the pump is off/there is no flow. Pulling them from
# the basic sensors keeps the 'last' value and is better for graphs.
if (
equipment_flags & EQUIPMENT.FLAG_INTELLICHEM
and sensor_name in SUPPORTED_BASIC_CHEM_SENSORS
if not (DEVICE_INCLUSION_RULES.get(device) or value_params.included).test(
gateway, data_path
):
entities.append(
ScreenLogicStatusSensor(coordinator, sensor_name, CODE.STATUS_CHANGED)
cleanup_excluded_entity(coordinator, DOMAIN, entity_key)
continue
try:
value_data = gateway.get_data(*data_path, strict=True)
except KeyError:
_LOGGER.debug("Failed to find %s", data_path)
continue
entity_description_kwargs = {
**build_base_entity_description(
gateway, entity_key, data_path, value_data, value_params
),
"device_class": SL_DEVICE_TYPE_TO_HA_DEVICE_CLASS.get(
value_data.get(ATTR.DEVICE_TYPE)
),
"native_unit_of_measurement": get_ha_unit(value_data),
"options": value_data.get(ATTR.ENUM_OPTIONS),
"state_class": SL_STATE_TYPE_TO_HA_STATE_CLASS.get(
value_data.get(ATTR.STATE_TYPE)
),
"value_mod": value_params.value_modification,
}
if (
sub_code := (
value_params.subscription_code or DEVICE_SUBSCRIPTION.get(device)
)
# Pump sensors
for pump_num, pump_data in coordinator.gateway_data[SL_DATA.KEY_PUMPS].items():
if pump_data["data"] != 0 and "currentWatts" in pump_data:
for pump_key in pump_data:
enabled = True
# Assumptions for Intelliflow VF
if pump_data["pumpType"] == 1 and pump_key == "currentRPM":
enabled = False
# Assumptions for Intelliflow VS
if pump_data["pumpType"] == 2 and pump_key == "currentGPM":
enabled = False
if pump_key in SUPPORTED_PUMP_SENSORS:
entities.append(
ScreenLogicPumpSensor(coordinator, pump_num, pump_key, enabled)
)
# IntelliChem sensors
if equipment_flags & EQUIPMENT.FLAG_INTELLICHEM:
for chem_sensor_name in coordinator.gateway_data[SL_DATA.KEY_CHEMISTRY]:
enabled = True
if equipment_flags & EQUIPMENT.FLAG_CHLORINATOR:
if chem_sensor_name in ("salt_tds_ppm",):
enabled = False
if chem_sensor_name in SUPPORTED_CHEM_SENSORS:
entities.append(
ScreenLogicChemistrySensor(
coordinator, chem_sensor_name, CODE.CHEMISTRY_CHANGED, enabled
)
) is not None:
entities.append(
ScreenLogicPushSensor(
coordinator,
ScreenLogicPushSensorDescription(
subscription_code=sub_code,
**entity_description_kwargs,
),
)
# SCG sensors
if equipment_flags & EQUIPMENT.FLAG_CHLORINATOR:
entities.extend(
[
ScreenLogicSCGSensor(coordinator, scg_sensor)
for scg_sensor in coordinator.gateway_data[SL_DATA.KEY_SCG]
if scg_sensor in SUPPORTED_SCG_SENSORS
]
)
)
else:
entities.append(
ScreenLogicSensor(
coordinator,
ScreenLogicSensorDescription(
**entity_description_kwargs,
),
)
)
async_add_entities(entities)
class ScreenLogicSensorEntity(ScreenlogicEntity, SensorEntity):
"""Base class for all ScreenLogic sensor entities."""
@dataclass
class ScreenLogicSensorMixin:
"""Mixin for SecreenLogic sensor entity."""
value_mod: Callable[[int | str], int | str] | None = None
@dataclass
class ScreenLogicSensorDescription(
ScreenLogicSensorMixin, SensorEntityDescription, ScreenLogicEntityDescription
):
"""Describes a ScreenLogic sensor."""
class ScreenLogicSensor(ScreenlogicEntity, SensorEntity):
"""Representation of a ScreenLogic sensor entity."""
entity_description: ScreenLogicSensorDescription
_attr_has_entity_name = True
@property
def name(self) -> str | None:
"""Name of the sensor."""
return self.sensor["name"]
@property
def native_unit_of_measurement(self) -> str | None:
"""Return the unit of measurement."""
sl_unit = self.sensor.get("unit")
return SL_UNIT_TO_HA_UNIT.get(sl_unit, sl_unit)
@property
def device_class(self) -> SensorDeviceClass | None:
"""Device class of the sensor."""
device_type = self.sensor.get("device_type")
return SL_DEVICE_TYPE_TO_HA_DEVICE_CLASS.get(device_type)
@property
def entity_category(self) -> EntityCategory | None:
"""Entity Category of the sensor."""
return (
None if self._data_key == "air_temperature" else EntityCategory.DIAGNOSTIC
)
@property
def state_class(self) -> SensorStateClass | None:
"""Return the state class of the sensor."""
state_type = self.sensor.get("state_type")
if self._data_key == "scg_super_chlor_timer":
return None
return SL_STATE_TYPE_TO_HA_STATE_CLASS.get(state_type)
@property
def options(self) -> list[str] | None:
"""Return a set of possible options."""
return self.sensor.get("enum_options")
@property
def native_value(self) -> str | int | float:
"""State of the sensor."""
return self.sensor["value"]
@property
def sensor(self) -> dict[str | int, Any]:
"""Shortcut to access the sensor data."""
return self.gateway_data[SL_DATA.KEY_SENSORS][self._data_key]
val = self.entity_data[ATTR.VALUE]
value_mod = self.entity_description.value_mod
return value_mod(val) if value_mod else val
class ScreenLogicStatusSensor(ScreenLogicSensorEntity, ScreenLogicPushEntity):
"""Representation of a basic ScreenLogic sensor entity."""
@dataclass
class ScreenLogicPushSensorDescription(
ScreenLogicSensorDescription, ScreenLogicPushEntityDescription
):
"""Describes a ScreenLogic push sensor."""
class ScreenLogicPumpSensor(ScreenLogicSensorEntity):
"""Representation of a ScreenLogic pump sensor entity."""
class ScreenLogicPushSensor(ScreenLogicSensor, ScreenLogicPushEntity):
"""Representation of a ScreenLogic push sensor entity."""
def __init__(self, coordinator, pump, key, enabled=True):
"""Initialize of the pump sensor."""
super().__init__(coordinator, f"{key}_{pump}", enabled)
self._pump_id = pump
self._key = key
@property
def sensor(self) -> dict[str | int, Any]:
"""Shortcut to access the pump sensor data."""
return self.gateway_data[SL_DATA.KEY_PUMPS][self._pump_id][self._key]
class ScreenLogicChemistrySensor(ScreenLogicSensorEntity, ScreenLogicPushEntity):
"""Representation of a ScreenLogic IntelliChem sensor entity."""
def __init__(self, coordinator, key, message_code, enabled=True):
"""Initialize of the pump sensor."""
super().__init__(coordinator, f"chem_{key}", message_code, enabled)
self._key = key
@property
def native_value(self) -> str | int | float:
"""State of the sensor."""
value = self.sensor["value"]
if "dosing_state" in self._key:
return CHEM_DOSING_STATE.NAME_FOR_NUM[value]
return (value - 1) if "supply" in self._data_key else value
@property
def sensor(self) -> dict[str | int, Any]:
"""Shortcut to access the pump sensor data."""
return self.gateway_data[SL_DATA.KEY_CHEMISTRY][self._key]
class ScreenLogicSCGSensor(ScreenLogicSensorEntity):
"""Representation of ScreenLogic SCG sensor entity."""
@property
def sensor(self) -> dict[str | int, Any]:
"""Shortcut to access the pump sensor data."""
return self.gateway_data[SL_DATA.KEY_SCG][self._data_key]
entity_description: ScreenLogicPushSensorDescription