Use EntityDescription - flume (#56433)

This commit is contained in:
Marc Mueller 2021-09-20 18:13:09 +02:00 committed by GitHub
parent 6f36419c6f
commit 4c4bd740f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 43 deletions

View File

@ -1,4 +1,8 @@
"""The Flume component.""" """The Flume component."""
from __future__ import annotations
from homeassistant.components.sensor import SensorEntityDescription
DOMAIN = "flume" DOMAIN = "flume"
PLATFORMS = ["sensor"] PLATFORMS = ["sensor"]
@ -6,15 +10,43 @@ PLATFORMS = ["sensor"]
DEFAULT_NAME = "Flume Sensor" DEFAULT_NAME = "Flume Sensor"
FLUME_TYPE_SENSOR = 2 FLUME_TYPE_SENSOR = 2
FLUME_QUERIES_SENSOR = { FLUME_QUERIES_SENSOR: tuple[SensorEntityDescription, ...] = (
"current_interval": {"friendly_name": "Current", "unit_of_measurement": "gal/m"}, SensorEntityDescription(
"month_to_date": {"friendly_name": "Current Month", "unit_of_measurement": "gal"}, key="current_interval",
"week_to_date": {"friendly_name": "Current Week", "unit_of_measurement": "gal"}, name="Current",
"today": {"friendly_name": "Current Day", "unit_of_measurement": "gal"}, native_unit_of_measurement="gal/m",
"last_60_min": {"friendly_name": "60 Minutes", "unit_of_measurement": "gal/h"}, ),
"last_24_hrs": {"friendly_name": "24 Hours", "unit_of_measurement": "gal/d"}, SensorEntityDescription(
"last_30_days": {"friendly_name": "30 Days", "unit_of_measurement": "gal/mo"}, key="month_to_date",
} name="Current Month",
native_unit_of_measurement="gal",
),
SensorEntityDescription(
key="week_to_date",
name="Current Week",
native_unit_of_measurement="gal",
),
SensorEntityDescription(
key="today",
name="Current Day",
native_unit_of_measurement="gal",
),
SensorEntityDescription(
key="last_60_min",
name="60 Minutes",
native_unit_of_measurement="gal/h",
),
SensorEntityDescription(
key="last_24_hrs",
name="24 Hours",
native_unit_of_measurement="gal/d",
),
SensorEntityDescription(
key="last_30_days",
name="30 Days",
native_unit_of_measurement="gal/mo",
),
)
FLUME_AUTH = "flume_auth" FLUME_AUTH = "flume_auth"
FLUME_HTTP_SESSION = "http_session" FLUME_HTTP_SESSION = "http_session"

View File

@ -6,7 +6,11 @@ from numbers import Number
from pyflume import FlumeData from pyflume import FlumeData
import voluptuous as vol import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity from homeassistant.components.sensor import (
PLATFORM_SCHEMA,
SensorEntity,
SensorEntityDescription,
)
from homeassistant.config_entries import SOURCE_IMPORT from homeassistant.config_entries import SOURCE_IMPORT
from homeassistant.const import ( from homeassistant.const import (
CONF_CLIENT_ID, CONF_CLIENT_ID,
@ -93,16 +97,18 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
coordinator = _create_flume_device_coordinator(hass, flume_device) coordinator = _create_flume_device_coordinator(hass, flume_device)
for flume_query_sensor in FLUME_QUERIES_SENSOR.items(): flume_entity_list.extend(
flume_entity_list.append( [
FlumeSensor( FlumeSensor(
coordinator, coordinator,
flume_device, flume_device,
flume_query_sensor, device_friendly_name,
f"{device_friendly_name} {flume_query_sensor[1]['friendly_name']}",
device_id, device_id,
description,
) )
) for description in FLUME_QUERIES_SENSOR
]
)
if flume_entity_list: if flume_entity_list:
async_add_entities(flume_entity_list) async_add_entities(flume_entity_list)
@ -111,50 +117,37 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class FlumeSensor(CoordinatorEntity, SensorEntity): class FlumeSensor(CoordinatorEntity, SensorEntity):
"""Representation of the Flume sensor.""" """Representation of the Flume sensor."""
def __init__(self, coordinator, flume_device, flume_query_sensor, name, device_id): def __init__(
self,
coordinator,
flume_device,
name,
device_id,
description: SensorEntityDescription,
):
"""Initialize the Flume sensor.""" """Initialize the Flume sensor."""
super().__init__(coordinator) super().__init__(coordinator)
self.entity_description = description
self._flume_device = flume_device self._flume_device = flume_device
self._flume_query_sensor = flume_query_sensor
self._name = name
self._device_id = device_id
self._state = None
@property self._attr_name = f"{name} {description.name}"
def device_info(self): self._attr_unique_id = f"{description.key}_{device_id}"
"""Device info for the flume sensor.""" self._attr_device_info = {
return { "name": self.name,
"name": self._name, "identifiers": {(DOMAIN, device_id)},
"identifiers": {(DOMAIN, self._device_id)},
"manufacturer": "Flume, Inc.", "manufacturer": "Flume, Inc.",
"model": "Flume Smart Water Monitor", "model": "Flume Smart Water Monitor",
} }
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property @property
def native_value(self): def native_value(self):
"""Return the state of the sensor.""" """Return the state of the sensor."""
sensor_key = self._flume_query_sensor[0] sensor_key = self.entity_description.key
if sensor_key not in self._flume_device.values: if sensor_key not in self._flume_device.values:
return None return None
return _format_state_value(self._flume_device.values[sensor_key]) return _format_state_value(self._flume_device.values[sensor_key])
@property
def native_unit_of_measurement(self):
"""Return the unit the value is expressed in."""
# This is in gallons per SCAN_INTERVAL
return self._flume_query_sensor[1]["unit_of_measurement"]
@property
def unique_id(self):
"""Flume query and Device unique ID."""
return f"{self._flume_query_sensor[0]}_{self._device_id}"
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Request an update when added.""" """Request an update when added."""
await super().async_added_to_hass() await super().async_added_to_hass()