mirror of
https://github.com/home-assistant/core.git
synced 2026-09-07 14:01:20 +01:00
198 lines
6.7 KiB
Python
198 lines
6.7 KiB
Python
"""Sensor platform for Besen."""
|
|
|
|
from collections.abc import Callable
|
|
from dataclasses import dataclass
|
|
from typing import override
|
|
|
|
from besen.models import BesenData
|
|
|
|
from homeassistant.components.sensor import (
|
|
SensorDeviceClass,
|
|
SensorEntity,
|
|
SensorEntityDescription,
|
|
SensorStateClass,
|
|
StateType,
|
|
)
|
|
from homeassistant.const import (
|
|
EntityCategory,
|
|
UnitOfElectricCurrent,
|
|
UnitOfElectricPotential,
|
|
UnitOfEnergy,
|
|
UnitOfPower,
|
|
UnitOfTemperature,
|
|
)
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
|
|
|
from . import BesenConfigEntry
|
|
from .coordinator import BesenCoordinator
|
|
from .entity import BesenEntity
|
|
|
|
PARALLEL_UPDATES = 0
|
|
|
|
|
|
@dataclass(frozen=True, kw_only=True)
|
|
class BesenSensorEntityDescription(SensorEntityDescription):
|
|
"""Describe a Besen sensor entity."""
|
|
|
|
value_fn: Callable[[BesenData], float | int | None]
|
|
three_phase_only: bool = False
|
|
|
|
|
|
SENSOR_DESCRIPTIONS: tuple[BesenSensorEntityDescription, ...] = (
|
|
BesenSensorEntityDescription(
|
|
key="charging_power",
|
|
translation_key="charging_power",
|
|
device_class=SensorDeviceClass.POWER,
|
|
native_unit_of_measurement=UnitOfPower.WATT,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
value_fn=lambda data: data.charge.power,
|
|
),
|
|
BesenSensorEntityDescription(
|
|
key="total_energy",
|
|
translation_key="total_energy",
|
|
device_class=SensorDeviceClass.ENERGY,
|
|
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
|
suggested_display_precision=2,
|
|
value_fn=lambda data: data.charge.total_energy,
|
|
),
|
|
BesenSensorEntityDescription(
|
|
key="session_energy",
|
|
translation_key="session_energy",
|
|
device_class=SensorDeviceClass.ENERGY,
|
|
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
|
suggested_display_precision=2,
|
|
value_fn=lambda data: data.charge.session_energy,
|
|
),
|
|
BesenSensorEntityDescription(
|
|
key="internal_temperature",
|
|
translation_key="internal_temperature",
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
|
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
suggested_display_precision=1,
|
|
value_fn=lambda data: data.charge.inner_temp_c,
|
|
),
|
|
BesenSensorEntityDescription(
|
|
key="external_temperature",
|
|
translation_key="external_temperature",
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
entity_registry_enabled_default=False,
|
|
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
suggested_display_precision=1,
|
|
value_fn=lambda data: data.charge.outer_temp,
|
|
),
|
|
BesenSensorEntityDescription(
|
|
key="l1_voltage",
|
|
translation_key="l1_voltage",
|
|
device_class=SensorDeviceClass.VOLTAGE,
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
entity_registry_enabled_default=False,
|
|
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
suggested_display_precision=1,
|
|
value_fn=lambda data: data.charge.l1_voltage,
|
|
),
|
|
BesenSensorEntityDescription(
|
|
key="l1_current",
|
|
translation_key="l1_current",
|
|
device_class=SensorDeviceClass.CURRENT,
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
entity_registry_enabled_default=False,
|
|
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
suggested_display_precision=1,
|
|
value_fn=lambda data: data.charge.l1_amperage,
|
|
),
|
|
BesenSensorEntityDescription(
|
|
key="l2_voltage",
|
|
translation_key="l2_voltage",
|
|
device_class=SensorDeviceClass.VOLTAGE,
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
entity_registry_enabled_default=False,
|
|
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
suggested_display_precision=1,
|
|
three_phase_only=True,
|
|
value_fn=lambda data: data.charge.l2_voltage,
|
|
),
|
|
BesenSensorEntityDescription(
|
|
key="l2_current",
|
|
translation_key="l2_current",
|
|
device_class=SensorDeviceClass.CURRENT,
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
entity_registry_enabled_default=False,
|
|
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
suggested_display_precision=1,
|
|
three_phase_only=True,
|
|
value_fn=lambda data: data.charge.l2_amperage,
|
|
),
|
|
BesenSensorEntityDescription(
|
|
key="l3_voltage",
|
|
translation_key="l3_voltage",
|
|
device_class=SensorDeviceClass.VOLTAGE,
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
entity_registry_enabled_default=False,
|
|
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
suggested_display_precision=1,
|
|
three_phase_only=True,
|
|
value_fn=lambda data: data.charge.l3_voltage,
|
|
),
|
|
BesenSensorEntityDescription(
|
|
key="l3_current",
|
|
translation_key="l3_current",
|
|
device_class=SensorDeviceClass.CURRENT,
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
entity_registry_enabled_default=False,
|
|
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
suggested_display_precision=1,
|
|
three_phase_only=True,
|
|
value_fn=lambda data: data.charge.l3_amperage,
|
|
),
|
|
)
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
entry: BesenConfigEntry,
|
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
|
) -> None:
|
|
"""Set up Besen sensors."""
|
|
|
|
coordinator = entry.runtime_data
|
|
async_add_entities(
|
|
BesenSensor(coordinator, description)
|
|
for description in SENSOR_DESCRIPTIONS
|
|
if not description.three_phase_only or coordinator.data.info.phases == 3
|
|
)
|
|
|
|
|
|
class BesenSensor(BesenEntity, SensorEntity):
|
|
"""Representation of a Besen sensor."""
|
|
|
|
entity_description: BesenSensorEntityDescription
|
|
|
|
def __init__(
|
|
self,
|
|
coordinator: BesenCoordinator,
|
|
description: BesenSensorEntityDescription,
|
|
) -> None:
|
|
"""Initialize a Besen sensor."""
|
|
|
|
super().__init__(coordinator, description.key)
|
|
self.entity_description = description
|
|
|
|
@property
|
|
@override
|
|
def native_value(self) -> StateType:
|
|
"""Return the sensor value."""
|
|
|
|
return self.entity_description.value_fn(self.coordinator.data)
|