Use EntityDescription - wallbox (#58690)

This commit is contained in:
Marc Mueller 2021-10-31 00:33:07 +02:00 committed by GitHub
parent d6e49bc5bc
commit 733280e169
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 108 additions and 118 deletions

View File

@ -16,10 +16,9 @@ from .const import (
CONF_CONNECTIONS, CONF_CONNECTIONS,
CONF_DATA_KEY, CONF_DATA_KEY,
CONF_MAX_CHARGING_CURRENT_KEY, CONF_MAX_CHARGING_CURRENT_KEY,
CONF_ROUND,
CONF_SENSOR_TYPES,
CONF_STATION, CONF_STATION,
DOMAIN, DOMAIN,
SENSOR_TYPES,
) )
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -72,10 +71,10 @@ class WallboxCoordinator(DataUpdateCoordinator):
CONF_MAX_CHARGING_CURRENT_KEY 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(): 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: try:
filtered_data[key] = round(value, sensor_round) filtered_data[key] = round(value, sensor_round)
except TypeError: except TypeError:

View File

@ -1,9 +1,10 @@
"""Constants for the Wallbox integration.""" """Constants for the Wallbox integration."""
from __future__ import annotations
from dataclasses import dataclass
from homeassistant.components.sensor import SensorEntityDescription
from homeassistant.const import ( from homeassistant.const import (
CONF_DEVICE_CLASS,
CONF_ICON,
CONF_NAME,
CONF_UNIT_OF_MEASUREMENT,
DEVICE_CLASS_BATTERY, DEVICE_CLASS_BATTERY,
DEVICE_CLASS_CURRENT, DEVICE_CLASS_CURRENT,
DEVICE_CLASS_ENERGY, DEVICE_CLASS_ENERGY,
@ -33,91 +34,86 @@ CONF_STATE_OF_CHARGE_KEY = "state_of_charge"
CONF_STATUS_DESCRIPTION_KEY = "status_description" CONF_STATUS_DESCRIPTION_KEY = "status_description"
CONF_CONNECTIONS = "connections" CONF_CONNECTIONS = "connections"
CONF_ROUND = "round"
CONF_SENSOR_TYPES = {
CONF_CHARGING_POWER_KEY: { @dataclass
CONF_ICON: None, class WallboxSensorEntityDescription(SensorEntityDescription):
CONF_NAME: "Charging Power", """Describes Wallbox sensor entity."""
CONF_ROUND: 2,
CONF_UNIT_OF_MEASUREMENT: POWER_KILO_WATT, precision: int | None = None
CONF_DEVICE_CLASS: DEVICE_CLASS_POWER,
},
CONF_MAX_AVAILABLE_POWER_KEY: { SENSOR_TYPES: dict[str, WallboxSensorEntityDescription] = {
CONF_ICON: None, CONF_CHARGING_POWER_KEY: WallboxSensorEntityDescription(
CONF_NAME: "Max Available Power", key=CONF_CHARGING_POWER_KEY,
CONF_ROUND: 0, name="Charging Power",
CONF_UNIT_OF_MEASUREMENT: ELECTRIC_CURRENT_AMPERE, precision=2,
CONF_DEVICE_CLASS: DEVICE_CLASS_CURRENT, native_unit_of_measurement=POWER_KILO_WATT,
}, device_class=DEVICE_CLASS_POWER,
CONF_CHARGING_SPEED_KEY: { ),
CONF_ICON: "mdi:speedometer", CONF_MAX_AVAILABLE_POWER_KEY: WallboxSensorEntityDescription(
CONF_NAME: "Charging Speed", key=CONF_MAX_AVAILABLE_POWER_KEY,
CONF_ROUND: 0, name="Max Available Power",
CONF_UNIT_OF_MEASUREMENT: None, precision=0,
CONF_DEVICE_CLASS: None, native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
}, device_class=DEVICE_CLASS_CURRENT,
CONF_ADDED_RANGE_KEY: { ),
CONF_ICON: "mdi:map-marker-distance", CONF_CHARGING_SPEED_KEY: WallboxSensorEntityDescription(
CONF_NAME: "Added Range", key=CONF_CHARGING_SPEED_KEY,
CONF_ROUND: 0, icon="mdi:speedometer",
CONF_UNIT_OF_MEASUREMENT: LENGTH_KILOMETERS, name="Charging Speed",
CONF_DEVICE_CLASS: None, precision=0,
}, ),
CONF_ADDED_ENERGY_KEY: { CONF_ADDED_RANGE_KEY: WallboxSensorEntityDescription(
CONF_ICON: None, key=CONF_ADDED_RANGE_KEY,
CONF_NAME: "Added Energy", icon="mdi:map-marker-distance",
CONF_ROUND: 2, name="Added Range",
CONF_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR, precision=0,
CONF_DEVICE_CLASS: DEVICE_CLASS_ENERGY, native_unit_of_measurement=LENGTH_KILOMETERS,
}, ),
CONF_CHARGING_TIME_KEY: { CONF_ADDED_ENERGY_KEY: WallboxSensorEntityDescription(
CONF_ICON: "mdi:timer", key=CONF_ADDED_ENERGY_KEY,
CONF_NAME: "Charging Time", name="Added Energy",
CONF_ROUND: None, precision=2,
CONF_UNIT_OF_MEASUREMENT: None, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
CONF_DEVICE_CLASS: None, device_class=DEVICE_CLASS_ENERGY,
}, ),
CONF_COST_KEY: { CONF_CHARGING_TIME_KEY: WallboxSensorEntityDescription(
CONF_ICON: "mdi:ev-station", key=CONF_CHARGING_TIME_KEY,
CONF_NAME: "Cost", icon="mdi:timer",
CONF_ROUND: None, name="Charging Time",
CONF_UNIT_OF_MEASUREMENT: None, ),
CONF_DEVICE_CLASS: None, CONF_COST_KEY: WallboxSensorEntityDescription(
}, key=CONF_COST_KEY,
CONF_STATE_OF_CHARGE_KEY: { icon="mdi:ev-station",
CONF_ICON: None, name="Cost",
CONF_NAME: "State of Charge", ),
CONF_ROUND: None, CONF_STATE_OF_CHARGE_KEY: WallboxSensorEntityDescription(
CONF_UNIT_OF_MEASUREMENT: PERCENTAGE, key=CONF_STATE_OF_CHARGE_KEY,
CONF_DEVICE_CLASS: DEVICE_CLASS_BATTERY, name="State of Charge",
}, native_unit_of_measurement=PERCENTAGE,
CONF_CURRENT_MODE_KEY: { device_class=DEVICE_CLASS_BATTERY,
CONF_ICON: "mdi:ev-station", ),
CONF_NAME: "Current Mode", CONF_CURRENT_MODE_KEY: WallboxSensorEntityDescription(
CONF_ROUND: None, key=CONF_CURRENT_MODE_KEY,
CONF_UNIT_OF_MEASUREMENT: None, icon="mdi:ev-station",
CONF_DEVICE_CLASS: None, name="Current Mode",
}, ),
CONF_DEPOT_PRICE_KEY: { CONF_DEPOT_PRICE_KEY: WallboxSensorEntityDescription(
CONF_ICON: "mdi:ev-station", key=CONF_DEPOT_PRICE_KEY,
CONF_NAME: "Depot Price", icon="mdi:ev-station",
CONF_ROUND: 2, name="Depot Price",
CONF_UNIT_OF_MEASUREMENT: None, precision=2,
CONF_DEVICE_CLASS: None, ),
}, CONF_STATUS_DESCRIPTION_KEY: WallboxSensorEntityDescription(
CONF_STATUS_DESCRIPTION_KEY: { key=CONF_STATUS_DESCRIPTION_KEY,
CONF_ICON: "mdi:ev-station", icon="mdi:ev-station",
CONF_NAME: "Status Description", name="Status Description",
CONF_ROUND: None, ),
CONF_UNIT_OF_MEASUREMENT: None, CONF_MAX_CHARGING_CURRENT_KEY: WallboxSensorEntityDescription(
CONF_DEVICE_CLASS: None, key=CONF_MAX_CHARGING_CURRENT_KEY,
}, name="Max. Charging Current",
CONF_MAX_CHARGING_CURRENT_KEY: { native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
CONF_ICON: None, device_class=DEVICE_CLASS_CURRENT,
CONF_NAME: "Max. Charging Current", ),
CONF_ROUND: None,
CONF_UNIT_OF_MEASUREMENT: ELECTRIC_CURRENT_AMPERE,
CONF_DEVICE_CLASS: DEVICE_CLASS_CURRENT,
},
} }

View File

@ -1,7 +1,5 @@
"""Home Assistant component for accessing the Wallbox Portal API. The sensor component creates multiple sensors regarding wallbox performance.""" """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.components.number import NumberEntity
from homeassistant.const import CONF_DEVICE_CLASS
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import InvalidAuth from . import InvalidAuth
@ -9,9 +7,8 @@ from .const import (
CONF_CONNECTIONS, CONF_CONNECTIONS,
CONF_MAX_AVAILABLE_POWER_KEY, CONF_MAX_AVAILABLE_POWER_KEY,
CONF_MAX_CHARGING_CURRENT_KEY, CONF_MAX_CHARGING_CURRENT_KEY,
CONF_NAME,
CONF_SENSOR_TYPES,
DOMAIN, DOMAIN,
SENSOR_TYPES,
) )
@ -35,11 +32,11 @@ class WallboxNumber(CoordinatorEntity, NumberEntity):
def __init__(self, coordinator, config): def __init__(self, coordinator, config):
"""Initialize a Wallbox sensor.""" """Initialize a Wallbox sensor."""
super().__init__(coordinator) 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._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_min_value = 6
self._attr_device_class = _properties[CONF_DEVICE_CLASS] self._attr_device_class = sensor_description.device_class
@property @property
def max_value(self): def max_value(self):

View File

@ -1,16 +1,12 @@
"""Home Assistant component for accessing the Wallbox Portal API. The sensor component creates multiple sensors regarding wallbox performance.""" """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.components.sensor import SensorEntity
from homeassistant.const import CONF_DEVICE_CLASS
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import ( from .const import (
CONF_CONNECTIONS, CONF_CONNECTIONS,
CONF_ICON,
CONF_NAME,
CONF_SENSOR_TYPES,
CONF_UNIT_OF_MEASUREMENT,
DOMAIN, DOMAIN,
SENSOR_TYPES,
WallboxSensorEntityDescription,
) )
CONF_STATION = "station" 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] coordinator = hass.data[DOMAIN][CONF_CONNECTIONS][config.entry_id]
async_add_entities( 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): class WallboxSensor(CoordinatorEntity, SensorEntity):
"""Representation of the Wallbox portal.""" """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.""" """Initialize a Wallbox sensor."""
super().__init__(coordinator) super().__init__(coordinator)
self._attr_name = f"{config.title} {CONF_SENSOR_TYPES[ent][CONF_NAME]}" self.entity_description = description
self._attr_icon = CONF_SENSOR_TYPES[ent][CONF_ICON] self._attr_name = f"{config.title} {description.name}"
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
@property @property
def native_value(self): def native_value(self):
"""Return the state of the sensor.""" """Return the state of the sensor."""
return self.coordinator.data[self._ent] return self.coordinator.data[self.entity_description.key]