Use EntityDescription - rainmachine (#55021)
parent
ee3e27c82a
commit
8522538d8f
|
@ -1,9 +1,12 @@
|
||||||
"""This platform provides support for sensor data from RainMachine."""
|
"""This platform provides support for sensor data from RainMachine."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
from regenmaschine.controller import Controller
|
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.config_entries import ConfigEntry
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
DEVICE_CLASS_TEMPERATURE,
|
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_FLOW_SENSOR_WATERING_CLICKS = "flow_sensor_watering_clicks"
|
||||||
TYPE_FREEZE_TEMP = "freeze_protect_temp"
|
TYPE_FREEZE_TEMP = "freeze_protect_temp"
|
||||||
|
|
||||||
SENSORS = {
|
|
||||||
TYPE_FLOW_SENSOR_CLICK_M3: (
|
@dataclass
|
||||||
"Flow Sensor Clicks",
|
class RainmachineRequiredKeysMixin:
|
||||||
"mdi:water-pump",
|
"""Mixin for required keys."""
|
||||||
f"clicks/{VOLUME_CUBIC_METERS}",
|
|
||||||
None,
|
api_category: str
|
||||||
False,
|
|
||||||
DATA_PROVISION_SETTINGS,
|
|
||||||
|
@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: (
|
RainmachineSensorEntityDescription(
|
||||||
"Flow Sensor Consumed Liters",
|
key=TYPE_FLOW_SENSOR_CONSUMED_LITERS,
|
||||||
"mdi:water-pump",
|
name="Flow Sensor Consumed Liters",
|
||||||
"liter",
|
icon="mdi:water-pump",
|
||||||
None,
|
native_unit_of_measurement="liter",
|
||||||
False,
|
entity_registry_enabled_default=False,
|
||||||
DATA_PROVISION_SETTINGS,
|
api_category=DATA_PROVISION_SETTINGS,
|
||||||
),
|
),
|
||||||
TYPE_FLOW_SENSOR_START_INDEX: (
|
RainmachineSensorEntityDescription(
|
||||||
"Flow Sensor Start Index",
|
key=TYPE_FLOW_SENSOR_START_INDEX,
|
||||||
"mdi:water-pump",
|
name="Flow Sensor Start Index",
|
||||||
"index",
|
icon="mdi:water-pump",
|
||||||
None,
|
native_unit_of_measurement="index",
|
||||||
False,
|
entity_registry_enabled_default=False,
|
||||||
DATA_PROVISION_SETTINGS,
|
api_category=DATA_PROVISION_SETTINGS,
|
||||||
),
|
),
|
||||||
TYPE_FLOW_SENSOR_WATERING_CLICKS: (
|
RainmachineSensorEntityDescription(
|
||||||
"Flow Sensor Clicks",
|
key=TYPE_FLOW_SENSOR_WATERING_CLICKS,
|
||||||
"mdi:water-pump",
|
name="Flow Sensor Clicks",
|
||||||
"clicks",
|
icon="mdi:water-pump",
|
||||||
None,
|
native_unit_of_measurement="clicks",
|
||||||
False,
|
entity_registry_enabled_default=False,
|
||||||
DATA_PROVISION_SETTINGS,
|
api_category=DATA_PROVISION_SETTINGS,
|
||||||
),
|
),
|
||||||
TYPE_FREEZE_TEMP: (
|
RainmachineSensorEntityDescription(
|
||||||
"Freeze Protect Temperature",
|
key=TYPE_FREEZE_TEMP,
|
||||||
"mdi:thermometer",
|
name="Freeze Protect Temperature",
|
||||||
TEMP_CELSIUS,
|
icon="mdi:thermometer",
|
||||||
DEVICE_CLASS_TEMPERATURE,
|
native_unit_of_measurement=TEMP_CELSIUS,
|
||||||
True,
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
DATA_RESTRICTIONS_UNIVERSAL,
|
entity_registry_enabled_default=True,
|
||||||
|
api_category=DATA_RESTRICTIONS_UNIVERSAL,
|
||||||
),
|
),
|
||||||
}
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
|
@ -96,19 +115,8 @@ async def async_setup_entry(
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
[
|
[
|
||||||
async_get_sensor(api_category)(
|
async_get_sensor(description.api_category)(controller, description)
|
||||||
controller,
|
for description in SENSOR_TYPES
|
||||||
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()
|
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -116,25 +124,17 @@ async def async_setup_entry(
|
||||||
class RainMachineSensor(RainMachineEntity, SensorEntity):
|
class RainMachineSensor(RainMachineEntity, SensorEntity):
|
||||||
"""Define a general RainMachine sensor."""
|
"""Define a general RainMachine sensor."""
|
||||||
|
|
||||||
|
entity_description: RainmachineSensorEntityDescription
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator: DataUpdateCoordinator,
|
coordinator: DataUpdateCoordinator,
|
||||||
controller: Controller,
|
controller: Controller,
|
||||||
sensor_type: str,
|
description: RainmachineSensorEntityDescription,
|
||||||
name: str,
|
|
||||||
icon: str,
|
|
||||||
unit: str,
|
|
||||||
device_class: str,
|
|
||||||
enabled_by_default: bool,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize."""
|
"""Initialize."""
|
||||||
super().__init__(coordinator, controller, sensor_type)
|
super().__init__(coordinator, controller, description.key)
|
||||||
|
self.entity_description = description
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
class ProvisionSettingsSensor(RainMachineSensor):
|
class ProvisionSettingsSensor(RainMachineSensor):
|
||||||
|
|
Loading…
Reference in New Issue