From 733280e1690c6c075d50d4168ea5109678bd8022 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Sun, 31 Oct 2021 00:33:07 +0200 Subject: [PATCH] Use EntityDescription - wallbox (#58690) --- homeassistant/components/wallbox/__init__.py | 7 +- homeassistant/components/wallbox/const.py | 176 +++++++++---------- homeassistant/components/wallbox/number.py | 11 +- homeassistant/components/wallbox/sensor.py | 32 ++-- 4 files changed, 108 insertions(+), 118 deletions(-) diff --git a/homeassistant/components/wallbox/__init__.py b/homeassistant/components/wallbox/__init__.py index 410a3115f9f..a1361984606 100644 --- a/homeassistant/components/wallbox/__init__.py +++ b/homeassistant/components/wallbox/__init__.py @@ -16,10 +16,9 @@ from .const import ( CONF_CONNECTIONS, CONF_DATA_KEY, CONF_MAX_CHARGING_CURRENT_KEY, - CONF_ROUND, - CONF_SENSOR_TYPES, CONF_STATION, DOMAIN, + SENSOR_TYPES, ) _LOGGER = logging.getLogger(__name__) @@ -72,10 +71,10 @@ class WallboxCoordinator(DataUpdateCoordinator): CONF_MAX_CHARGING_CURRENT_KEY ] - filtered_data = {k: data[k] for k in CONF_SENSOR_TYPES if k in data} + filtered_data = {k: data[k] for k in SENSOR_TYPES if k in data} for key, value in filtered_data.items(): - if (sensor_round := CONF_SENSOR_TYPES[key][CONF_ROUND]) is not None: + if (sensor_round := SENSOR_TYPES[key].precision) is not None: try: filtered_data[key] = round(value, sensor_round) except TypeError: diff --git a/homeassistant/components/wallbox/const.py b/homeassistant/components/wallbox/const.py index 62c9b2f6efd..26af7f4c499 100644 --- a/homeassistant/components/wallbox/const.py +++ b/homeassistant/components/wallbox/const.py @@ -1,9 +1,10 @@ """Constants for the Wallbox integration.""" +from __future__ import annotations + +from dataclasses import dataclass + +from homeassistant.components.sensor import SensorEntityDescription from homeassistant.const import ( - CONF_DEVICE_CLASS, - CONF_ICON, - CONF_NAME, - CONF_UNIT_OF_MEASUREMENT, DEVICE_CLASS_BATTERY, DEVICE_CLASS_CURRENT, DEVICE_CLASS_ENERGY, @@ -33,91 +34,86 @@ CONF_STATE_OF_CHARGE_KEY = "state_of_charge" CONF_STATUS_DESCRIPTION_KEY = "status_description" CONF_CONNECTIONS = "connections" -CONF_ROUND = "round" -CONF_SENSOR_TYPES = { - CONF_CHARGING_POWER_KEY: { - CONF_ICON: None, - CONF_NAME: "Charging Power", - CONF_ROUND: 2, - CONF_UNIT_OF_MEASUREMENT: POWER_KILO_WATT, - CONF_DEVICE_CLASS: DEVICE_CLASS_POWER, - }, - CONF_MAX_AVAILABLE_POWER_KEY: { - CONF_ICON: None, - CONF_NAME: "Max Available Power", - CONF_ROUND: 0, - CONF_UNIT_OF_MEASUREMENT: ELECTRIC_CURRENT_AMPERE, - CONF_DEVICE_CLASS: DEVICE_CLASS_CURRENT, - }, - CONF_CHARGING_SPEED_KEY: { - CONF_ICON: "mdi:speedometer", - CONF_NAME: "Charging Speed", - CONF_ROUND: 0, - CONF_UNIT_OF_MEASUREMENT: None, - CONF_DEVICE_CLASS: None, - }, - CONF_ADDED_RANGE_KEY: { - CONF_ICON: "mdi:map-marker-distance", - CONF_NAME: "Added Range", - CONF_ROUND: 0, - CONF_UNIT_OF_MEASUREMENT: LENGTH_KILOMETERS, - CONF_DEVICE_CLASS: None, - }, - CONF_ADDED_ENERGY_KEY: { - CONF_ICON: None, - CONF_NAME: "Added Energy", - CONF_ROUND: 2, - CONF_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR, - CONF_DEVICE_CLASS: DEVICE_CLASS_ENERGY, - }, - CONF_CHARGING_TIME_KEY: { - CONF_ICON: "mdi:timer", - CONF_NAME: "Charging Time", - CONF_ROUND: None, - CONF_UNIT_OF_MEASUREMENT: None, - CONF_DEVICE_CLASS: None, - }, - CONF_COST_KEY: { - CONF_ICON: "mdi:ev-station", - CONF_NAME: "Cost", - CONF_ROUND: None, - CONF_UNIT_OF_MEASUREMENT: None, - CONF_DEVICE_CLASS: None, - }, - CONF_STATE_OF_CHARGE_KEY: { - CONF_ICON: None, - CONF_NAME: "State of Charge", - CONF_ROUND: None, - CONF_UNIT_OF_MEASUREMENT: PERCENTAGE, - CONF_DEVICE_CLASS: DEVICE_CLASS_BATTERY, - }, - CONF_CURRENT_MODE_KEY: { - CONF_ICON: "mdi:ev-station", - CONF_NAME: "Current Mode", - CONF_ROUND: None, - CONF_UNIT_OF_MEASUREMENT: None, - CONF_DEVICE_CLASS: None, - }, - CONF_DEPOT_PRICE_KEY: { - CONF_ICON: "mdi:ev-station", - CONF_NAME: "Depot Price", - CONF_ROUND: 2, - CONF_UNIT_OF_MEASUREMENT: None, - CONF_DEVICE_CLASS: None, - }, - CONF_STATUS_DESCRIPTION_KEY: { - CONF_ICON: "mdi:ev-station", - CONF_NAME: "Status Description", - CONF_ROUND: None, - CONF_UNIT_OF_MEASUREMENT: None, - CONF_DEVICE_CLASS: None, - }, - CONF_MAX_CHARGING_CURRENT_KEY: { - CONF_ICON: None, - CONF_NAME: "Max. Charging Current", - CONF_ROUND: None, - CONF_UNIT_OF_MEASUREMENT: ELECTRIC_CURRENT_AMPERE, - CONF_DEVICE_CLASS: DEVICE_CLASS_CURRENT, - }, + +@dataclass +class WallboxSensorEntityDescription(SensorEntityDescription): + """Describes Wallbox sensor entity.""" + + precision: int | None = None + + +SENSOR_TYPES: dict[str, WallboxSensorEntityDescription] = { + CONF_CHARGING_POWER_KEY: WallboxSensorEntityDescription( + key=CONF_CHARGING_POWER_KEY, + name="Charging Power", + precision=2, + native_unit_of_measurement=POWER_KILO_WATT, + device_class=DEVICE_CLASS_POWER, + ), + CONF_MAX_AVAILABLE_POWER_KEY: WallboxSensorEntityDescription( + key=CONF_MAX_AVAILABLE_POWER_KEY, + name="Max Available Power", + precision=0, + native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE, + device_class=DEVICE_CLASS_CURRENT, + ), + CONF_CHARGING_SPEED_KEY: WallboxSensorEntityDescription( + key=CONF_CHARGING_SPEED_KEY, + icon="mdi:speedometer", + name="Charging Speed", + precision=0, + ), + CONF_ADDED_RANGE_KEY: WallboxSensorEntityDescription( + key=CONF_ADDED_RANGE_KEY, + icon="mdi:map-marker-distance", + name="Added Range", + precision=0, + native_unit_of_measurement=LENGTH_KILOMETERS, + ), + CONF_ADDED_ENERGY_KEY: WallboxSensorEntityDescription( + key=CONF_ADDED_ENERGY_KEY, + name="Added Energy", + precision=2, + native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, + device_class=DEVICE_CLASS_ENERGY, + ), + CONF_CHARGING_TIME_KEY: WallboxSensorEntityDescription( + key=CONF_CHARGING_TIME_KEY, + icon="mdi:timer", + name="Charging Time", + ), + CONF_COST_KEY: WallboxSensorEntityDescription( + key=CONF_COST_KEY, + icon="mdi:ev-station", + name="Cost", + ), + CONF_STATE_OF_CHARGE_KEY: WallboxSensorEntityDescription( + key=CONF_STATE_OF_CHARGE_KEY, + name="State of Charge", + native_unit_of_measurement=PERCENTAGE, + device_class=DEVICE_CLASS_BATTERY, + ), + CONF_CURRENT_MODE_KEY: WallboxSensorEntityDescription( + key=CONF_CURRENT_MODE_KEY, + icon="mdi:ev-station", + name="Current Mode", + ), + CONF_DEPOT_PRICE_KEY: WallboxSensorEntityDescription( + key=CONF_DEPOT_PRICE_KEY, + icon="mdi:ev-station", + name="Depot Price", + precision=2, + ), + CONF_STATUS_DESCRIPTION_KEY: WallboxSensorEntityDescription( + key=CONF_STATUS_DESCRIPTION_KEY, + icon="mdi:ev-station", + name="Status Description", + ), + CONF_MAX_CHARGING_CURRENT_KEY: WallboxSensorEntityDescription( + key=CONF_MAX_CHARGING_CURRENT_KEY, + name="Max. Charging Current", + native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE, + device_class=DEVICE_CLASS_CURRENT, + ), } diff --git a/homeassistant/components/wallbox/number.py b/homeassistant/components/wallbox/number.py index d99d6511822..01e453593f3 100644 --- a/homeassistant/components/wallbox/number.py +++ b/homeassistant/components/wallbox/number.py @@ -1,7 +1,5 @@ """Home Assistant component for accessing the Wallbox Portal API. The sensor component creates multiple sensors regarding wallbox performance.""" - from homeassistant.components.number import NumberEntity -from homeassistant.const import CONF_DEVICE_CLASS from homeassistant.helpers.update_coordinator import CoordinatorEntity from . import InvalidAuth @@ -9,9 +7,8 @@ from .const import ( CONF_CONNECTIONS, CONF_MAX_AVAILABLE_POWER_KEY, CONF_MAX_CHARGING_CURRENT_KEY, - CONF_NAME, - CONF_SENSOR_TYPES, DOMAIN, + SENSOR_TYPES, ) @@ -35,11 +32,11 @@ class WallboxNumber(CoordinatorEntity, NumberEntity): def __init__(self, coordinator, config): """Initialize a Wallbox sensor.""" super().__init__(coordinator) - _properties = CONF_SENSOR_TYPES[CONF_MAX_CHARGING_CURRENT_KEY] + sensor_description = SENSOR_TYPES[CONF_MAX_CHARGING_CURRENT_KEY] self._coordinator = coordinator - self._attr_name = f"{config.title} {_properties[CONF_NAME]}" + self._attr_name = f"{config.title} {sensor_description.name}" self._attr_min_value = 6 - self._attr_device_class = _properties[CONF_DEVICE_CLASS] + self._attr_device_class = sensor_description.device_class @property def max_value(self): diff --git a/homeassistant/components/wallbox/sensor.py b/homeassistant/components/wallbox/sensor.py index 37450a5ea79..3b87d3b29c3 100644 --- a/homeassistant/components/wallbox/sensor.py +++ b/homeassistant/components/wallbox/sensor.py @@ -1,16 +1,12 @@ """Home Assistant component for accessing the Wallbox Portal API. The sensor component creates multiple sensors regarding wallbox performance.""" - from homeassistant.components.sensor import SensorEntity -from homeassistant.const import CONF_DEVICE_CLASS from homeassistant.helpers.update_coordinator import CoordinatorEntity from .const import ( CONF_CONNECTIONS, - CONF_ICON, - CONF_NAME, - CONF_SENSOR_TYPES, - CONF_UNIT_OF_MEASUREMENT, DOMAIN, + SENSOR_TYPES, + WallboxSensorEntityDescription, ) CONF_STATION = "station" @@ -22,26 +18,28 @@ async def async_setup_entry(hass, config, async_add_entities): coordinator = hass.data[DOMAIN][CONF_CONNECTIONS][config.entry_id] async_add_entities( - WallboxSensor(coordinator, idx, ent, config) - for idx, ent in enumerate(coordinator.data) + [ + WallboxSensor(coordinator, config, description) + for ent in coordinator.data + if (description := SENSOR_TYPES[ent]) + ] ) class WallboxSensor(CoordinatorEntity, SensorEntity): """Representation of the Wallbox portal.""" - def __init__(self, coordinator, idx, ent, config): + entity_description: WallboxSensorEntityDescription + + def __init__( + self, coordinator, config, description: WallboxSensorEntityDescription + ): """Initialize a Wallbox sensor.""" super().__init__(coordinator) - self._attr_name = f"{config.title} {CONF_SENSOR_TYPES[ent][CONF_NAME]}" - self._attr_icon = CONF_SENSOR_TYPES[ent][CONF_ICON] - self._attr_native_unit_of_measurement = CONF_SENSOR_TYPES[ent][ - CONF_UNIT_OF_MEASUREMENT - ] - self._attr_device_class = CONF_SENSOR_TYPES[ent][CONF_DEVICE_CLASS] - self._ent = ent + self.entity_description = description + self._attr_name = f"{config.title} {description.name}" @property def native_value(self): """Return the state of the sensor.""" - return self.coordinator.data[self._ent] + return self.coordinator.data[self.entity_description.key]