mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Use EntityDescription - volkszaehler (#55034)
This commit is contained in:
parent
9b472aee9a
commit
c2b2c8604f
@ -1,4 +1,6 @@
|
|||||||
"""Support for consuming values for the Volkszaehler API."""
|
"""Support for consuming values for the Volkszaehler API."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
@ -6,7 +8,11 @@ from volkszaehler import Volkszaehler
|
|||||||
from volkszaehler.exceptions import VolkszaehlerApiConnectionError
|
from volkszaehler.exceptions import VolkszaehlerApiConnectionError
|
||||||
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.const import (
|
from homeassistant.const import (
|
||||||
CONF_HOST,
|
CONF_HOST,
|
||||||
CONF_MONITORED_CONDITIONS,
|
CONF_MONITORED_CONDITIONS,
|
||||||
@ -30,12 +36,34 @@ DEFAULT_PORT = 80
|
|||||||
|
|
||||||
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1)
|
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1)
|
||||||
|
|
||||||
SENSOR_TYPES = {
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||||
"average": ["Average", POWER_WATT, "mdi:power-off"],
|
SensorEntityDescription(
|
||||||
"consumption": ["Consumption", ENERGY_WATT_HOUR, "mdi:power-plug"],
|
key="average",
|
||||||
"max": ["Max", POWER_WATT, "mdi:arrow-up"],
|
name="Average",
|
||||||
"min": ["Min", POWER_WATT, "mdi:arrow-down"],
|
native_unit_of_measurement=POWER_WATT,
|
||||||
}
|
icon="mdi:power-off",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="consumption",
|
||||||
|
name="Consumption",
|
||||||
|
native_unit_of_measurement=ENERGY_WATT_HOUR,
|
||||||
|
icon="mdi:power-plug",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="max",
|
||||||
|
name="Max",
|
||||||
|
native_unit_of_measurement=POWER_WATT,
|
||||||
|
icon="mdi:arrow-up",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="min",
|
||||||
|
name="Min",
|
||||||
|
native_unit_of_measurement=POWER_WATT,
|
||||||
|
icon="mdi:arrow-down",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
SENSOR_KEYS: list[str] = [desc.key for desc in SENSOR_TYPES]
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
@ -44,7 +72,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||||
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
||||||
vol.Optional(CONF_MONITORED_CONDITIONS, default=["average"]): vol.All(
|
vol.Optional(CONF_MONITORED_CONDITIONS, default=["average"]): vol.All(
|
||||||
cv.ensure_list, [vol.In(SENSOR_TYPES)]
|
cv.ensure_list, [vol.In(SENSOR_KEYS)]
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -69,54 +97,38 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
if vz_api.api.data is None:
|
if vz_api.api.data is None:
|
||||||
raise PlatformNotReady
|
raise PlatformNotReady
|
||||||
|
|
||||||
dev = []
|
entities = [
|
||||||
for condition in conditions:
|
VolkszaehlerSensor(vz_api, name, description)
|
||||||
dev.append(VolkszaehlerSensor(vz_api, name, condition))
|
for description in SENSOR_TYPES
|
||||||
|
if description.key in conditions
|
||||||
|
]
|
||||||
|
|
||||||
async_add_entities(dev, True)
|
async_add_entities(entities, True)
|
||||||
|
|
||||||
|
|
||||||
class VolkszaehlerSensor(SensorEntity):
|
class VolkszaehlerSensor(SensorEntity):
|
||||||
"""Implementation of a Volkszaehler sensor."""
|
"""Implementation of a Volkszaehler sensor."""
|
||||||
|
|
||||||
def __init__(self, vz_api, name, sensor_type):
|
def __init__(self, vz_api, name, description: SensorEntityDescription):
|
||||||
"""Initialize the Volkszaehler sensor."""
|
"""Initialize the Volkszaehler sensor."""
|
||||||
|
self.entity_description = description
|
||||||
self.vz_api = vz_api
|
self.vz_api = vz_api
|
||||||
self._name = name
|
|
||||||
self.type = sensor_type
|
|
||||||
self._state = None
|
|
||||||
|
|
||||||
@property
|
self._attr_name = f"{name} {description.name}"
|
||||||
def name(self):
|
|
||||||
"""Return the name of the sensor."""
|
|
||||||
return f"{self._name} {SENSOR_TYPES[self.type][0]}"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def icon(self):
|
|
||||||
"""Icon to use in the frontend, if any."""
|
|
||||||
return SENSOR_TYPES[self.type][2]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_unit_of_measurement(self):
|
|
||||||
"""Return the unit the value is expressed in."""
|
|
||||||
return SENSOR_TYPES[self.type][1]
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self):
|
def available(self):
|
||||||
"""Could the device be accessed during the last update call."""
|
"""Could the device be accessed during the last update call."""
|
||||||
return self.vz_api.available
|
return self.vz_api.available
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self):
|
|
||||||
"""Return the state of the resources."""
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
"""Get the latest data from REST API."""
|
"""Get the latest data from REST API."""
|
||||||
await self.vz_api.async_update()
|
await self.vz_api.async_update()
|
||||||
|
|
||||||
if self.vz_api.api.data is not None:
|
if self.vz_api.api.data is not None:
|
||||||
self._state = round(getattr(self.vz_api.api, self.type), 2)
|
self._attr_native_value = round(
|
||||||
|
getattr(self.vz_api.api, self.entity_description.key), 2
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class VolkszaehlerData:
|
class VolkszaehlerData:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user