Use NamedTuple - ondilo_ico (#53296)

pull/53304/head
Marc Mueller 2021-07-22 07:27:31 +02:00 committed by GitHub
parent 5c3fb77660
commit 551c117717
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 58 additions and 44 deletions

View File

@ -1,6 +1,9 @@
"""Platform for sensor integration.""" """Platform for sensor integration."""
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
@ -22,29 +25,59 @@ from homeassistant.helpers.update_coordinator import (
from .const import DOMAIN from .const import DOMAIN
SENSOR_TYPES = {
"temperature": [ class OndiloIOCSensorMetadata(NamedTuple):
"""Sensor metadata for an individual Ondilo IOC sensor."""
name: str
unit_of_measurement: str | None
icon: str | None
device_class: str | None
SENSOR_TYPES: dict[str, OndiloIOCSensorMetadata] = {
"temperature": OndiloIOCSensorMetadata(
"Temperature", "Temperature",
TEMP_CELSIUS, unit_of_measurement=TEMP_CELSIUS,
None, icon=None,
DEVICE_CLASS_TEMPERATURE, device_class=DEVICE_CLASS_TEMPERATURE,
], ),
"orp": [ "orp": OndiloIOCSensorMetadata(
"Oxydo Reduction Potential", "Oxydo Reduction Potential",
ELECTRIC_POTENTIAL_MILLIVOLT, unit_of_measurement=ELECTRIC_POTENTIAL_MILLIVOLT,
"mdi:pool", icon="mdi:pool",
None, device_class=None,
], ),
"ph": ["pH", "", "mdi:pool", None], "ph": OndiloIOCSensorMetadata(
"tds": ["TDS", CONCENTRATION_PARTS_PER_MILLION, "mdi:pool", None], "pH",
"battery": ["Battery", PERCENTAGE, None, DEVICE_CLASS_BATTERY], unit_of_measurement=None,
"rssi": [ icon="mdi:pool",
device_class=None,
),
"tds": OndiloIOCSensorMetadata(
"TDS",
unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
icon="mdi:pool",
device_class=None,
),
"battery": OndiloIOCSensorMetadata(
"Battery",
unit_of_measurement=PERCENTAGE,
icon=None,
device_class=DEVICE_CLASS_BATTERY,
),
"rssi": OndiloIOCSensorMetadata(
"RSSI", "RSSI",
PERCENTAGE, unit_of_measurement=PERCENTAGE,
None, icon=None,
DEVICE_CLASS_SIGNAL_STRENGTH, device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
], ),
"salt": ["Salt", "mg/L", "mdi:pool", None], "salt": OndiloIOCSensorMetadata(
"Salt",
unit_of_measurement="mg/L",
icon="mdi:pool",
device_class=None,
),
} }
SCAN_INTERVAL = timedelta(hours=1) SCAN_INTERVAL = timedelta(hours=1)
@ -105,10 +138,11 @@ class OndiloICO(CoordinatorEntity, SensorEntity):
self._data_type = pooldata["sensors"][sensor_idx]["data_type"] self._data_type = pooldata["sensors"][sensor_idx]["data_type"]
self._unique_id = f"{pooldata['ICO']['serial_number']}-{self._data_type}" self._unique_id = f"{pooldata['ICO']['serial_number']}-{self._data_type}"
self._device_name = pooldata["name"] self._device_name = pooldata["name"]
self._name = f"{self._device_name} {SENSOR_TYPES[self._data_type][0]}" metadata = SENSOR_TYPES[self._data_type]
self._device_class = SENSOR_TYPES[self._data_type][3] self._name = f"{self._device_name} {metadata.name}"
self._icon = SENSOR_TYPES[self._data_type][2] self._attr_device_class = metadata.device_class
self._unit = SENSOR_TYPES[self._data_type][1] 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."""
@ -128,31 +162,11 @@ class OndiloICO(CoordinatorEntity, SensorEntity):
None, None,
) )
@property
def name(self):
"""Name of the sensor."""
return self._name
@property @property
def state(self): def state(self):
"""Last value of the sensor.""" """Last value of the sensor."""
return self._devdata()["value"] return self._devdata()["value"]
@property
def icon(self):
"""Icon to use in the frontend, if any."""
return self._icon
@property
def device_class(self):
"""Return the device class of the sensor."""
return self._device_class
@property
def unit_of_measurement(self):
"""Return the Unit of the sensor's measurement."""
return self._unit
@property @property
def unique_id(self): def unique_id(self):
"""Return the unique ID of this entity.""" """Return the unique ID of this entity."""