From 1a5f2c9c32d7b4705b8c4dd3ea43e9902660214b Mon Sep 17 00:00:00 2001 From: Matthias Alphart Date: Wed, 24 Nov 2021 11:17:38 +0100 Subject: [PATCH] Disable less popular Fronius entities by default (#60264) --- homeassistant/components/fronius/sensor.py | 41 ++++++++++++++++++++++ tests/components/fronius/__init__.py | 18 +++++++++- tests/components/fronius/test_sensor.py | 28 +++++++++++---- 3 files changed, 80 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/fronius/sensor.py b/homeassistant/components/fronius/sensor.py index 7c2a1bf6c09..fa2a33ca7ea 100644 --- a/homeassistant/components/fronius/sensor.py +++ b/homeassistant/components/fronius/sensor.py @@ -21,6 +21,7 @@ from homeassistant.const import ( DEVICE_CLASS_BATTERY, DEVICE_CLASS_CURRENT, DEVICE_CLASS_ENERGY, + DEVICE_CLASS_FREQUENCY, DEVICE_CLASS_POWER, DEVICE_CLASS_POWER_FACTOR, DEVICE_CLASS_TEMPERATURE, @@ -145,7 +146,9 @@ INVERTER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ key="frequency_ac", name="Frequency AC", native_unit_of_measurement=FREQUENCY_HERTZ, + device_class=DEVICE_CLASS_FREQUENCY, state_class=STATE_CLASS_MEASUREMENT, + entity_registry_enabled_default=False, ), SensorEntityDescription( key="current_ac", @@ -183,6 +186,7 @@ INVERTER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT, device_class=DEVICE_CLASS_VOLTAGE, state_class=STATE_CLASS_MEASUREMENT, + entity_registry_enabled_default=False, ), SensorEntityDescription( key="voltage_dc", @@ -220,11 +224,13 @@ INVERTER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ key="led_state", name="LED state", entity_category=ENTITY_CATEGORY_DIAGNOSTIC, + entity_registry_enabled_default=False, ), SensorEntityDescription( key="led_color", name="LED color", entity_category=ENTITY_CATEGORY_DIAGNOSTIC, + entity_registry_enabled_default=False, ), ] @@ -256,6 +262,7 @@ METER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE, device_class=DEVICE_CLASS_CURRENT, state_class=STATE_CLASS_MEASUREMENT, + entity_registry_enabled_default=False, ), SensorEntityDescription( key="current_ac_phase_2", @@ -263,6 +270,7 @@ METER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE, device_class=DEVICE_CLASS_CURRENT, state_class=STATE_CLASS_MEASUREMENT, + entity_registry_enabled_default=False, ), SensorEntityDescription( key="current_ac_phase_3", @@ -270,6 +278,7 @@ METER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE, device_class=DEVICE_CLASS_CURRENT, state_class=STATE_CLASS_MEASUREMENT, + entity_registry_enabled_default=False, ), SensorEntityDescription( key="energy_reactive_ac_consumed", @@ -277,6 +286,7 @@ METER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=ENERGY_VOLT_AMPERE_REACTIVE_HOUR, state_class=STATE_CLASS_TOTAL_INCREASING, icon="mdi:lightning-bolt-outline", + entity_registry_enabled_default=False, ), SensorEntityDescription( key="energy_reactive_ac_produced", @@ -284,6 +294,7 @@ METER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=ENERGY_VOLT_AMPERE_REACTIVE_HOUR, state_class=STATE_CLASS_TOTAL_INCREASING, icon="mdi:lightning-bolt-outline", + entity_registry_enabled_default=False, ), SensorEntityDescription( key="energy_real_ac_minus", @@ -291,6 +302,7 @@ METER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=ENERGY_WATT_HOUR, device_class=DEVICE_CLASS_ENERGY, state_class=STATE_CLASS_TOTAL_INCREASING, + entity_registry_enabled_default=False, ), SensorEntityDescription( key="energy_real_ac_plus", @@ -298,6 +310,7 @@ METER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=ENERGY_WATT_HOUR, device_class=DEVICE_CLASS_ENERGY, state_class=STATE_CLASS_TOTAL_INCREASING, + entity_registry_enabled_default=False, ), SensorEntityDescription( key="energy_real_consumed", @@ -317,6 +330,7 @@ METER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ key="frequency_phase_average", name="Frequency phase average", native_unit_of_measurement=FREQUENCY_HERTZ, + device_class=DEVICE_CLASS_FREQUENCY, state_class=STATE_CLASS_MEASUREMENT, ), SensorEntityDescription( @@ -330,6 +344,7 @@ METER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=POWER_VOLT_AMPERE, state_class=STATE_CLASS_MEASUREMENT, icon="mdi:flash-outline", + entity_registry_enabled_default=False, ), SensorEntityDescription( key="power_apparent_phase_2", @@ -337,6 +352,7 @@ METER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=POWER_VOLT_AMPERE, state_class=STATE_CLASS_MEASUREMENT, icon="mdi:flash-outline", + entity_registry_enabled_default=False, ), SensorEntityDescription( key="power_apparent_phase_3", @@ -344,6 +360,7 @@ METER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=POWER_VOLT_AMPERE, state_class=STATE_CLASS_MEASUREMENT, icon="mdi:flash-outline", + entity_registry_enabled_default=False, ), SensorEntityDescription( key="power_apparent", @@ -351,24 +368,28 @@ METER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=POWER_VOLT_AMPERE, state_class=STATE_CLASS_MEASUREMENT, icon="mdi:flash-outline", + entity_registry_enabled_default=False, ), SensorEntityDescription( key="power_factor_phase_1", name="Power factor phase 1", device_class=DEVICE_CLASS_POWER_FACTOR, state_class=STATE_CLASS_MEASUREMENT, + entity_registry_enabled_default=False, ), SensorEntityDescription( key="power_factor_phase_2", name="Power factor phase 2", device_class=DEVICE_CLASS_POWER_FACTOR, state_class=STATE_CLASS_MEASUREMENT, + entity_registry_enabled_default=False, ), SensorEntityDescription( key="power_factor_phase_3", name="Power factor phase 3", device_class=DEVICE_CLASS_POWER_FACTOR, state_class=STATE_CLASS_MEASUREMENT, + entity_registry_enabled_default=False, ), SensorEntityDescription( key="power_factor", @@ -382,6 +403,7 @@ METER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=POWER_VOLT_AMPERE_REACTIVE, state_class=STATE_CLASS_MEASUREMENT, icon="mdi:flash-outline", + entity_registry_enabled_default=False, ), SensorEntityDescription( key="power_reactive_phase_2", @@ -389,6 +411,7 @@ METER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=POWER_VOLT_AMPERE_REACTIVE, state_class=STATE_CLASS_MEASUREMENT, icon="mdi:flash-outline", + entity_registry_enabled_default=False, ), SensorEntityDescription( key="power_reactive_phase_3", @@ -396,6 +419,7 @@ METER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=POWER_VOLT_AMPERE_REACTIVE, state_class=STATE_CLASS_MEASUREMENT, icon="mdi:flash-outline", + entity_registry_enabled_default=False, ), SensorEntityDescription( key="power_reactive", @@ -403,6 +427,7 @@ METER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=POWER_VOLT_AMPERE_REACTIVE, state_class=STATE_CLASS_MEASUREMENT, icon="mdi:flash-outline", + entity_registry_enabled_default=False, ), SensorEntityDescription( key="power_real_phase_1", @@ -410,6 +435,7 @@ METER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=POWER_WATT, device_class=DEVICE_CLASS_POWER, state_class=STATE_CLASS_MEASUREMENT, + entity_registry_enabled_default=False, ), SensorEntityDescription( key="power_real_phase_2", @@ -417,6 +443,7 @@ METER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=POWER_WATT, device_class=DEVICE_CLASS_POWER, state_class=STATE_CLASS_MEASUREMENT, + entity_registry_enabled_default=False, ), SensorEntityDescription( key="power_real_phase_3", @@ -424,6 +451,7 @@ METER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=POWER_WATT, device_class=DEVICE_CLASS_POWER, state_class=STATE_CLASS_MEASUREMENT, + entity_registry_enabled_default=False, ), SensorEntityDescription( key="power_real", @@ -438,6 +466,7 @@ METER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT, device_class=DEVICE_CLASS_VOLTAGE, state_class=STATE_CLASS_MEASUREMENT, + entity_registry_enabled_default=False, ), SensorEntityDescription( key="voltage_ac_phase_2", @@ -445,6 +474,7 @@ METER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT, device_class=DEVICE_CLASS_VOLTAGE, state_class=STATE_CLASS_MEASUREMENT, + entity_registry_enabled_default=False, ), SensorEntityDescription( key="voltage_ac_phase_3", @@ -452,6 +482,7 @@ METER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT, device_class=DEVICE_CLASS_VOLTAGE, state_class=STATE_CLASS_MEASUREMENT, + entity_registry_enabled_default=False, ), SensorEntityDescription( key="voltage_ac_phase_to_phase_12", @@ -459,6 +490,7 @@ METER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT, device_class=DEVICE_CLASS_VOLTAGE, state_class=STATE_CLASS_MEASUREMENT, + entity_registry_enabled_default=False, ), SensorEntityDescription( key="voltage_ac_phase_to_phase_23", @@ -466,6 +498,7 @@ METER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT, device_class=DEVICE_CLASS_VOLTAGE, state_class=STATE_CLASS_MEASUREMENT, + entity_registry_enabled_default=False, ), SensorEntityDescription( key="voltage_ac_phase_to_phase_31", @@ -473,6 +506,7 @@ METER_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT, device_class=DEVICE_CLASS_VOLTAGE, state_class=STATE_CLASS_MEASUREMENT, + entity_registry_enabled_default=False, ), ] @@ -483,6 +517,7 @@ POWER_FLOW_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=ENERGY_WATT_HOUR, device_class=DEVICE_CLASS_ENERGY, state_class=STATE_CLASS_TOTAL_INCREASING, + entity_registry_enabled_default=False, ), SensorEntityDescription( key="energy_year", @@ -490,6 +525,7 @@ POWER_FLOW_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=ENERGY_WATT_HOUR, device_class=DEVICE_CLASS_ENERGY, state_class=STATE_CLASS_TOTAL_INCREASING, + entity_registry_enabled_default=False, ), SensorEntityDescription( key="energy_total", @@ -497,6 +533,7 @@ POWER_FLOW_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ native_unit_of_measurement=ENERGY_WATT_HOUR, device_class=DEVICE_CLASS_ENERGY, state_class=STATE_CLASS_TOTAL_INCREASING, + entity_registry_enabled_default=False, ), SensorEntityDescription( key="meter_mode", @@ -552,11 +589,13 @@ STORAGE_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ key="capacity_maximum", name="Capacity maximum", native_unit_of_measurement=ELECTRIC_CHARGE_AMPERE_HOURS, + entity_category=ENTITY_CATEGORY_DIAGNOSTIC, ), SensorEntityDescription( key="capacity_designed", name="Capacity designed", native_unit_of_measurement=ELECTRIC_CHARGE_AMPERE_HOURS, + entity_category=ENTITY_CATEGORY_DIAGNOSTIC, ), SensorEntityDescription( key="current_dc", @@ -581,6 +620,7 @@ STORAGE_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ device_class=DEVICE_CLASS_VOLTAGE, state_class=STATE_CLASS_MEASUREMENT, icon="mdi:current-dc", + entity_registry_enabled_default=False, ), SensorEntityDescription( key="voltage_dc_minimum_cell", @@ -589,6 +629,7 @@ STORAGE_ENTITY_DESCRIPTIONS: list[SensorEntityDescription] = [ device_class=DEVICE_CLASS_VOLTAGE, state_class=STATE_CLASS_MEASUREMENT, icon="mdi:current-dc", + entity_registry_enabled_default=False, ), SensorEntityDescription( key="state_of_charge", diff --git a/tests/components/fronius/__init__.py b/tests/components/fronius/__init__.py index e2ef369987c..37c9481e32e 100644 --- a/tests/components/fronius/__init__.py +++ b/tests/components/fronius/__init__.py @@ -1,8 +1,10 @@ """Tests for the Fronius integration.""" from homeassistant.components.fronius.const import DOMAIN from homeassistant.const import CONF_HOST +from homeassistant.helpers import entity_registry as er +from homeassistant.util import dt -from tests.common import MockConfigEntry, load_fixture +from tests.common import MockConfigEntry, async_fire_time_changed, load_fixture from tests.test_util.aiohttp import AiohttpClientMocker MOCK_HOST = "http://fronius" @@ -22,6 +24,7 @@ async def setup_fronius_integration(hass): entry.add_to_hass(hass) await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() + return entry def mock_responses( @@ -70,3 +73,16 @@ def mock_responses( f"{host}/solar_api/v1/GetStorageRealtimeData.cgi?Scope=System", text=load_fixture("symo/GetStorageRealtimeData_System.json", "fronius"), ) + + +async def enable_all_entities(hass, config_entry_id, time_till_next_update): + """Enable all entities for a config entry and fast forward time to receive data.""" + registry = er.async_get(hass) + entities = er.async_entries_for_config_entry(registry, config_entry_id) + for entry in [ + entry for entry in entities if entry.disabled_by == er.DISABLED_INTEGRATION + ]: + registry.async_update_entity(entry.entity_id, **{"disabled_by": None}) + await hass.async_block_till_done() + async_fire_time_changed(hass, dt.utcnow() + time_till_next_update) + await hass.async_block_till_done() diff --git a/tests/components/fronius/test_sensor.py b/tests/components/fronius/test_sensor.py index fd64c127496..b4e56a06d62 100644 --- a/tests/components/fronius/test_sensor.py +++ b/tests/components/fronius/test_sensor.py @@ -1,13 +1,14 @@ """Tests for the Fronius sensor platform.""" from homeassistant.components.fronius.coordinator import ( FroniusInverterUpdateCoordinator, + FroniusMeterUpdateCoordinator, FroniusPowerFlowUpdateCoordinator, ) from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN from homeassistant.const import STATE_UNKNOWN from homeassistant.util import dt -from . import mock_responses, setup_fronius_integration +from . import enable_all_entities, mock_responses, setup_fronius_integration from tests.common import async_fire_time_changed @@ -21,8 +22,12 @@ async def test_symo_inverter(hass, aioclient_mock): # Init at night mock_responses(aioclient_mock, night=True) - await setup_fronius_integration(hass) + config_entry = await setup_fronius_integration(hass) + assert len(hass.states.async_all(domain_filter=SENSOR_DOMAIN)) == 23 + await enable_all_entities( + hass, config_entry.entry_id, FroniusInverterUpdateCoordinator.default_interval + ) assert len(hass.states.async_all(domain_filter=SENSOR_DOMAIN)) == 55 assert_state("sensor.current_dc_fronius_inverter_1_http_fronius", 0) assert_state("sensor.energy_day_fronius_inverter_1_http_fronius", 10828) @@ -36,7 +41,10 @@ async def test_symo_inverter(hass, aioclient_mock): hass, dt.utcnow() + FroniusInverterUpdateCoordinator.default_interval ) await hass.async_block_till_done() - + assert len(hass.states.async_all(domain_filter=SENSOR_DOMAIN)) == 57 + await enable_all_entities( + hass, config_entry.entry_id, FroniusInverterUpdateCoordinator.default_interval + ) assert len(hass.states.async_all(domain_filter=SENSOR_DOMAIN)) == 59 # 4 additional AC entities assert_state("sensor.current_dc_fronius_inverter_1_http_fronius", 2.19) @@ -60,8 +68,8 @@ async def test_symo_logger(hass, aioclient_mock): mock_responses(aioclient_mock) await setup_fronius_integration(hass) + assert len(hass.states.async_all(domain_filter=SENSOR_DOMAIN)) == 25 - assert len(hass.states.async_all(domain_filter=SENSOR_DOMAIN)) == 59 # ignored constant entities: # hardware_platform, hardware_version, product_type # software_version, time_zone, time_zone_location @@ -91,8 +99,12 @@ async def test_symo_meter(hass, aioclient_mock): assert state.state == str(expected_state) mock_responses(aioclient_mock) - await setup_fronius_integration(hass) + config_entry = await setup_fronius_integration(hass) + assert len(hass.states.async_all(domain_filter=SENSOR_DOMAIN)) == 25 + await enable_all_entities( + hass, config_entry.entry_id, FroniusMeterUpdateCoordinator.default_interval + ) assert len(hass.states.async_all(domain_filter=SENSOR_DOMAIN)) == 59 # ignored entities: # manufacturer, model, serial, enable, timestamp, visible, meter_location @@ -152,8 +164,12 @@ async def test_symo_power_flow(hass, aioclient_mock): # First test at night mock_responses(aioclient_mock, night=True) - await setup_fronius_integration(hass) + config_entry = await setup_fronius_integration(hass) + assert len(hass.states.async_all(domain_filter=SENSOR_DOMAIN)) == 23 + await enable_all_entities( + hass, config_entry.entry_id, FroniusInverterUpdateCoordinator.default_interval + ) assert len(hass.states.async_all(domain_filter=SENSOR_DOMAIN)) == 55 # ignored: location, mode, timestamp #