Imeon-Energy b51bb668c6
Add imeon inverter integration (#130958)
* Initial commit prototype with empty inverters

* Use modern methods and global variable for character strings

* Platform that get the value of the meter in an entity

* Add check if inverter already configured

* Add tests for config_flow

* Update "imeon_inverter_api" in manifest.json

* Update "imeon_inverter_api" in requirements_all.txt

* Remove async_setup, clean comments, use of const PLATFORM

* Use of global variable and remove configuration of device name

* Use of entry.data instead of user_input variable

* Remove services.yaml

* No quality scale

* Use of common string

* Add sensors, use of EntityDescription and '_attr_device_info'

* Remove name from config_flow tests

* Use sentence case and change integration from hub to device

* Check connection before add platform in config_flow

* Use of _async_setup and minor changes

* Improve sensor description

* Add quality_scale.yaml

* Update the quality_scale.json

* Add tests for host invalid, route invalid, exception and invalid auth

* Type more precisely 'DataUpdateCoordinator'

* Don't use 'self.data' directly in coordinator and minor corrections

* Complete full quality_scale.yaml

* Use of fixtures in the tests

* Add snapshot tests for sensors

* Refactor the try except and use serial as unique id

* Change API version

* Add test for sensor

* Mock the api to generate the snapshot

* New type for async_add_entries

* Except timeout error for get_serial

* Add test for get_serial timeout error

* Move store data out of the try

* Use sentence case

* Use of fixtures

* Use separates fixtures

* Mock the api

* Put sensors fake data in json fixture file

* Use of a const interval, remove except timeout, enhance lisibility

* Try to use same fixture in test_config_flow

* Try use same fixture for all mock of inverter

* Modify the fixture in the context manager, correct the tests

* Fixture return mock.__aenter__ directly

* Adjust code clarity

* Bring all tests to either ABORT or CREATE_ENTRY

* Make the try except more concise

* Synthetize exception tests into one

* Add code clarity

* Nitpick with the tests

* Use unique id sensor

* Log an error on unknown error

* Remove useless comments, disable always_update and better use of timeout

* Adjust units, set the model and software version

* Set full name for Battery SOC and use ip instead of url

* Use of host instead of IP

* Fix the unit of economy factor

* Reduce mornitoring data display precision and update snapshots

* Remove unused variable HUBs

* Fix device info

* Set address label 'Host or IP'

* Fix the config_flow tests

* Re evaluate the quality_scale

* Use of 'host' instead of 'address'

* Make inverter discoverable by ssdp

* Add test ssdp configuration already exist

* Add exemption in quality scale

* Test abort ssdp if serial is unknown

* Handle update error

* Raise other exceptions

* Handle ClientError and ValueError from the api

* Update homeassistant/components/imeon_inverter/quality_scale.yaml

---------

Co-authored-by: Franck Nijhof <git@frenck.dev>
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
Co-authored-by: Josef Zweck <josef@zweck.dev>
2025-04-10 08:25:35 +02:00

465 lines
17 KiB
Python

"""Imeon inverter sensor support."""
import logging
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
PERCENTAGE,
EntityCategory,
UnitOfElectricCurrent,
UnitOfElectricPotential,
UnitOfEnergy,
UnitOfFrequency,
UnitOfPower,
UnitOfTemperature,
UnitOfTime,
)
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
from .coordinator import InverterCoordinator
type InverterConfigEntry = ConfigEntry[InverterCoordinator]
_LOGGER = logging.getLogger(__name__)
ENTITY_DESCRIPTIONS = (
# Battery
SensorEntityDescription(
key="battery_autonomy",
translation_key="battery_autonomy",
native_unit_of_measurement=UnitOfTime.HOURS,
device_class=SensorDeviceClass.DURATION,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="battery_charge_time",
translation_key="battery_charge_time",
native_unit_of_measurement=UnitOfTime.HOURS,
device_class=SensorDeviceClass.DURATION,
state_class=SensorStateClass.TOTAL,
),
SensorEntityDescription(
key="battery_power",
translation_key="battery_power",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="battery_soc",
translation_key="battery_soc",
native_unit_of_measurement=PERCENTAGE,
device_class=SensorDeviceClass.BATTERY,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="battery_stored",
translation_key="battery_stored",
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
device_class=SensorDeviceClass.ENERGY_STORAGE,
state_class=SensorStateClass.TOTAL,
),
# Grid
SensorEntityDescription(
key="grid_current_l1",
translation_key="grid_current_l1",
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="grid_current_l2",
translation_key="grid_current_l2",
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="grid_current_l3",
translation_key="grid_current_l3",
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="grid_frequency",
translation_key="grid_frequency",
native_unit_of_measurement=UnitOfFrequency.HERTZ,
device_class=SensorDeviceClass.FREQUENCY,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="grid_voltage_l1",
translation_key="grid_voltage_l1",
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
device_class=SensorDeviceClass.VOLTAGE,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="grid_voltage_l2",
translation_key="grid_voltage_l2",
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
device_class=SensorDeviceClass.VOLTAGE,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="grid_voltage_l3",
translation_key="grid_voltage_l3",
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
device_class=SensorDeviceClass.VOLTAGE,
state_class=SensorStateClass.MEASUREMENT,
),
# AC Input
SensorEntityDescription(
key="input_power_l1",
translation_key="input_power_l1",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="input_power_l2",
translation_key="input_power_l2",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="input_power_l3",
translation_key="input_power_l3",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="input_power_total",
translation_key="input_power_total",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
),
# Inverter settings
SensorEntityDescription(
key="inverter_charging_current_limit",
translation_key="inverter_charging_current_limit",
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="inverter_injection_power_limit",
translation_key="inverter_injection_power_limit",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
),
# Meter
SensorEntityDescription(
key="meter_power",
translation_key="meter_power",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="meter_power_protocol",
translation_key="meter_power_protocol",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
),
# AC Output
SensorEntityDescription(
key="output_current_l1",
translation_key="output_current_l1",
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="output_current_l2",
translation_key="output_current_l2",
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="output_current_l3",
translation_key="output_current_l3",
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="output_frequency",
translation_key="output_frequency",
native_unit_of_measurement=UnitOfFrequency.HERTZ,
device_class=SensorDeviceClass.FREQUENCY,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="output_power_l1",
translation_key="output_power_l1",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="output_power_l2",
translation_key="output_power_l2",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="output_power_l3",
translation_key="output_power_l3",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="output_power_total",
translation_key="output_power_total",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="output_voltage_l1",
translation_key="output_voltage_l1",
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
device_class=SensorDeviceClass.VOLTAGE,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="output_voltage_l2",
translation_key="output_voltage_l2",
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
device_class=SensorDeviceClass.VOLTAGE,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="output_voltage_l3",
translation_key="output_voltage_l3",
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
device_class=SensorDeviceClass.VOLTAGE,
state_class=SensorStateClass.MEASUREMENT,
),
# Solar Panel
SensorEntityDescription(
key="pv_consumed",
translation_key="pv_consumed",
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL,
),
SensorEntityDescription(
key="pv_injected",
translation_key="pv_injected",
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL,
),
SensorEntityDescription(
key="pv_power_1",
translation_key="pv_power_1",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="pv_power_2",
translation_key="pv_power_2",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="pv_power_total",
translation_key="pv_power_total",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
),
# Temperature
SensorEntityDescription(
key="temp_air_temperature",
translation_key="temp_air_temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="temp_component_temperature",
translation_key="temp_component_temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
),
# Monitoring (data over the last 24 hours)
SensorEntityDescription(
key="monitoring_building_consumption",
translation_key="monitoring_building_consumption",
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL,
suggested_display_precision=2,
),
SensorEntityDescription(
key="monitoring_economy_factor",
translation_key="monitoring_economy_factor",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL,
suggested_display_precision=2,
),
SensorEntityDescription(
key="monitoring_grid_consumption",
translation_key="monitoring_grid_consumption",
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL,
suggested_display_precision=2,
),
SensorEntityDescription(
key="monitoring_grid_injection",
translation_key="monitoring_grid_injection",
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL,
suggested_display_precision=2,
),
SensorEntityDescription(
key="monitoring_grid_power_flow",
translation_key="monitoring_grid_power_flow",
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL,
suggested_display_precision=2,
),
SensorEntityDescription(
key="monitoring_self_consumption",
translation_key="monitoring_self_consumption",
native_unit_of_measurement=PERCENTAGE,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=2,
),
SensorEntityDescription(
key="monitoring_self_sufficiency",
translation_key="monitoring_self_sufficiency",
native_unit_of_measurement=PERCENTAGE,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=2,
),
SensorEntityDescription(
key="monitoring_solar_production",
translation_key="monitoring_solar_production",
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL,
suggested_display_precision=2,
),
# Monitoring (instant minute data)
SensorEntityDescription(
key="monitoring_minute_building_consumption",
translation_key="monitoring_minute_building_consumption",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=2,
),
SensorEntityDescription(
key="monitoring_minute_grid_consumption",
translation_key="monitoring_minute_grid_consumption",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=2,
),
SensorEntityDescription(
key="monitoring_minute_grid_injection",
translation_key="monitoring_minute_grid_injection",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=2,
),
SensorEntityDescription(
key="monitoring_minute_grid_power_flow",
translation_key="monitoring_minute_grid_power_flow",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=2,
),
SensorEntityDescription(
key="monitoring_minute_solar_production",
translation_key="monitoring_minute_solar_production",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=2,
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: InverterConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Create each sensor for a given config entry."""
coordinator = entry.runtime_data
# Init sensor entities
async_add_entities(
InverterSensor(coordinator, entry, description)
for description in ENTITY_DESCRIPTIONS
)
class InverterSensor(CoordinatorEntity[InverterCoordinator], SensorEntity):
"""A sensor that returns numerical values with units."""
_attr_has_entity_name = True
_attr_entity_category = EntityCategory.DIAGNOSTIC
def __init__(
self,
coordinator: InverterCoordinator,
entry: InverterConfigEntry,
description: SensorEntityDescription,
) -> None:
"""Pass coordinator to CoordinatorEntity."""
super().__init__(coordinator)
self.entity_description = description
self._inverter = coordinator.api.inverter
self.data_key = description.key
assert entry.unique_id
self._attr_unique_id = f"{entry.unique_id}_{self.data_key}"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, entry.unique_id)},
name="Imeon inverter",
manufacturer="Imeon Energy",
model=self._inverter.get("inverter"),
sw_version=self._inverter.get("software"),
)
@property
def native_value(self) -> StateType | None:
"""Value of the sensor."""
return self.coordinator.data.get(self.data_key)