mirror of
https://github.com/home-assistant/core.git
synced 2025-07-12 07:47:08 +00:00
Use EntityDescription - ondilo_ico (#53579)
This commit is contained in:
parent
9806bda272
commit
ac837cd76e
@ -3,11 +3,10 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
from typing import NamedTuple
|
|
||||||
|
|
||||||
from ondilo import OndiloError
|
from ondilo import OndiloError
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorEntity
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONCENTRATION_PARTS_PER_MILLION,
|
CONCENTRATION_PARTS_PER_MILLION,
|
||||||
DEVICE_CLASS_BATTERY,
|
DEVICE_CLASS_BATTERY,
|
||||||
@ -25,60 +24,58 @@ from homeassistant.helpers.update_coordinator import (
|
|||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
|
||||||
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||||
class OndiloIOCSensorMetadata(NamedTuple):
|
SensorEntityDescription(
|
||||||
"""Sensor metadata for an individual Ondilo IOC sensor."""
|
key="temperature",
|
||||||
|
name="Temperature",
|
||||||
name: str
|
|
||||||
unit_of_measurement: str | None
|
|
||||||
icon: str | None
|
|
||||||
device_class: str | None
|
|
||||||
|
|
||||||
|
|
||||||
SENSOR_TYPES: dict[str, OndiloIOCSensorMetadata] = {
|
|
||||||
"temperature": OndiloIOCSensorMetadata(
|
|
||||||
"Temperature",
|
|
||||||
unit_of_measurement=TEMP_CELSIUS,
|
unit_of_measurement=TEMP_CELSIUS,
|
||||||
icon=None,
|
icon=None,
|
||||||
device_class=DEVICE_CLASS_TEMPERATURE,
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
),
|
),
|
||||||
"orp": OndiloIOCSensorMetadata(
|
SensorEntityDescription(
|
||||||
"Oxydo Reduction Potential",
|
key="orp",
|
||||||
|
name="Oxydo Reduction Potential",
|
||||||
unit_of_measurement=ELECTRIC_POTENTIAL_MILLIVOLT,
|
unit_of_measurement=ELECTRIC_POTENTIAL_MILLIVOLT,
|
||||||
icon="mdi:pool",
|
icon="mdi:pool",
|
||||||
device_class=None,
|
device_class=None,
|
||||||
),
|
),
|
||||||
"ph": OndiloIOCSensorMetadata(
|
SensorEntityDescription(
|
||||||
"pH",
|
key="ph",
|
||||||
|
name="pH",
|
||||||
unit_of_measurement=None,
|
unit_of_measurement=None,
|
||||||
icon="mdi:pool",
|
icon="mdi:pool",
|
||||||
device_class=None,
|
device_class=None,
|
||||||
),
|
),
|
||||||
"tds": OndiloIOCSensorMetadata(
|
SensorEntityDescription(
|
||||||
"TDS",
|
key="tds",
|
||||||
|
name="TDS",
|
||||||
unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
|
unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
|
||||||
icon="mdi:pool",
|
icon="mdi:pool",
|
||||||
device_class=None,
|
device_class=None,
|
||||||
),
|
),
|
||||||
"battery": OndiloIOCSensorMetadata(
|
SensorEntityDescription(
|
||||||
"Battery",
|
key="battery",
|
||||||
|
name="Battery",
|
||||||
unit_of_measurement=PERCENTAGE,
|
unit_of_measurement=PERCENTAGE,
|
||||||
icon=None,
|
icon=None,
|
||||||
device_class=DEVICE_CLASS_BATTERY,
|
device_class=DEVICE_CLASS_BATTERY,
|
||||||
),
|
),
|
||||||
"rssi": OndiloIOCSensorMetadata(
|
SensorEntityDescription(
|
||||||
"RSSI",
|
key="rssi",
|
||||||
|
name="RSSI",
|
||||||
unit_of_measurement=PERCENTAGE,
|
unit_of_measurement=PERCENTAGE,
|
||||||
icon=None,
|
icon=None,
|
||||||
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
|
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
|
||||||
),
|
),
|
||||||
"salt": OndiloIOCSensorMetadata(
|
SensorEntityDescription(
|
||||||
"Salt",
|
key="salt",
|
||||||
|
name="Salt",
|
||||||
unit_of_measurement="mg/L",
|
unit_of_measurement="mg/L",
|
||||||
icon="mdi:pool",
|
icon="mdi:pool",
|
||||||
device_class=None,
|
device_class=None,
|
||||||
),
|
),
|
||||||
}
|
)
|
||||||
|
|
||||||
|
|
||||||
SCAN_INTERVAL = timedelta(hours=1)
|
SCAN_INTERVAL = timedelta(hours=1)
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -116,9 +113,14 @@ async def async_setup_entry(hass, entry, async_add_entities):
|
|||||||
|
|
||||||
entities = []
|
entities = []
|
||||||
for poolidx, pool in enumerate(coordinator.data):
|
for poolidx, pool in enumerate(coordinator.data):
|
||||||
for sensor_idx, sensor in enumerate(pool["sensors"]):
|
entities.extend(
|
||||||
if sensor["data_type"] in SENSOR_TYPES:
|
[
|
||||||
entities.append(OndiloICO(coordinator, poolidx, sensor_idx))
|
OndiloICO(coordinator, poolidx, description)
|
||||||
|
for sensor in pool["sensors"]
|
||||||
|
for description in SENSOR_TYPES
|
||||||
|
if description.key == sensor["data_type"]
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
@ -127,22 +129,21 @@ class OndiloICO(CoordinatorEntity, SensorEntity):
|
|||||||
"""Representation of a Sensor."""
|
"""Representation of a Sensor."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, coordinator: DataUpdateCoordinator, poolidx: int, sensor_idx: int
|
self,
|
||||||
|
coordinator: DataUpdateCoordinator,
|
||||||
|
poolidx: int,
|
||||||
|
description: SensorEntityDescription,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize sensor entity with data from coordinator."""
|
"""Initialize sensor entity with data from coordinator."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
|
self.entity_description = description
|
||||||
|
|
||||||
self._poolid = self.coordinator.data[poolidx]["id"]
|
self._poolid = self.coordinator.data[poolidx]["id"]
|
||||||
|
|
||||||
pooldata = self._pooldata()
|
pooldata = self._pooldata()
|
||||||
self._data_type = pooldata["sensors"][sensor_idx]["data_type"]
|
self._unique_id = f"{pooldata['ICO']['serial_number']}-{description.key}"
|
||||||
self._unique_id = f"{pooldata['ICO']['serial_number']}-{self._data_type}"
|
|
||||||
self._device_name = pooldata["name"]
|
self._device_name = pooldata["name"]
|
||||||
metadata = SENSOR_TYPES[self._data_type]
|
self._name = f"{self._device_name} {description.name}"
|
||||||
self._name = f"{self._device_name} {metadata.name}"
|
|
||||||
self._attr_device_class = metadata.device_class
|
|
||||||
self._attr_icon = metadata.icon
|
|
||||||
self._attr_unit_of_measurement = metadata.unit_of_measurement
|
|
||||||
|
|
||||||
def _pooldata(self):
|
def _pooldata(self):
|
||||||
"""Get pool data dict."""
|
"""Get pool data dict."""
|
||||||
@ -157,7 +158,7 @@ class OndiloICO(CoordinatorEntity, SensorEntity):
|
|||||||
(
|
(
|
||||||
data_type
|
data_type
|
||||||
for data_type in self._pooldata()["sensors"]
|
for data_type in self._pooldata()["sensors"]
|
||||||
if data_type["data_type"] == self._data_type
|
if data_type["data_type"] == self.entity_description.key
|
||||||
),
|
),
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user