2021-01-04 16:09:01 +00:00
|
|
|
"""Platform for sensor integration."""
|
2024-03-08 14:04:07 +00:00
|
|
|
|
2021-07-22 05:27:31 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-10-24 09:52:25 +00:00
|
|
|
from homeassistant.components.sensor import (
|
2021-12-16 16:04:47 +00:00
|
|
|
SensorDeviceClass,
|
2021-10-24 09:52:25 +00:00
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
2021-12-16 16:04:47 +00:00
|
|
|
SensorStateClass,
|
2021-10-24 09:52:25 +00:00
|
|
|
)
|
2022-01-03 18:10:57 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-01-04 16:09:01 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONCENTRATION_PARTS_PER_MILLION,
|
|
|
|
PERCENTAGE,
|
2022-12-17 09:06:39 +00:00
|
|
|
UnitOfElectricPotential,
|
|
|
|
UnitOfTemperature,
|
2021-01-04 16:09:01 +00:00
|
|
|
)
|
2022-01-03 18:10:57 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2023-08-11 02:04:26 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceInfo
|
2022-01-03 18:10:57 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2024-05-02 13:57:47 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2021-01-04 16:09:01 +00:00
|
|
|
|
|
|
|
from .const import DOMAIN
|
2024-05-02 13:57:47 +00:00
|
|
|
from .coordinator import OndiloIcoCoordinator
|
2021-01-04 16:09:01 +00:00
|
|
|
|
2021-07-27 20:51:32 +00:00
|
|
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="temperature",
|
2022-12-17 09:06:39 +00:00
|
|
|
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
2021-12-16 16:04:47 +00:00
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-07-22 05:27:31 +00:00
|
|
|
),
|
2021-07-27 20:51:32 +00:00
|
|
|
SensorEntityDescription(
|
|
|
|
key="orp",
|
2023-07-22 11:16:07 +00:00
|
|
|
translation_key="oxydo_reduction_potential",
|
2022-12-17 09:06:39 +00:00
|
|
|
native_unit_of_measurement=UnitOfElectricPotential.MILLIVOLT,
|
2021-12-16 16:04:47 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-07-22 05:27:31 +00:00
|
|
|
),
|
2021-07-27 20:51:32 +00:00
|
|
|
SensorEntityDescription(
|
|
|
|
key="ph",
|
2023-07-22 11:16:07 +00:00
|
|
|
translation_key="ph",
|
2021-12-16 16:04:47 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-07-22 05:27:31 +00:00
|
|
|
),
|
2021-07-27 20:51:32 +00:00
|
|
|
SensorEntityDescription(
|
|
|
|
key="tds",
|
2023-07-22 11:16:07 +00:00
|
|
|
translation_key="tds",
|
2021-08-12 11:26:17 +00:00
|
|
|
native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
|
2021-12-16 16:04:47 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-07-22 05:27:31 +00:00
|
|
|
),
|
2021-07-27 20:51:32 +00:00
|
|
|
SensorEntityDescription(
|
|
|
|
key="battery",
|
2021-08-12 11:26:17 +00:00
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
2021-12-16 16:04:47 +00:00
|
|
|
device_class=SensorDeviceClass.BATTERY,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-07-22 05:27:31 +00:00
|
|
|
),
|
2021-07-27 20:51:32 +00:00
|
|
|
SensorEntityDescription(
|
|
|
|
key="rssi",
|
2023-07-22 11:16:07 +00:00
|
|
|
translation_key="rssi",
|
2021-08-12 11:26:17 +00:00
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
2021-12-16 16:04:47 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-07-22 05:27:31 +00:00
|
|
|
),
|
2021-07-27 20:51:32 +00:00
|
|
|
SensorEntityDescription(
|
|
|
|
key="salt",
|
2023-07-22 11:16:07 +00:00
|
|
|
translation_key="salt",
|
2021-08-12 11:26:17 +00:00
|
|
|
native_unit_of_measurement="mg/L",
|
2021-12-16 16:04:47 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-07-22 05:27:31 +00:00
|
|
|
),
|
2021-07-27 20:51:32 +00:00
|
|
|
)
|
|
|
|
|
2021-01-04 16:09:01 +00:00
|
|
|
|
2022-01-03 18:10:57 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2021-01-04 16:09:01 +00:00
|
|
|
"""Set up the Ondilo ICO sensors."""
|
|
|
|
|
2024-05-02 13:57:47 +00:00
|
|
|
coordinator: OndiloIcoCoordinator = hass.data[DOMAIN][entry.entry_id]
|
2021-01-04 16:09:01 +00:00
|
|
|
|
2024-05-02 13:57:47 +00:00
|
|
|
async_add_entities(
|
|
|
|
OndiloICO(coordinator, poolidx, description)
|
|
|
|
for poolidx, pool in enumerate(coordinator.data)
|
|
|
|
for sensor in pool["sensors"]
|
|
|
|
for description in SENSOR_TYPES
|
|
|
|
if description.key == sensor["data_type"]
|
2021-01-04 16:09:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-05-02 13:57:47 +00:00
|
|
|
class OndiloICO(CoordinatorEntity[OndiloIcoCoordinator], SensorEntity):
|
2021-01-04 16:09:01 +00:00
|
|
|
"""Representation of a Sensor."""
|
|
|
|
|
2023-07-22 11:16:07 +00:00
|
|
|
_attr_has_entity_name = True
|
|
|
|
|
2021-01-04 16:09:01 +00:00
|
|
|
def __init__(
|
2021-07-27 20:51:32 +00:00
|
|
|
self,
|
2024-05-02 13:57:47 +00:00
|
|
|
coordinator: OndiloIcoCoordinator,
|
2021-07-27 20:51:32 +00:00
|
|
|
poolidx: int,
|
|
|
|
description: SensorEntityDescription,
|
2021-05-20 13:58:17 +00:00
|
|
|
) -> None:
|
2021-01-04 16:09:01 +00:00
|
|
|
"""Initialize sensor entity with data from coordinator."""
|
|
|
|
super().__init__(coordinator)
|
2021-07-27 20:51:32 +00:00
|
|
|
self.entity_description = description
|
2021-01-04 16:09:01 +00:00
|
|
|
|
|
|
|
self._poolid = self.coordinator.data[poolidx]["id"]
|
|
|
|
|
|
|
|
pooldata = self._pooldata()
|
2021-08-09 18:53:30 +00:00
|
|
|
self._attr_unique_id = f"{pooldata['ICO']['serial_number']}-{description.key}"
|
2023-09-05 13:40:43 +00:00
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, pooldata["ICO"]["serial_number"])},
|
|
|
|
manufacturer="Ondilo",
|
|
|
|
model="ICO",
|
|
|
|
name=pooldata["name"],
|
|
|
|
sw_version=pooldata["ICO"]["sw_version"],
|
|
|
|
)
|
2021-01-04 16:09:01 +00:00
|
|
|
|
|
|
|
def _pooldata(self):
|
|
|
|
"""Get pool data dict."""
|
|
|
|
return next(
|
|
|
|
(pool for pool in self.coordinator.data if pool["id"] == self._poolid),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
|
|
|
|
def _devdata(self):
|
|
|
|
"""Get device data dict."""
|
|
|
|
return next(
|
|
|
|
(
|
|
|
|
data_type
|
|
|
|
for data_type in self._pooldata()["sensors"]
|
2021-07-27 20:51:32 +00:00
|
|
|
if data_type["data_type"] == self.entity_description.key
|
2021-01-04 16:09:01 +00:00
|
|
|
),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
2021-08-12 11:26:17 +00:00
|
|
|
def native_value(self):
|
2021-01-04 16:09:01 +00:00
|
|
|
"""Last value of the sensor."""
|
|
|
|
return self._devdata()["value"]
|