Use EntityDescription - flume (#56433)

pull/56466/head
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."""
from __future__ import annotations
from homeassistant.components.sensor import SensorEntityDescription
DOMAIN = "flume"
PLATFORMS = ["sensor"]
@ -6,15 +10,43 @@ PLATFORMS = ["sensor"]
DEFAULT_NAME = "Flume Sensor"
FLUME_TYPE_SENSOR = 2
FLUME_QUERIES_SENSOR = {
"current_interval": {"friendly_name": "Current", "unit_of_measurement": "gal/m"},
"month_to_date": {"friendly_name": "Current Month", "unit_of_measurement": "gal"},
"week_to_date": {"friendly_name": "Current Week", "unit_of_measurement": "gal"},
"today": {"friendly_name": "Current Day", "unit_of_measurement": "gal"},
"last_60_min": {"friendly_name": "60 Minutes", "unit_of_measurement": "gal/h"},
"last_24_hrs": {"friendly_name": "24 Hours", "unit_of_measurement": "gal/d"},
"last_30_days": {"friendly_name": "30 Days", "unit_of_measurement": "gal/mo"},
}
FLUME_QUERIES_SENSOR: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription(
key="current_interval",
name="Current",
native_unit_of_measurement="gal/m",
),
SensorEntityDescription(
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_HTTP_SESSION = "http_session"

View File

@ -6,7 +6,11 @@ from numbers import Number
from pyflume import FlumeData
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.const import (
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)
for flume_query_sensor in FLUME_QUERIES_SENSOR.items():
flume_entity_list.append(
flume_entity_list.extend(
[
FlumeSensor(
coordinator,
flume_device,
flume_query_sensor,
f"{device_friendly_name} {flume_query_sensor[1]['friendly_name']}",
device_friendly_name,
device_id,
description,
)
)
for description in FLUME_QUERIES_SENSOR
]
)
if 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):
"""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."""
super().__init__(coordinator)
self.entity_description = description
self._flume_device = flume_device
self._flume_query_sensor = flume_query_sensor
self._name = name
self._device_id = device_id
self._state = None
@property
def device_info(self):
"""Device info for the flume sensor."""
return {
"name": self._name,
"identifiers": {(DOMAIN, self._device_id)},
self._attr_name = f"{name} {description.name}"
self._attr_unique_id = f"{description.key}_{device_id}"
self._attr_device_info = {
"name": self.name,
"identifiers": {(DOMAIN, device_id)},
"manufacturer": "Flume, Inc.",
"model": "Flume Smart Water Monitor",
}
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def native_value(self):
"""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:
return None
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):
"""Request an update when added."""
await super().async_added_to_hass()