From fc1b9f81914e5faaf831c16842621564a7c55fbc Mon Sep 17 00:00:00 2001 From: titidnh Date: Sun, 10 Mar 2024 10:38:23 +0100 Subject: [PATCH 1/7] Update sensor.py --- custom_components/huawei_solar/sensor.py | 27 +++++++++++++++--------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/custom_components/huawei_solar/sensor.py b/custom_components/huawei_solar/sensor.py index c0b981a..0b37ec2 100644 --- a/custom_components/huawei_solar/sensor.py +++ b/custom_components/huawei_solar/sensor.py @@ -36,6 +36,7 @@ DEFAULT_RECONNECT_INTERVAL = 30 CONF_OPTIMIZERS = "optimizers" +CONF_NAME = "name" CONF_BATTERY = "battery" CONF_SLAVE = "slave" CONF_PORT = "port" @@ -78,7 +79,8 @@ "phase_B_current", "phase_C_current", "power_meter_active_power", - "input_power", + "input_power", # Total input power + "active_power", "grid_A_voltage", "grid_B_voltage", "grid_C_voltage", @@ -87,6 +89,8 @@ "active_grid_C_current", "active_grid_power_factor", "active_grid_frequency", + "daily_yield_energy", + "accumulated_yield_energy", "grid_exported_energy", "grid_accumulated_energy", "active_grid_A_B_voltage", @@ -155,6 +159,7 @@ vol.Optional(CONF_BATTERY, default=False): cv.boolean, vol.Optional(CONF_SLAVE, default=0): int, vol.Optional(CONF_PORT, default=502): int, + vol.Optional(CONF_NAME, default=''): cv.string, } ) @@ -166,14 +171,16 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= static_attributes = {} try: - inverter = AsyncHuaweiSolar( - host=config[CONF_HOST], port=config[CONF_PORT], loop=hass.loop, slave=config[CONF_SLAVE] + inverter = await AsyncHuaweiSolar.create( + host=config[CONF_HOST], port=config[CONF_PORT], slave=config[CONF_SLAVE] ) for register in STATIC_ATTR_LIST: static_attributes[register] = (await inverter.get(register)).value _LOGGER.debug("get sensor static attribute: %s", register) await asyncio.sleep(DEFAULT_COOLDOWN_INTERVAL) + if config[CONF_NAME]: + static_attributes["model_name"] = config[CONF_NAME] register = ATTR_GRID_CODE tmp = (await inverter.get(register)).value @@ -216,7 +223,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= device_class=DEVICE_CLASS_ENERGY, parent_sensor=huawei_solar_sensor, register=ATTR_DAILY_YIELD, - name_prefix="daily_yield", + name_prefix=static_attributes["model_name"] + "_daily_yield", state_class=STATE_CLASS_MEASUREMENT, ), HuaweiSolarEntitySensor( @@ -226,7 +233,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= device_class=DEVICE_CLASS_ENERGY, parent_sensor=huawei_solar_sensor, register=ATTR_ACCUMULATED_YIELD, - name_prefix="total_yield", + name_prefix=static_attributes["model_name"] +"_total_yield", ), HuaweiSolarEntitySensor( inverter=inverter, @@ -235,7 +242,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= device_class=DEVICE_CLASS_ENERGY, parent_sensor=huawei_solar_sensor, register=ATTR_GRID_EXPORTED, - name_prefix="grid_exported", + name_prefix=static_attributes["model_name"] + "_grid_exported", ), HuaweiSolarEntitySensor( inverter=inverter, @@ -244,7 +251,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= device_class=DEVICE_CLASS_ENERGY, parent_sensor=huawei_solar_sensor, register=ATTR_GRID_ACCUMULATED, - name_prefix="grid_accumulated", + name_prefix=static_attributes["model_name"] + "_grid_accumulated", ), ] @@ -297,7 +304,7 @@ def __init__( self.sensor_states = {} self._attributes = static_attributes self._name = ( - self._attributes["model_name"] + "_" + self._attributes["serial_number"] + self._attributes["model_name"] # + "_" + self._attributes["serial_number"] ) self._unique_id = self._name self._pv_strings_voltage = [None] * self._attributes[ATTR_NB_PV_STRINGS] @@ -368,7 +375,7 @@ async def async_update(self): for register in DYNAMIC_ATTR_LIST: try: self._attributes[register] = (await self._inverter.get(register)).value - _LOGGER.debug("get register: %s", register) + _LOGGER.debug("get register: %s = %s", register, self._attributes[register]) await asyncio.sleep(DEFAULT_COOLDOWN_INTERVAL) except (ReadException, ConnectionException) as ex: _LOGGER.error("could not get register '%s': %s", register, ex) @@ -459,7 +466,7 @@ def __init__( self._register = register if name_prefix: self._name = ( - name_prefix + "_" + self._parent_sensor._attributes[ATTR_SERIAL_NUMBER] + name_prefix # + "_" + self._parent_sensor._attributes[ATTR_SERIAL_NUMBER] ) else: self._name = ( From 6f87d065e45122a9af5a2ca2e52e5ee4ebe75d5d Mon Sep 17 00:00:00 2001 From: titidnh Date: Sun, 10 Mar 2024 10:39:22 +0100 Subject: [PATCH 2/7] Update manifest.json --- custom_components/huawei_solar/manifest.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/custom_components/huawei_solar/manifest.json b/custom_components/huawei_solar/manifest.json index 1107173..830e8aa 100644 --- a/custom_components/huawei_solar/manifest.json +++ b/custom_components/huawei_solar/manifest.json @@ -1,9 +1,9 @@ { "domain": "huawei_solar", "name": "huawei_solar", - "documentation": "https://github.com/Emilv2/huawei_solar", + "documentation": "https://github.com/titidnh/huawei_solar", "dependencies": [], "codeowners": ["Emilv2"], - "requirements": ["huawei-solar>=2.0.0,<=2.2.6"], + "requirements": ["huawei-solar>=2.0.0,<=2.2.9"], "version": "1.2.4" } From 5ace094a7bbacf238085548b3c2aedde41386f81 Mon Sep 17 00:00:00 2001 From: titidnh Date: Sun, 10 Mar 2024 13:31:02 +0100 Subject: [PATCH 3/7] Update sensor.py --- custom_components/huawei_solar/sensor.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/custom_components/huawei_solar/sensor.py b/custom_components/huawei_solar/sensor.py index 0b37ec2..e287c12 100644 --- a/custom_components/huawei_solar/sensor.py +++ b/custom_components/huawei_solar/sensor.py @@ -10,6 +10,8 @@ PLATFORM_SCHEMA, STATE_CLASS_MEASUREMENT, STATE_CLASS_TOTAL_INCREASING, + SensorStateClass, + SensorDeviceClass, ) from homeassistant.const import ( CONF_HOST, @@ -19,6 +21,7 @@ DEVICE_CLASS_VOLTAGE, ENERGY_KILO_WATT_HOUR, POWER_WATT, + UnitOfEnergy, ) from homeassistant.helpers.entity import Entity from homeassistant.util.dt import utc_from_timestamp @@ -224,7 +227,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= parent_sensor=huawei_solar_sensor, register=ATTR_DAILY_YIELD, name_prefix=static_attributes["model_name"] + "_daily_yield", - state_class=STATE_CLASS_MEASUREMENT, + state_class=SensorStateClass.MEASUREMENT, ), HuaweiSolarEntitySensor( inverter=inverter, @@ -455,7 +458,7 @@ def __init__( parent_sensor, register, name_prefix=None, - state_class=STATE_CLASS_TOTAL_INCREASING, + state_class=SensorStateClass.TOTAL_INCREASING, ): self._inverter = inverter self._hidden = False From bd3d68a75238892454167b9298f1e1021c85d753 Mon Sep 17 00:00:00 2001 From: titidnh Date: Sun, 10 Mar 2024 13:31:31 +0100 Subject: [PATCH 4/7] Update sensor.py --- custom_components/huawei_solar/sensor.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/custom_components/huawei_solar/sensor.py b/custom_components/huawei_solar/sensor.py index e287c12..e06cc19 100644 --- a/custom_components/huawei_solar/sensor.py +++ b/custom_components/huawei_solar/sensor.py @@ -8,8 +8,6 @@ ATTR_LAST_RESET, ATTR_STATE_CLASS, PLATFORM_SCHEMA, - STATE_CLASS_MEASUREMENT, - STATE_CLASS_TOTAL_INCREASING, SensorStateClass, SensorDeviceClass, ) From 2a296e639ac5475867f9a0f5215734a51d93ccf7 Mon Sep 17 00:00:00 2001 From: titidnh Date: Sun, 10 Mar 2024 14:00:48 +0100 Subject: [PATCH 5/7] Update sensor.py --- custom_components/huawei_solar/sensor.py | 38 ++++++++++-------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/custom_components/huawei_solar/sensor.py b/custom_components/huawei_solar/sensor.py index e06cc19..a19f960 100644 --- a/custom_components/huawei_solar/sensor.py +++ b/custom_components/huawei_solar/sensor.py @@ -13,12 +13,6 @@ ) from homeassistant.const import ( CONF_HOST, - DEVICE_CLASS_CURRENT, - DEVICE_CLASS_ENERGY, - DEVICE_CLASS_POWER, - DEVICE_CLASS_VOLTAGE, - ENERGY_KILO_WATT_HOUR, - POWER_WATT, UnitOfEnergy, ) from homeassistant.helpers.entity import Entity @@ -219,9 +213,9 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= huawei_solar_sensor, HuaweiSolarDailyYieldSensor( inverter=inverter, - unit=ENERGY_KILO_WATT_HOUR, + unit=UnitOfEnergy.KILO_WATT_HOUR, icon="mdi:solar-power", - device_class=DEVICE_CLASS_ENERGY, + device_class=SensorDeviceClass.ENERGY, parent_sensor=huawei_solar_sensor, register=ATTR_DAILY_YIELD, name_prefix=static_attributes["model_name"] + "_daily_yield", @@ -229,27 +223,27 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= ), HuaweiSolarEntitySensor( inverter=inverter, - unit=ENERGY_KILO_WATT_HOUR, + unit=UnitOfEnergy.KILO_WATT_HOUR, icon="mdi:solar-power", - device_class=DEVICE_CLASS_ENERGY, + device_class=SensorDeviceClass.ENERGY, parent_sensor=huawei_solar_sensor, register=ATTR_ACCUMULATED_YIELD, name_prefix=static_attributes["model_name"] +"_total_yield", ), HuaweiSolarEntitySensor( inverter=inverter, - unit=ENERGY_KILO_WATT_HOUR, + unit=UnitOfEnergy.KILO_WATT_HOUR, icon="mdi:solar-power", - device_class=DEVICE_CLASS_ENERGY, + device_class=SensorDeviceClass.ENERGY, parent_sensor=huawei_solar_sensor, register=ATTR_GRID_EXPORTED, name_prefix=static_attributes["model_name"] + "_grid_exported", ), HuaweiSolarEntitySensor( inverter=inverter, - unit=ENERGY_KILO_WATT_HOUR, + unit=UnitOfEnergy.KILO_WATT_HOUR, icon="mdi:solar-power", - device_class=DEVICE_CLASS_ENERGY, + device_class=SensorDeviceClass.ENERGY, parent_sensor=huawei_solar_sensor, register=ATTR_GRID_ACCUMULATED, name_prefix=static_attributes["model_name"] + "_grid_accumulated", @@ -261,25 +255,25 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= [ HuaweiSolarEntitySensor( inverter=inverter, - unit=POWER_WATT, + unit=UnitOfPower.WATT, icon="mdi:solar-power", - device_class=DEVICE_CLASS_POWER, + device_class=SensorDeviceClass.POWER, parent_sensor=huawei_solar_sensor, register=ATTR_STORAGE_CHARGE_DISCHARGE_POWER, ), HuaweiSolarEntitySensor( inverter=inverter, - unit=ENERGY_KILO_WATT_HOUR, + unit=UnitOfEnergy.KILO_WATT_HOUR, icon="mdi:solar-power", - device_class=DEVICE_CLASS_ENERGY, + device_class=SensorDeviceClass.ENERGY, parent_sensor=huawei_solar_sensor, register=ATTR_STORAGE_TOTAL_CHARGE, ), HuaweiSolarEntitySensor( inverter=inverter, - unit=ENERGY_KILO_WATT_HOUR, + unit=UnitOfEnergy.KILO_WATT_HOUR, icon="mdi:solar-power", - device_class=DEVICE_CLASS_ENERGY, + device_class=SensorDeviceClass.ENERGY, parent_sensor=huawei_solar_sensor, register=ATTR_STORAGE_TOTAL_DISCHARGE, ), @@ -298,7 +292,7 @@ def __init__( self._optimizers_installed = optimizers_installed self._battery_installed = battery_installed self._hidden = False - self._unit = POWER_WATT + self._unit = UnitOfPower.WATT self._icon = "mdi:solar-power" self._available = False self._state = None @@ -335,7 +329,7 @@ def state(self): @property def device_class(self): - return DEVICE_CLASS_POWER + return SensorDeviceClass.POWER @property def extra_state_attributes(self): From d9aff7fbbccb71ee9ed1c8bc40c689140d72901a Mon Sep 17 00:00:00 2001 From: titidnh Date: Sun, 10 Mar 2024 14:08:14 +0100 Subject: [PATCH 6/7] Update sensor.py --- custom_components/huawei_solar/sensor.py | 1 + 1 file changed, 1 insertion(+) diff --git a/custom_components/huawei_solar/sensor.py b/custom_components/huawei_solar/sensor.py index a19f960..2f49af8 100644 --- a/custom_components/huawei_solar/sensor.py +++ b/custom_components/huawei_solar/sensor.py @@ -14,6 +14,7 @@ from homeassistant.const import ( CONF_HOST, UnitOfEnergy, + UnitOfPower, ) from homeassistant.helpers.entity import Entity from homeassistant.util.dt import utc_from_timestamp From 916023bd17b932f85a007ab3cd177ed9ebe85ab8 Mon Sep 17 00:00:00 2001 From: titidnh Date: Sun, 10 Mar 2024 14:10:15 +0100 Subject: [PATCH 7/7] Update manifest.json --- custom_components/huawei_solar/manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/huawei_solar/manifest.json b/custom_components/huawei_solar/manifest.json index 830e8aa..b2dbc5d 100644 --- a/custom_components/huawei_solar/manifest.json +++ b/custom_components/huawei_solar/manifest.json @@ -1,7 +1,7 @@ { "domain": "huawei_solar", "name": "huawei_solar", - "documentation": "https://github.com/titidnh/huawei_solar", + "documentation": "https://github.com/Emilv2/huawei_solar", "dependencies": [], "codeowners": ["Emilv2"], "requirements": ["huawei-solar>=2.0.0,<=2.2.9"],