Use device classes in volkszaehler (#83823)

pull/83838/head
epenet 2022-12-12 10:27:44 +01:00 committed by GitHub
parent 9cd159ee01
commit a30f14a15e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 14 deletions

View File

@ -10,6 +10,7 @@ import voluptuous as vol
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
PLATFORM_SCHEMA, PLATFORM_SCHEMA,
SensorDeviceClass,
SensorEntity, SensorEntity,
SensorEntityDescription, SensorEntityDescription,
) )
@ -18,8 +19,8 @@ from homeassistant.const import (
CONF_MONITORED_CONDITIONS, CONF_MONITORED_CONDITIONS,
CONF_NAME, CONF_NAME,
CONF_PORT, CONF_PORT,
ENERGY_WATT_HOUR, UnitOfEnergy,
POWER_WATT, UnitOfPower,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import PlatformNotReady from homeassistant.exceptions import PlatformNotReady
@ -43,25 +44,29 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
key="average", key="average",
name="Average", name="Average",
native_unit_of_measurement=POWER_WATT, native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
icon="mdi:power-off", icon="mdi:power-off",
), ),
SensorEntityDescription( SensorEntityDescription(
key="consumption", key="consumption",
name="Consumption", name="Consumption",
native_unit_of_measurement=ENERGY_WATT_HOUR, native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
icon="mdi:power-plug", icon="mdi:power-plug",
), ),
SensorEntityDescription( SensorEntityDescription(
key="max", key="max",
name="Max", name="Max",
native_unit_of_measurement=POWER_WATT, native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
icon="mdi:arrow-up", icon="mdi:arrow-up",
), ),
SensorEntityDescription( SensorEntityDescription(
key="min", key="min",
name="Min", name="Min",
native_unit_of_measurement=POWER_WATT, native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
icon="mdi:arrow-down", icon="mdi:arrow-down",
), ),
) )
@ -89,11 +94,11 @@ async def async_setup_platform(
) -> None: ) -> None:
"""Set up the Volkszaehler sensors.""" """Set up the Volkszaehler sensors."""
host = config[CONF_HOST] host: str = config[CONF_HOST]
name = config[CONF_NAME] name: str = config[CONF_NAME]
port = config[CONF_PORT] port: int = config[CONF_PORT]
uuid = config[CONF_UUID] uuid: str = config[CONF_UUID]
conditions = config[CONF_MONITORED_CONDITIONS] conditions: list[str] = config[CONF_MONITORED_CONDITIONS]
session = async_get_clientsession(hass) session = async_get_clientsession(hass)
vz_api = VolkszaehlerData(Volkszaehler(session, uuid, host=host, port=port)) vz_api = VolkszaehlerData(Volkszaehler(session, uuid, host=host, port=port))
@ -115,7 +120,9 @@ async def async_setup_platform(
class VolkszaehlerSensor(SensorEntity): class VolkszaehlerSensor(SensorEntity):
"""Implementation of a Volkszaehler sensor.""" """Implementation of a Volkszaehler sensor."""
def __init__(self, vz_api, name, description: SensorEntityDescription): def __init__(
self, vz_api: VolkszaehlerData, name: str, description: SensorEntityDescription
) -> None:
"""Initialize the Volkszaehler sensor.""" """Initialize the Volkszaehler sensor."""
self.entity_description = description self.entity_description = description
self.vz_api = vz_api self.vz_api = vz_api
@ -140,13 +147,13 @@ class VolkszaehlerSensor(SensorEntity):
class VolkszaehlerData: class VolkszaehlerData:
"""The class for handling the data retrieval from the Volkszaehler API.""" """The class for handling the data retrieval from the Volkszaehler API."""
def __init__(self, api): def __init__(self, api: Volkszaehler) -> None:
"""Initialize the data object.""" """Initialize the data object."""
self.api = api self.api = api
self.available = True self.available = True
@Throttle(MIN_TIME_BETWEEN_UPDATES) @Throttle(MIN_TIME_BETWEEN_UPDATES)
async def async_update(self): async def async_update(self) -> None:
"""Get the latest data from the Volkszaehler REST API.""" """Get the latest data from the Volkszaehler REST API."""
try: try: