From 8522538d8f7d50d5dde4058c85a79c8bddc36286 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Mon, 23 Aug 2021 19:23:35 +0200 Subject: [PATCH] Use EntityDescription - rainmachine (#55021) --- .../components/rainmachine/sensor.py | 128 +++++++++--------- 1 file changed, 64 insertions(+), 64 deletions(-) diff --git a/homeassistant/components/rainmachine/sensor.py b/homeassistant/components/rainmachine/sensor.py index 2316b27acbf..269bd6bcd4b 100644 --- a/homeassistant/components/rainmachine/sensor.py +++ b/homeassistant/components/rainmachine/sensor.py @@ -1,9 +1,12 @@ """This platform provides support for sensor data from RainMachine.""" +from __future__ import annotations + +from dataclasses import dataclass from functools import partial from regenmaschine.controller import Controller -from homeassistant.components.sensor import SensorEntity +from homeassistant.components.sensor import SensorEntity, SensorEntityDescription from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( DEVICE_CLASS_TEMPERATURE, @@ -29,48 +32,64 @@ TYPE_FLOW_SENSOR_START_INDEX = "flow_sensor_start_index" TYPE_FLOW_SENSOR_WATERING_CLICKS = "flow_sensor_watering_clicks" TYPE_FREEZE_TEMP = "freeze_protect_temp" -SENSORS = { - TYPE_FLOW_SENSOR_CLICK_M3: ( - "Flow Sensor Clicks", - "mdi:water-pump", - f"clicks/{VOLUME_CUBIC_METERS}", - None, - False, - DATA_PROVISION_SETTINGS, + +@dataclass +class RainmachineRequiredKeysMixin: + """Mixin for required keys.""" + + api_category: str + + +@dataclass +class RainmachineSensorEntityDescription( + SensorEntityDescription, RainmachineRequiredKeysMixin +): + """Describes Rainmachine sensor entity.""" + + +SENSOR_TYPES: tuple[RainmachineSensorEntityDescription, ...] = ( + RainmachineSensorEntityDescription( + key=TYPE_FLOW_SENSOR_CLICK_M3, + name="Flow Sensor Clicks", + icon="mdi:water-pump", + native_unit_of_measurement=f"clicks/{VOLUME_CUBIC_METERS}", + entity_registry_enabled_default=False, + api_category=DATA_PROVISION_SETTINGS, ), - TYPE_FLOW_SENSOR_CONSUMED_LITERS: ( - "Flow Sensor Consumed Liters", - "mdi:water-pump", - "liter", - None, - False, - DATA_PROVISION_SETTINGS, + RainmachineSensorEntityDescription( + key=TYPE_FLOW_SENSOR_CONSUMED_LITERS, + name="Flow Sensor Consumed Liters", + icon="mdi:water-pump", + native_unit_of_measurement="liter", + entity_registry_enabled_default=False, + api_category=DATA_PROVISION_SETTINGS, ), - TYPE_FLOW_SENSOR_START_INDEX: ( - "Flow Sensor Start Index", - "mdi:water-pump", - "index", - None, - False, - DATA_PROVISION_SETTINGS, + RainmachineSensorEntityDescription( + key=TYPE_FLOW_SENSOR_START_INDEX, + name="Flow Sensor Start Index", + icon="mdi:water-pump", + native_unit_of_measurement="index", + entity_registry_enabled_default=False, + api_category=DATA_PROVISION_SETTINGS, ), - TYPE_FLOW_SENSOR_WATERING_CLICKS: ( - "Flow Sensor Clicks", - "mdi:water-pump", - "clicks", - None, - False, - DATA_PROVISION_SETTINGS, + RainmachineSensorEntityDescription( + key=TYPE_FLOW_SENSOR_WATERING_CLICKS, + name="Flow Sensor Clicks", + icon="mdi:water-pump", + native_unit_of_measurement="clicks", + entity_registry_enabled_default=False, + api_category=DATA_PROVISION_SETTINGS, ), - TYPE_FREEZE_TEMP: ( - "Freeze Protect Temperature", - "mdi:thermometer", - TEMP_CELSIUS, - DEVICE_CLASS_TEMPERATURE, - True, - DATA_RESTRICTIONS_UNIVERSAL, + RainmachineSensorEntityDescription( + key=TYPE_FREEZE_TEMP, + name="Freeze Protect Temperature", + icon="mdi:thermometer", + native_unit_of_measurement=TEMP_CELSIUS, + device_class=DEVICE_CLASS_TEMPERATURE, + entity_registry_enabled_default=True, + api_category=DATA_RESTRICTIONS_UNIVERSAL, ), -} +) async def async_setup_entry( @@ -96,19 +115,8 @@ async def async_setup_entry( async_add_entities( [ - async_get_sensor(api_category)( - controller, - sensor_type, - name, - icon, - unit, - device_class, - enabled_by_default, - ) - for ( - sensor_type, - (name, icon, unit, device_class, enabled_by_default, api_category), - ) in SENSORS.items() + async_get_sensor(description.api_category)(controller, description) + for description in SENSOR_TYPES ] ) @@ -116,25 +124,17 @@ async def async_setup_entry( class RainMachineSensor(RainMachineEntity, SensorEntity): """Define a general RainMachine sensor.""" + entity_description: RainmachineSensorEntityDescription + def __init__( self, coordinator: DataUpdateCoordinator, controller: Controller, - sensor_type: str, - name: str, - icon: str, - unit: str, - device_class: str, - enabled_by_default: bool, + description: RainmachineSensorEntityDescription, ) -> None: """Initialize.""" - super().__init__(coordinator, controller, sensor_type) - - self._attr_device_class = device_class - self._attr_entity_registry_enabled_default = enabled_by_default - self._attr_icon = icon - self._attr_name = name - self._attr_native_unit_of_measurement = unit + super().__init__(coordinator, controller, description.key) + self.entity_description = description class ProvisionSettingsSensor(RainMachineSensor):