"""Support for Homevolt sensors.""" 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.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.typing import StateType from .coordinator import HomevoltConfigEntry, HomevoltDataUpdateCoordinator from .entity import HomevoltEntity 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.KILO_WATT_HOUR, ), SensorEntityDescription( key="energy_imported", translation_key="energy_imported", device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL_INCREASING, native_unit_of_measurement=UnitOfEnergy.KILO_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(HomevoltEntity, SensorEntity): """Representation of a Homevolt sensor.""" entity_description: SensorEntityDescription def __init__( self, description: SensorEntityDescription, coordinator: HomevoltDataUpdateCoordinator, sensor_key: str, ) -> None: """Initialize the sensor.""" sensor_data = coordinator.data.sensors[sensor_key] super().__init__(coordinator, sensor_data.device_identifier) self.entity_description = description self._attr_unique_id = f"{coordinator.data.unique_id}_{sensor_key}" self._sensor_key = sensor_key @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