Files

112 lines
3.6 KiB
Python

from __future__ import annotations
from dataclasses import dataclass
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfElectricCurrent, UnitOfPower
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN
from .coordinator import EaseeSolarCoordinator
@dataclass(frozen=True, kw_only=True)
class EaseeSolarSensorDescription(SensorEntityDescription):
data_key: str
SENSOR_DESCRIPTIONS: tuple[EaseeSolarSensorDescription, ...] = (
EaseeSolarSensorDescription(
key="solar_production",
data_key="solar_production_w",
name="Solar Production",
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfPower.WATT,
),
EaseeSolarSensorDescription(
key="house_load",
data_key="house_load_w",
name="House Load",
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfPower.WATT,
),
EaseeSolarSensorDescription(
key="ev_charging",
data_key="ev_charging_w",
name="EV Charging Power",
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfPower.WATT,
),
EaseeSolarSensorDescription(
key="solar_excess",
data_key="solar_excess_w",
name="Solar Excess Power",
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfPower.WATT,
),
EaseeSolarSensorDescription(
key="target_current",
data_key="target_current_a",
name="Target Charging Current",
device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
),
EaseeSolarSensorDescription(
key="charger_current",
data_key="charger_current_a",
name="Charger Current",
device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
),
EaseeSolarSensorDescription(
key="charger_status",
data_key="charger_status",
name="Charger Status",
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
coordinator: EaseeSolarCoordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities(
EaseeSolarSensor(coordinator, description)
for description in SENSOR_DESCRIPTIONS
)
class EaseeSolarSensor(CoordinatorEntity[EaseeSolarCoordinator], SensorEntity):
entity_description: EaseeSolarSensorDescription
_attr_has_entity_name = True
def __init__(
self,
coordinator: EaseeSolarCoordinator,
description: EaseeSolarSensorDescription,
) -> None:
super().__init__(coordinator)
self.entity_description = description
self._attr_unique_id = f"{coordinator.charger_id}_{description.key}"
self.entity_id = f"sensor.esc_{coordinator.charger_id.lower()}_{description.key}"
@property
def native_value(self):
return self.coordinator.data.get(self.entity_description.data_key)