From 167fbcd1adb5bbdc82f35b4ef30f9fdc828b37f5 Mon Sep 17 00:00:00 2001 From: Joakim Olsson Date: Thu, 20 Apr 2023 22:18:34 +0200 Subject: [PATCH] fix: only create battery specific sensors if present in message from EnergyHub Fixes #316 --- custom_components/ferroamp/sensor.py | 8 ++- tests/test_sensor.py | 81 ++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/custom_components/ferroamp/sensor.py b/custom_components/ferroamp/sensor.py index ba79a2b..8bd3cc2 100644 --- a/custom_components/ferroamp/sensor.py +++ b/custom_components/ferroamp/sensor.py @@ -1409,6 +1409,7 @@ def ehub_sensors(slug, interval, precision_battery, precision_current, precision interval, precision_energy, config_id, + check_presence=True, ), EnergyFerroampSensor( "Battery Energy Consumed", @@ -1420,6 +1421,7 @@ def ehub_sensors(slug, interval, precision_battery, precision_current, precision interval, precision_energy, config_id, + check_presence=True, ), IntValFerroampSensor( "System State", @@ -1451,6 +1453,7 @@ def ehub_sensors(slug, interval, precision_battery, precision_current, precision interval, precision_battery, config_id, + check_presence=True, ), PercentageFerroampSensor( "System State of Health", @@ -1461,6 +1464,7 @@ def ehub_sensors(slug, interval, precision_battery, precision_current, precision interval, precision_battery, config_id, + check_presence=True, ), IntValFerroampSensor( "Apparent power", @@ -1493,7 +1497,8 @@ def ehub_sensors(slug, interval, precision_battery, precision_current, precision EHUB_NAME, interval, config_id, - state_class=SensorStateClass.MEASUREMENT + state_class=SensorStateClass.MEASUREMENT, + check_presence=True, ), IntValFerroampSensor( "Total rated capacity of all batteries", @@ -1505,6 +1510,7 @@ def ehub_sensors(slug, interval, precision_battery, precision_current, precision EHUB_NAME, interval, config_id, + check_presence=True, ), FloatValFerroampSensor( "Available three phase active current for load balancing", diff --git a/tests/test_sensor.py b/tests/test_sensor.py index 1fce812..5056403 100644 --- a/tests/test_sensor.py +++ b/tests/test_sensor.py @@ -621,6 +621,87 @@ async def test_only_adding_load_balancing_sensors_if_present_in_message(hass, mq assert state is None +async def test_only_adding_battery_specific_sensors_if_present_in_mqtt_message(hass, mqtt_mock): + config_entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_NAME: "Ferroamp", + CONF_PREFIX: "extapi" + }, + options={ + CONF_INTERVAL: 1, + CONF_PRECISION_BATTERY: 0, + CONF_PRECISION_CURRENT: 0, + CONF_PRECISION_ENERGY: 0, + CONF_PRECISION_FREQUENCY: 0, + CONF_PRECISION_TEMPERATURE: 0, + CONF_PRECISION_VOLTAGE: 0, + }, + version=1, + unique_id="ferroamp", + ) + config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(config_entry.entry_id) + er = entity_registry.async_get(hass) + await hass.async_block_till_done() + + topic = "extapi/data/ehub" + msg = """{ + "wloadconsq": {"L2": "5509231063416", "L3": "10852247351438", "L1": "7902091810549"}, + "iloadd": {"L2": "-0.67", "L3": "0.56", "L1": "1.55"}, + "wextconsq": {"L2": "5364952651263", "L3": "10118502962305", "L1": "7277915408026"}, + "ppv": {"val": "10107.51"}, + "iext": {"L2": "8.90", "L3": "7.49", "L1": "7.59"}, + "iloadq": {"L2": "1.16", "L3": "3.61", "L1": "3.89"}, + "iace": {"L2": "0.00", "L3": "0.00", "L1": "0.00"}, + "ul": {"L2": "233.81", "L3": "231.18", "L1": "228.81"}, + "pinvreactive": {"L2": "438.12", "L3": "444.64", "L1": "430.37"}, + "ts": {"val": "2021-03-08T08:43:12UTC"}, + "ploadreactive": {"L2": "-110.77", "L3": "91.54", "L1": "250.78"}, + "state": {"val": "4097"}, + "wloadprodq": {"L2": "18020837409", "L3": "8433745", "L1": "4976003"}, + "iavbl": {"L2": "26.31", "L3": "29.69", "L1": "31.20"}, + "pinv": {"L2": "-2263.35", "L3": "-2234.62", "L1": "-2224.66"}, + "iextq": {"L2": "-12.53", "L3": "-10.06", "L1": "-9.86"}, + "pext": {"L2": "-2071.57", "L3": "-1644.50", "L1": "-1595.28"}, + "wextprodq": {"L2": "1118056851556", "L3": "604554554552", "L1": "662115344893"}, + "wpv": {"val": "4422089590383"}, + "winvconsq": {"L2": "1475109889749", "L3": "1451934095829", "L1": "1436427014025"}, + "pextreactive": {"L2": "327.35", "L3": "536.18", "L1": "681.15"}, + "udc": {"neg": "-383.96", "pos": "384.31"}, + "sext": {"val": "5549.12"}, + "iextd": {"L2": "1.98", "L3": "3.28", "L1": "4.21"}, + "iavblq_3p": {"val": "29.05"}, + "iavblq": {"L2": "29.05", "L3": "33.93", "L1": "35.89"}, + "ild": {"L2": "2.65", "L3": "2.72", "L1": "2.66"}, + "gridfreq": {"val": "50.07"}, + "pload": {"L2": "191.78", "L3": "590.12", "L1": "629.38"}, + "ilq": {"L2": "-13.69", "L3": "-13.67", "L1": "-13.75"}, + "winvprodq": {"L2": "2610825033980", "L3": "2570987302422", "L1": "2567078340545"}, + "il": {"L2": "9.85", "L3": "9.85", "L1": "9.89"} + }""" + async_fire_mqtt_message(hass, topic, msg) + await hass.async_block_till_done() + + state = hass.states.get("sensor.ferroamp_battery_energy_produced") + assert state is None + + state = hass.states.get("sensor.ferroamp_battery_energy_consumed") + assert state is None + + state = hass.states.get("sensor.ferroamp_system_state_of_charge") + assert state is None + + state = hass.states.get("sensor.ferroamp_system_state_of_health") + assert state is None + + state = hass.states.get("sensor.ferroamp_battery_power") + assert state is None + + state = hass.states.get("sensor.ferroamp_total_rated_capacity_of_all_batteries") + assert state is None + + async def test_setting_esm_sensor_values_via_mqtt_message(hass, mqtt_mock): config_entry = create_config() config_entry.add_to_hass(hass)