2019-08-19 12:29:26 +00:00
|
|
|
"""Support for KEBA charging station sensors."""
|
2021-08-18 14:45:30 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-08-12 20:40:56 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
DEVICE_CLASS_CURRENT,
|
|
|
|
DEVICE_CLASS_ENERGY,
|
2020-05-17 13:58:31 +00:00
|
|
|
DEVICE_CLASS_POWER,
|
2021-08-12 20:40:56 +00:00
|
|
|
STATE_CLASS_MEASUREMENT,
|
2021-08-18 14:45:30 +00:00
|
|
|
STATE_CLASS_TOTAL_INCREASING,
|
2021-08-12 20:40:56 +00:00
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
)
|
|
|
|
from homeassistant.const import (
|
2021-07-20 18:06:23 +00:00
|
|
|
ELECTRIC_CURRENT_AMPERE,
|
2020-05-17 13:58:31 +00:00
|
|
|
ENERGY_KILO_WATT_HOUR,
|
2021-08-12 20:40:56 +00:00
|
|
|
POWER_KILO_WATT,
|
2020-05-17 13:58:31 +00:00
|
|
|
)
|
2019-08-19 12:29:26 +00:00
|
|
|
|
2021-08-21 12:17:19 +00:00
|
|
|
from . import DOMAIN, KebaHandler
|
2019-08-19 12:29:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
|
|
|
"""Set up the KEBA charging station platform."""
|
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
keba = hass.data[DOMAIN]
|
|
|
|
|
|
|
|
sensors = [
|
2020-05-17 13:58:31 +00:00
|
|
|
KebaSensor(
|
|
|
|
keba,
|
|
|
|
"max_current",
|
2021-08-12 20:40:56 +00:00
|
|
|
SensorEntityDescription(
|
|
|
|
key="Curr user",
|
|
|
|
name="Max Current",
|
|
|
|
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
|
|
|
|
device_class=DEVICE_CLASS_CURRENT,
|
|
|
|
),
|
2020-05-17 13:58:31 +00:00
|
|
|
),
|
2019-08-19 12:29:26 +00:00
|
|
|
KebaSensor(
|
2019-12-22 18:46:53 +00:00
|
|
|
keba,
|
|
|
|
"energy_target",
|
2021-08-12 20:40:56 +00:00
|
|
|
SensorEntityDescription(
|
|
|
|
key="Setenergy",
|
|
|
|
name="Energy Target",
|
|
|
|
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
|
|
|
device_class=DEVICE_CLASS_ENERGY,
|
|
|
|
),
|
2019-08-19 12:29:26 +00:00
|
|
|
),
|
|
|
|
KebaSensor(
|
2019-12-22 18:46:53 +00:00
|
|
|
keba,
|
|
|
|
"charging_power",
|
2021-08-12 20:40:56 +00:00
|
|
|
SensorEntityDescription(
|
|
|
|
key="P",
|
|
|
|
name="Charging Power",
|
|
|
|
native_unit_of_measurement=POWER_KILO_WATT,
|
|
|
|
device_class=DEVICE_CLASS_POWER,
|
|
|
|
state_class=STATE_CLASS_MEASUREMENT,
|
|
|
|
),
|
2019-12-22 18:46:53 +00:00
|
|
|
),
|
|
|
|
KebaSensor(
|
|
|
|
keba,
|
|
|
|
"session_energy",
|
2021-08-12 20:40:56 +00:00
|
|
|
SensorEntityDescription(
|
|
|
|
key="E pres",
|
|
|
|
name="Session Energy",
|
|
|
|
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
|
|
|
device_class=DEVICE_CLASS_ENERGY,
|
|
|
|
),
|
2019-12-22 18:46:53 +00:00
|
|
|
),
|
|
|
|
KebaSensor(
|
|
|
|
keba,
|
|
|
|
"total_energy",
|
2021-08-12 20:40:56 +00:00
|
|
|
SensorEntityDescription(
|
|
|
|
key="E total",
|
|
|
|
name="Total Energy",
|
|
|
|
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
|
|
|
device_class=DEVICE_CLASS_ENERGY,
|
2021-08-18 14:45:30 +00:00
|
|
|
state_class=STATE_CLASS_TOTAL_INCREASING,
|
2021-08-12 20:40:56 +00:00
|
|
|
),
|
2019-08-19 12:29:26 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
async_add_entities(sensors)
|
|
|
|
|
|
|
|
|
2021-03-22 18:59:03 +00:00
|
|
|
class KebaSensor(SensorEntity):
|
2019-08-19 12:29:26 +00:00
|
|
|
"""The entity class for KEBA charging stations sensors."""
|
|
|
|
|
2021-08-12 20:40:56 +00:00
|
|
|
_attr_should_poll = False
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2021-08-21 12:17:19 +00:00
|
|
|
keba: KebaHandler,
|
|
|
|
entity_type: str,
|
2021-08-12 20:40:56 +00:00
|
|
|
description: SensorEntityDescription,
|
2021-08-21 12:17:19 +00:00
|
|
|
) -> None:
|
2019-08-19 12:29:26 +00:00
|
|
|
"""Initialize the KEBA Sensor."""
|
|
|
|
self._keba = keba
|
2021-08-12 20:40:56 +00:00
|
|
|
self.entity_description = description
|
2019-12-22 18:46:53 +00:00
|
|
|
|
2021-08-12 20:40:56 +00:00
|
|
|
self._attr_name = f"{keba.device_name} {description.name}"
|
|
|
|
self._attr_unique_id = f"{keba.device_id}_{entity_type}"
|
2019-08-19 12:29:26 +00:00
|
|
|
|
2021-08-12 20:40:56 +00:00
|
|
|
self._attributes: dict[str, str] = {}
|
2019-08-19 12:29:26 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-11 19:11:25 +00:00
|
|
|
def extra_state_attributes(self):
|
2019-08-19 12:29:26 +00:00
|
|
|
"""Return the state attributes of the binary sensor."""
|
|
|
|
return self._attributes
|
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Get latest cached states from the device."""
|
2021-08-12 20:40:56 +00:00
|
|
|
self._attr_native_value = self._keba.get_value(self.entity_description.key)
|
2019-08-19 12:29:26 +00:00
|
|
|
|
2021-08-12 20:40:56 +00:00
|
|
|
if self.entity_description.key == "P":
|
2019-08-19 12:29:26 +00:00
|
|
|
self._attributes["power_factor"] = self._keba.get_value("PF")
|
|
|
|
self._attributes["voltage_u1"] = str(self._keba.get_value("U1"))
|
|
|
|
self._attributes["voltage_u2"] = str(self._keba.get_value("U2"))
|
|
|
|
self._attributes["voltage_u3"] = str(self._keba.get_value("U3"))
|
|
|
|
self._attributes["current_i1"] = str(self._keba.get_value("I1"))
|
|
|
|
self._attributes["current_i2"] = str(self._keba.get_value("I2"))
|
|
|
|
self._attributes["current_i3"] = str(self._keba.get_value("I3"))
|
2021-08-12 20:40:56 +00:00
|
|
|
elif self.entity_description.key == "Curr user":
|
2019-08-19 12:29:26 +00:00
|
|
|
self._attributes["max_current_hardware"] = self._keba.get_value("Curr HW")
|
|
|
|
|
|
|
|
def update_callback(self):
|
|
|
|
"""Schedule a state update."""
|
|
|
|
self.async_schedule_update_ha_state(True)
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Add update callback after being added to hass."""
|
|
|
|
self._keba.add_update_listener(self.update_callback)
|