mirror of
https://github.com/home-assistant/core.git
synced 2026-03-01 06:16:29 +00:00
352 lines
12 KiB
Python
352 lines
12 KiB
Python
"""Support for Homevolt sensors."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
from homeassistant.components.sensor import (
|
|
SensorDeviceClass,
|
|
SensorEntity,
|
|
SensorEntityDescription,
|
|
SensorStateClass,
|
|
)
|
|
from homeassistant.const import (
|
|
PERCENTAGE,
|
|
SIGNAL_STRENGTH_DECIBELS,
|
|
EntityCategory,
|
|
UnitOfElectricCurrent,
|
|
UnitOfElectricPotential,
|
|
UnitOfEnergy,
|
|
UnitOfFrequency,
|
|
UnitOfPower,
|
|
UnitOfTemperature,
|
|
)
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.device_registry import DeviceInfo
|
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
|
from homeassistant.helpers.typing import StateType
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
|
|
from .const import DOMAIN, MANUFACTURER
|
|
from .coordinator import HomevoltConfigEntry, HomevoltDataUpdateCoordinator
|
|
|
|
PARALLEL_UPDATES = 0 # Coordinator-based updates
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
SENSORS: tuple[SensorEntityDescription, ...] = (
|
|
SensorEntityDescription(
|
|
key="available_charging_energy",
|
|
translation_key="available_charging_energy",
|
|
device_class=SensorDeviceClass.ENERGY,
|
|
state_class=SensorStateClass.TOTAL,
|
|
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
|
|
),
|
|
SensorEntityDescription(
|
|
key="available_charging_power",
|
|
translation_key="available_charging_power",
|
|
device_class=SensorDeviceClass.POWER,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
native_unit_of_measurement=UnitOfPower.WATT,
|
|
),
|
|
SensorEntityDescription(
|
|
key="available_discharge_energy",
|
|
translation_key="available_discharge_energy",
|
|
device_class=SensorDeviceClass.ENERGY,
|
|
state_class=SensorStateClass.TOTAL,
|
|
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
|
|
),
|
|
SensorEntityDescription(
|
|
key="available_discharge_power",
|
|
translation_key="available_discharge_power",
|
|
device_class=SensorDeviceClass.POWER,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
native_unit_of_measurement=UnitOfPower.WATT,
|
|
),
|
|
SensorEntityDescription(
|
|
key="rssi",
|
|
translation_key="rssi",
|
|
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS,
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
entity_registry_enabled_default=False,
|
|
),
|
|
SensorEntityDescription(
|
|
key="average_rssi",
|
|
translation_key="average_rssi",
|
|
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS,
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
entity_registry_enabled_default=False,
|
|
),
|
|
SensorEntityDescription(
|
|
key="charge_cycles",
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
|
native_unit_of_measurement="cycles",
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
),
|
|
SensorEntityDescription(
|
|
key="energy_exported",
|
|
translation_key="energy_exported",
|
|
device_class=SensorDeviceClass.ENERGY,
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
|
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
|
|
),
|
|
SensorEntityDescription(
|
|
key="energy_imported",
|
|
translation_key="energy_imported",
|
|
device_class=SensorDeviceClass.ENERGY,
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
|
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
|
|
),
|
|
SensorEntityDescription(
|
|
key="frequency",
|
|
device_class=SensorDeviceClass.FREQUENCY,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
native_unit_of_measurement=UnitOfFrequency.HERTZ,
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
),
|
|
SensorEntityDescription(
|
|
key="l1_current",
|
|
translation_key="l1_current",
|
|
device_class=SensorDeviceClass.CURRENT,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
|
|
),
|
|
SensorEntityDescription(
|
|
key="l1_l2_voltage",
|
|
translation_key="l1_l2_voltage",
|
|
device_class=SensorDeviceClass.VOLTAGE,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
|
|
),
|
|
SensorEntityDescription(
|
|
key="l1_power",
|
|
translation_key="l1_power",
|
|
device_class=SensorDeviceClass.POWER,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
native_unit_of_measurement=UnitOfPower.WATT,
|
|
entity_registry_enabled_default=False,
|
|
),
|
|
SensorEntityDescription(
|
|
key="l1_voltage",
|
|
translation_key="l1_voltage",
|
|
device_class=SensorDeviceClass.VOLTAGE,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
|
|
entity_registry_enabled_default=False,
|
|
),
|
|
SensorEntityDescription(
|
|
key="l2_current",
|
|
translation_key="l2_current",
|
|
device_class=SensorDeviceClass.CURRENT,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
|
|
),
|
|
SensorEntityDescription(
|
|
key="l2_l3_voltage",
|
|
translation_key="l2_l3_voltage",
|
|
device_class=SensorDeviceClass.VOLTAGE,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
|
|
),
|
|
SensorEntityDescription(
|
|
key="l2_power",
|
|
translation_key="l2_power",
|
|
device_class=SensorDeviceClass.POWER,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
native_unit_of_measurement=UnitOfPower.WATT,
|
|
entity_registry_enabled_default=False,
|
|
),
|
|
SensorEntityDescription(
|
|
key="l2_voltage",
|
|
translation_key="l2_voltage",
|
|
device_class=SensorDeviceClass.VOLTAGE,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
|
|
entity_registry_enabled_default=False,
|
|
),
|
|
SensorEntityDescription(
|
|
key="l3_current",
|
|
translation_key="l3_current",
|
|
device_class=SensorDeviceClass.CURRENT,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
|
|
),
|
|
SensorEntityDescription(
|
|
key="l3_l1_voltage",
|
|
translation_key="l3_l1_voltage",
|
|
device_class=SensorDeviceClass.VOLTAGE,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
|
|
),
|
|
SensorEntityDescription(
|
|
key="l3_power",
|
|
translation_key="l3_power",
|
|
device_class=SensorDeviceClass.POWER,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
native_unit_of_measurement=UnitOfPower.WATT,
|
|
entity_registry_enabled_default=False,
|
|
),
|
|
SensorEntityDescription(
|
|
key="l3_voltage",
|
|
translation_key="l3_voltage",
|
|
device_class=SensorDeviceClass.VOLTAGE,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
|
|
entity_registry_enabled_default=False,
|
|
),
|
|
SensorEntityDescription(
|
|
key="power",
|
|
device_class=SensorDeviceClass.POWER,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
native_unit_of_measurement=UnitOfPower.WATT,
|
|
entity_registry_enabled_default=False,
|
|
),
|
|
SensorEntityDescription(
|
|
key="schedule_id",
|
|
translation_key="schedule_id",
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
),
|
|
SensorEntityDescription(
|
|
key="schedule_max_discharge",
|
|
translation_key="schedule_max_discharge",
|
|
device_class=SensorDeviceClass.POWER,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
native_unit_of_measurement=UnitOfPower.WATT,
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
),
|
|
SensorEntityDescription(
|
|
key="schedule_max_power",
|
|
translation_key="schedule_max_power",
|
|
device_class=SensorDeviceClass.POWER,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
native_unit_of_measurement=UnitOfPower.WATT,
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
),
|
|
SensorEntityDescription(
|
|
key="schedule_power_setpoint",
|
|
translation_key="schedule_power_setpoint",
|
|
device_class=SensorDeviceClass.POWER,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
native_unit_of_measurement=UnitOfPower.WATT,
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
),
|
|
SensorEntityDescription(
|
|
key="schedule_type",
|
|
translation_key="schedule_type",
|
|
device_class=SensorDeviceClass.ENUM,
|
|
options=[
|
|
"idle",
|
|
"inverter_charge",
|
|
"inverter_discharge",
|
|
"grid_charge",
|
|
"grid_discharge",
|
|
"grid_charge_discharge",
|
|
"frequency_reserve",
|
|
"solar_charge",
|
|
"solar_charge_discharge",
|
|
"full_solar_export",
|
|
],
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
),
|
|
SensorEntityDescription(
|
|
key="state_of_charge",
|
|
device_class=SensorDeviceClass.BATTERY,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
native_unit_of_measurement=PERCENTAGE,
|
|
),
|
|
SensorEntityDescription(
|
|
key="system_temperature",
|
|
translation_key="system_temperature",
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
),
|
|
SensorEntityDescription(
|
|
key="tmax",
|
|
translation_key="tmax",
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
),
|
|
SensorEntityDescription(
|
|
key="tmin",
|
|
translation_key="tmin",
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
),
|
|
)
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
entry: HomevoltConfigEntry,
|
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
|
) -> None:
|
|
"""Set up the Homevolt sensor."""
|
|
coordinator = entry.runtime_data
|
|
entities: list[HomevoltSensor] = []
|
|
sensors_by_key = {sensor.key: sensor for sensor in SENSORS}
|
|
for sensor_key, sensor in coordinator.data.sensors.items():
|
|
if (description := sensors_by_key.get(sensor.type)) is None:
|
|
_LOGGER.warning("Unsupported sensor '%s' found during setup", sensor)
|
|
continue
|
|
entities.append(
|
|
HomevoltSensor(
|
|
description,
|
|
coordinator,
|
|
sensor_key,
|
|
)
|
|
)
|
|
async_add_entities(entities)
|
|
|
|
|
|
class HomevoltSensor(CoordinatorEntity[HomevoltDataUpdateCoordinator], SensorEntity):
|
|
"""Representation of a Homevolt sensor."""
|
|
|
|
entity_description: SensorEntityDescription
|
|
_attr_has_entity_name = True
|
|
|
|
def __init__(
|
|
self,
|
|
description: SensorEntityDescription,
|
|
coordinator: HomevoltDataUpdateCoordinator,
|
|
sensor_key: str,
|
|
) -> None:
|
|
"""Initialize the sensor."""
|
|
super().__init__(coordinator)
|
|
self.entity_description = description
|
|
unique_id = coordinator.data.unique_id
|
|
self._attr_unique_id = f"{unique_id}_{sensor_key}"
|
|
sensor_data = coordinator.data.sensors[sensor_key]
|
|
self._sensor_key = sensor_key
|
|
|
|
device_metadata = coordinator.data.device_metadata.get(
|
|
sensor_data.device_identifier
|
|
)
|
|
self._attr_device_info = DeviceInfo(
|
|
identifiers={(DOMAIN, f"{unique_id}_{sensor_data.device_identifier}")},
|
|
configuration_url=coordinator.client.base_url,
|
|
manufacturer=MANUFACTURER,
|
|
model=device_metadata.model if device_metadata else None,
|
|
name=device_metadata.name if device_metadata else None,
|
|
)
|
|
|
|
@property
|
|
def available(self) -> bool:
|
|
"""Return if entity is available."""
|
|
return super().available and self._sensor_key in self.coordinator.data.sensors
|
|
|
|
@property
|
|
def native_value(self) -> StateType:
|
|
"""Return the native value of the sensor."""
|
|
return self.coordinator.data.sensors[self._sensor_key].value
|