From 551c1177172850a9fb6afbed3bf99c1740fb470b Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Thu, 22 Jul 2021 07:27:31 +0200 Subject: [PATCH] Use NamedTuple - ondilo_ico (#53296) --- homeassistant/components/ondilo_ico/sensor.py | 102 ++++++++++-------- 1 file changed, 58 insertions(+), 44 deletions(-) diff --git a/homeassistant/components/ondilo_ico/sensor.py b/homeassistant/components/ondilo_ico/sensor.py index 633e03157e4..122b4154892 100644 --- a/homeassistant/components/ondilo_ico/sensor.py +++ b/homeassistant/components/ondilo_ico/sensor.py @@ -1,6 +1,9 @@ """Platform for sensor integration.""" +from __future__ import annotations + from datetime import timedelta import logging +from typing import NamedTuple from ondilo import OndiloError @@ -22,29 +25,59 @@ from homeassistant.helpers.update_coordinator import ( 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", - TEMP_CELSIUS, - None, - DEVICE_CLASS_TEMPERATURE, - ], - "orp": [ + unit_of_measurement=TEMP_CELSIUS, + icon=None, + device_class=DEVICE_CLASS_TEMPERATURE, + ), + "orp": OndiloIOCSensorMetadata( "Oxydo Reduction Potential", - ELECTRIC_POTENTIAL_MILLIVOLT, - "mdi:pool", - None, - ], - "ph": ["pH", "", "mdi:pool", None], - "tds": ["TDS", CONCENTRATION_PARTS_PER_MILLION, "mdi:pool", None], - "battery": ["Battery", PERCENTAGE, None, DEVICE_CLASS_BATTERY], - "rssi": [ + unit_of_measurement=ELECTRIC_POTENTIAL_MILLIVOLT, + icon="mdi:pool", + device_class=None, + ), + "ph": OndiloIOCSensorMetadata( + "pH", + unit_of_measurement=None, + 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", - PERCENTAGE, - None, - DEVICE_CLASS_SIGNAL_STRENGTH, - ], - "salt": ["Salt", "mg/L", "mdi:pool", None], + unit_of_measurement=PERCENTAGE, + icon=None, + device_class=DEVICE_CLASS_SIGNAL_STRENGTH, + ), + "salt": OndiloIOCSensorMetadata( + "Salt", + unit_of_measurement="mg/L", + icon="mdi:pool", + device_class=None, + ), } SCAN_INTERVAL = timedelta(hours=1) @@ -105,10 +138,11 @@ class OndiloICO(CoordinatorEntity, SensorEntity): self._data_type = pooldata["sensors"][sensor_idx]["data_type"] self._unique_id = f"{pooldata['ICO']['serial_number']}-{self._data_type}" self._device_name = pooldata["name"] - self._name = f"{self._device_name} {SENSOR_TYPES[self._data_type][0]}" - self._device_class = SENSOR_TYPES[self._data_type][3] - self._icon = SENSOR_TYPES[self._data_type][2] - self._unit = SENSOR_TYPES[self._data_type][1] + metadata = SENSOR_TYPES[self._data_type] + 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): """Get pool data dict.""" @@ -128,31 +162,11 @@ class OndiloICO(CoordinatorEntity, SensorEntity): None, ) - @property - def name(self): - """Name of the sensor.""" - return self._name - @property def state(self): """Last value of the sensor.""" 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 def unique_id(self): """Return the unique ID of this entity."""