2021-05-24 11:08:24 +00:00
|
|
|
"""Home Assistant component for accessing the Wallbox Portal API. The sensor component creates multiple sensors regarding wallbox performance."""
|
2021-11-02 10:11:46 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from dataclasses import dataclass
|
|
|
|
import logging
|
2021-11-23 21:30:22 +00:00
|
|
|
from typing import cast
|
2021-11-02 10:11:46 +00:00
|
|
|
|
2021-11-04 08:58:58 +00:00
|
|
|
from homeassistant.components.sensor import (
|
2021-12-17 11:46:21 +00:00
|
|
|
SensorDeviceClass,
|
2021-11-04 08:58:58 +00:00
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
2021-12-17 11:46:21 +00:00
|
|
|
SensorStateClass,
|
2021-11-04 08:58:58 +00:00
|
|
|
)
|
2021-11-23 21:30:22 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-11-02 10:11:46 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ELECTRIC_CURRENT_AMPERE,
|
|
|
|
ENERGY_KILO_WATT_HOUR,
|
|
|
|
LENGTH_KILOMETERS,
|
|
|
|
PERCENTAGE,
|
|
|
|
POWER_KILO_WATT,
|
|
|
|
)
|
2021-11-23 21:30:22 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import StateType
|
2021-05-24 11:08:24 +00:00
|
|
|
|
2022-01-04 14:46:57 +00:00
|
|
|
from . import WallboxCoordinator, WallboxEntity
|
2021-05-24 11:08:24 +00:00
|
|
|
from .const import (
|
2022-07-09 18:41:39 +00:00
|
|
|
CHARGER_ADDED_DISCHARGED_ENERGY_KEY,
|
2022-04-19 06:44:25 +00:00
|
|
|
CHARGER_ADDED_ENERGY_KEY,
|
|
|
|
CHARGER_ADDED_RANGE_KEY,
|
|
|
|
CHARGER_CHARGING_POWER_KEY,
|
|
|
|
CHARGER_CHARGING_SPEED_KEY,
|
|
|
|
CHARGER_COST_KEY,
|
2022-11-18 16:36:38 +00:00
|
|
|
CHARGER_CURRENCY_KEY,
|
2022-04-19 06:44:25 +00:00
|
|
|
CHARGER_CURRENT_MODE_KEY,
|
|
|
|
CHARGER_DATA_KEY,
|
|
|
|
CHARGER_DEPOT_PRICE_KEY,
|
2022-11-18 16:36:38 +00:00
|
|
|
CHARGER_ENERGY_PRICE_KEY,
|
2022-04-19 06:44:25 +00:00
|
|
|
CHARGER_MAX_AVAILABLE_POWER_KEY,
|
|
|
|
CHARGER_MAX_CHARGING_CURRENT_KEY,
|
|
|
|
CHARGER_SERIAL_NUMBER_KEY,
|
|
|
|
CHARGER_STATE_OF_CHARGE_KEY,
|
|
|
|
CHARGER_STATUS_DESCRIPTION_KEY,
|
2021-05-24 11:08:24 +00:00
|
|
|
DOMAIN,
|
|
|
|
)
|
|
|
|
|
2022-04-19 06:44:25 +00:00
|
|
|
CHARGER_STATION = "station"
|
2021-05-24 11:08:24 +00:00
|
|
|
UPDATE_INTERVAL = 30
|
|
|
|
|
2021-11-02 10:11:46 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class WallboxSensorEntityDescription(SensorEntityDescription):
|
|
|
|
"""Describes Wallbox sensor entity."""
|
|
|
|
|
|
|
|
precision: int | None = None
|
|
|
|
|
|
|
|
|
|
|
|
SENSOR_TYPES: dict[str, WallboxSensorEntityDescription] = {
|
2022-04-19 06:44:25 +00:00
|
|
|
CHARGER_CHARGING_POWER_KEY: WallboxSensorEntityDescription(
|
|
|
|
key=CHARGER_CHARGING_POWER_KEY,
|
2021-11-02 10:11:46 +00:00
|
|
|
name="Charging Power",
|
|
|
|
precision=2,
|
|
|
|
native_unit_of_measurement=POWER_KILO_WATT,
|
2021-12-17 11:46:21 +00:00
|
|
|
device_class=SensorDeviceClass.POWER,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-11-02 10:11:46 +00:00
|
|
|
),
|
2022-04-19 06:44:25 +00:00
|
|
|
CHARGER_MAX_AVAILABLE_POWER_KEY: WallboxSensorEntityDescription(
|
|
|
|
key=CHARGER_MAX_AVAILABLE_POWER_KEY,
|
2021-11-02 10:11:46 +00:00
|
|
|
name="Max Available Power",
|
|
|
|
precision=0,
|
|
|
|
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
|
2021-12-17 11:46:21 +00:00
|
|
|
device_class=SensorDeviceClass.CURRENT,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-11-02 10:11:46 +00:00
|
|
|
),
|
2022-04-19 06:44:25 +00:00
|
|
|
CHARGER_CHARGING_SPEED_KEY: WallboxSensorEntityDescription(
|
|
|
|
key=CHARGER_CHARGING_SPEED_KEY,
|
2021-11-02 10:11:46 +00:00
|
|
|
icon="mdi:speedometer",
|
|
|
|
name="Charging Speed",
|
|
|
|
precision=0,
|
2021-12-17 11:46:21 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-11-02 10:11:46 +00:00
|
|
|
),
|
2022-04-19 06:44:25 +00:00
|
|
|
CHARGER_ADDED_RANGE_KEY: WallboxSensorEntityDescription(
|
|
|
|
key=CHARGER_ADDED_RANGE_KEY,
|
2021-11-02 10:11:46 +00:00
|
|
|
icon="mdi:map-marker-distance",
|
|
|
|
name="Added Range",
|
|
|
|
precision=0,
|
|
|
|
native_unit_of_measurement=LENGTH_KILOMETERS,
|
2022-09-29 10:19:34 +00:00
|
|
|
device_class=SensorDeviceClass.DISTANCE,
|
2021-12-17 11:46:21 +00:00
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
2021-11-02 10:11:46 +00:00
|
|
|
),
|
2022-04-19 06:44:25 +00:00
|
|
|
CHARGER_ADDED_ENERGY_KEY: WallboxSensorEntityDescription(
|
|
|
|
key=CHARGER_ADDED_ENERGY_KEY,
|
2021-11-02 10:11:46 +00:00
|
|
|
name="Added Energy",
|
|
|
|
precision=2,
|
|
|
|
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
2021-12-17 11:46:21 +00:00
|
|
|
device_class=SensorDeviceClass.ENERGY,
|
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
2022-07-09 18:41:39 +00:00
|
|
|
),
|
|
|
|
CHARGER_ADDED_DISCHARGED_ENERGY_KEY: WallboxSensorEntityDescription(
|
|
|
|
key=CHARGER_ADDED_DISCHARGED_ENERGY_KEY,
|
|
|
|
name="Discharged Energy",
|
|
|
|
precision=2,
|
|
|
|
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
|
|
|
device_class=SensorDeviceClass.ENERGY,
|
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
2021-11-02 10:11:46 +00:00
|
|
|
),
|
2022-04-19 06:44:25 +00:00
|
|
|
CHARGER_COST_KEY: WallboxSensorEntityDescription(
|
|
|
|
key=CHARGER_COST_KEY,
|
2021-11-02 10:11:46 +00:00
|
|
|
icon="mdi:ev-station",
|
|
|
|
name="Cost",
|
2021-12-17 11:46:21 +00:00
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
2021-11-02 10:11:46 +00:00
|
|
|
),
|
2022-04-19 06:44:25 +00:00
|
|
|
CHARGER_STATE_OF_CHARGE_KEY: WallboxSensorEntityDescription(
|
|
|
|
key=CHARGER_STATE_OF_CHARGE_KEY,
|
2021-11-02 10:11:46 +00:00
|
|
|
name="State of Charge",
|
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
2021-12-17 11:46:21 +00:00
|
|
|
device_class=SensorDeviceClass.BATTERY,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-11-02 10:11:46 +00:00
|
|
|
),
|
2022-04-19 06:44:25 +00:00
|
|
|
CHARGER_CURRENT_MODE_KEY: WallboxSensorEntityDescription(
|
|
|
|
key=CHARGER_CURRENT_MODE_KEY,
|
2021-11-02 10:11:46 +00:00
|
|
|
icon="mdi:ev-station",
|
|
|
|
name="Current Mode",
|
|
|
|
),
|
2022-04-19 06:44:25 +00:00
|
|
|
CHARGER_DEPOT_PRICE_KEY: WallboxSensorEntityDescription(
|
|
|
|
key=CHARGER_DEPOT_PRICE_KEY,
|
2021-11-02 10:11:46 +00:00
|
|
|
icon="mdi:ev-station",
|
|
|
|
name="Depot Price",
|
|
|
|
precision=2,
|
2022-11-18 16:36:38 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
),
|
|
|
|
CHARGER_ENERGY_PRICE_KEY: WallboxSensorEntityDescription(
|
|
|
|
key=CHARGER_ENERGY_PRICE_KEY,
|
|
|
|
icon="mdi:ev-station",
|
|
|
|
name="Energy Price",
|
|
|
|
precision=2,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-11-02 10:11:46 +00:00
|
|
|
),
|
2022-04-19 06:44:25 +00:00
|
|
|
CHARGER_STATUS_DESCRIPTION_KEY: WallboxSensorEntityDescription(
|
|
|
|
key=CHARGER_STATUS_DESCRIPTION_KEY,
|
2021-11-02 10:11:46 +00:00
|
|
|
icon="mdi:ev-station",
|
|
|
|
name="Status Description",
|
|
|
|
),
|
2022-04-19 06:44:25 +00:00
|
|
|
CHARGER_MAX_CHARGING_CURRENT_KEY: WallboxSensorEntityDescription(
|
|
|
|
key=CHARGER_MAX_CHARGING_CURRENT_KEY,
|
2021-11-02 10:11:46 +00:00
|
|
|
name="Max. Charging Current",
|
|
|
|
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
|
2021-12-17 11:46:21 +00:00
|
|
|
device_class=SensorDeviceClass.CURRENT,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-11-02 10:11:46 +00:00
|
|
|
),
|
|
|
|
}
|
|
|
|
|
2021-05-24 11:08:24 +00:00
|
|
|
|
2021-11-23 21:30:22 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2021-05-24 11:08:24 +00:00
|
|
|
"""Create wallbox sensor entities in HASS."""
|
2021-11-23 21:30:22 +00:00
|
|
|
coordinator: WallboxCoordinator = hass.data[DOMAIN][entry.entry_id]
|
2021-05-24 11:08:24 +00:00
|
|
|
|
|
|
|
async_add_entities(
|
2021-10-30 22:33:07 +00:00
|
|
|
[
|
2021-11-23 21:30:22 +00:00
|
|
|
WallboxSensor(coordinator, entry, description)
|
2021-10-30 22:33:07 +00:00
|
|
|
for ent in coordinator.data
|
2021-11-02 10:11:46 +00:00
|
|
|
if (description := SENSOR_TYPES.get(ent))
|
2021-10-30 22:33:07 +00:00
|
|
|
]
|
2021-05-24 11:08:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-01-04 14:46:57 +00:00
|
|
|
class WallboxSensor(WallboxEntity, SensorEntity):
|
2021-05-24 11:08:24 +00:00
|
|
|
"""Representation of the Wallbox portal."""
|
|
|
|
|
2021-10-30 22:33:07 +00:00
|
|
|
entity_description: WallboxSensorEntityDescription
|
|
|
|
|
|
|
|
def __init__(
|
2021-11-23 21:30:22 +00:00
|
|
|
self,
|
|
|
|
coordinator: WallboxCoordinator,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
description: WallboxSensorEntityDescription,
|
|
|
|
) -> None:
|
2021-05-24 11:08:24 +00:00
|
|
|
"""Initialize a Wallbox sensor."""
|
|
|
|
super().__init__(coordinator)
|
2021-10-30 22:33:07 +00:00
|
|
|
self.entity_description = description
|
2021-11-23 21:30:22 +00:00
|
|
|
self._attr_name = f"{entry.title} {description.name}"
|
2022-04-19 06:44:25 +00:00
|
|
|
self._attr_unique_id = f"{description.key}-{coordinator.data[CHARGER_DATA_KEY][CHARGER_SERIAL_NUMBER_KEY]}"
|
2021-05-24 11:08:24 +00:00
|
|
|
|
|
|
|
@property
|
2021-11-23 21:30:22 +00:00
|
|
|
def native_value(self) -> StateType:
|
2022-06-10 18:55:55 +00:00
|
|
|
"""Return the state of the sensor. Round the value when it, and the precision property are not None."""
|
|
|
|
if (
|
|
|
|
sensor_round := self.entity_description.precision
|
|
|
|
) is not None and self.coordinator.data[
|
|
|
|
self.entity_description.key
|
|
|
|
] is not None:
|
2022-03-24 21:09:59 +00:00
|
|
|
return cast(
|
|
|
|
StateType,
|
|
|
|
round(self.coordinator.data[self.entity_description.key], sensor_round),
|
|
|
|
)
|
2021-11-23 21:30:22 +00:00
|
|
|
return cast(StateType, self.coordinator.data[self.entity_description.key])
|
2022-11-18 16:36:38 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def native_unit_of_measurement(self) -> str | None:
|
|
|
|
"""Return the unit of measurement of the sensor. When monetary, get the value from the api."""
|
2022-11-23 10:25:21 +00:00
|
|
|
if self.entity_description.key in (
|
|
|
|
CHARGER_ENERGY_PRICE_KEY,
|
|
|
|
CHARGER_DEPOT_PRICE_KEY,
|
|
|
|
):
|
2022-11-18 16:36:38 +00:00
|
|
|
return cast(str, self.coordinator.data[CHARGER_CURRENCY_KEY])
|
|
|
|
return cast(str, self.entity_description.native_unit_of_measurement)
|