From a911ed2ce515f5a3e7fbfc04affd9a7dc297578f Mon Sep 17 00:00:00 2001 From: Tijs Verkoyen Date: Fri, 1 Sep 2023 15:52:16 +0200 Subject: [PATCH 1/3] Skip devices that are linked to a disabled station. This limits the amount of data that is requested. But also will skip updating data for entities that are not enabled. --- .../fusion_solar/device_real_kpi_coordinator.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/custom_components/fusion_solar/device_real_kpi_coordinator.py b/custom_components/fusion_solar/device_real_kpi_coordinator.py index b01b93c..f275568 100644 --- a/custom_components/fusion_solar/device_real_kpi_coordinator.py +++ b/custom_components/fusion_solar/device_real_kpi_coordinator.py @@ -84,6 +84,11 @@ def device_ids_grouped_per_type_id(self): _LOGGER.debug(f'Device {device.name} ({device.device_id}) is disabled by the user.') continue + station_from_registry = device_registry.async_get_device(identifiers={(DOMAIN, device.station_code)}) + if station_from_registry is not None and station_from_registry.disabled: + _LOGGER.debug(f'Device {device.name} ({device.device_id}) linked to a disabled station ({device.station_code}).') + continue + if device.type_id not in device_ids_grouped_per_type_id: device_ids_grouped_per_type_id[device.type_id] = [] device_ids_grouped_per_type_id[device.type_id].append(str(device.device_id)) From 89cc579290dbc92df9312fa691d45a9244631e97 Mon Sep 17 00:00:00 2001 From: Tijs Verkoyen Date: Fri, 1 Sep 2023 15:58:21 +0200 Subject: [PATCH 2/3] Warning in logs if more than 100 stations are detected. --- custom_components/fusion_solar/sensor.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/custom_components/fusion_solar/sensor.py b/custom_components/fusion_solar/sensor.py index 442d82f..189f40f 100644 --- a/custom_components/fusion_solar/sensor.py +++ b/custom_components/fusion_solar/sensor.py @@ -797,7 +797,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities): if not stations: _LOGGER.error('No stations found') - raise IntegrationError("No stations found in OpenAPI") + raise IntegrationError('No stations found in OpenAPI') + + if len(stations) > 100: + _LOGGER.error('More than 100 stations found, which is not a good idea.') await add_entities_for_stations(hass, async_add_entities, stations, api) From 5be45ca3c723be9800221fb781647185effc6270 Mon Sep 17 00:00:00 2001 From: Tijs Verkoyen Date: Fri, 1 Sep 2023 15:59:13 +0200 Subject: [PATCH 3/3] Better way of filtering the disabled stations --- custom_components/fusion_solar/sensor.py | 47 +++++++++++------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/custom_components/fusion_solar/sensor.py b/custom_components/fusion_solar/sensor.py index 189f40f..333b74b 100644 --- a/custom_components/fusion_solar/sensor.py +++ b/custom_components/fusion_solar/sensor.py @@ -56,6 +56,15 @@ _LOGGER = logging.getLogger(__name__) +def filter_for_enabled_stations(station, device_registry): + device_from_registry = device_registry.async_get_device(identifiers={(DOMAIN, station.code)}) + if device_from_registry is not None and device_from_registry.disabled: + _LOGGER.debug(f'Station {station.code} is disabled by the user.') + return False + + return True + + async def add_entities_for_kiosk(hass, async_add_entities, kiosk: FusionSolarKiosk): _LOGGER.debug(f'Adding entities for kiosk {kiosk.id}') @@ -137,8 +146,10 @@ async def async_update_kiosk_data(): async def add_entities_for_stations(hass, async_add_entities, stations, api: FusionSolarOpenApi): - _LOGGER.debug(f'Adding entities for stations') + device_registry = dr.async_get(hass) + stations = list(filter(lambda x: filter_for_enabled_stations(x, device_registry), stations)) station_codes = [station.code for station in stations] + _LOGGER.debug(f'Adding entities for stations ({len(station_codes)})') await _add_entities_for_stations_real_kpi_data(hass, async_add_entities, stations, api) await _add_entities_for_stations_year_kpi_data(hass, async_add_entities, stations, api) @@ -574,26 +585,19 @@ async def add_entities_for_stations(hass, async_add_entities, stations, api: Fus async def _add_entities_for_stations_real_kpi_data(hass, async_add_entities, stations, api: FusionSolarOpenApi): + device_registry = dr.async_get(hass) + stations = list(filter(lambda x: filter_for_enabled_stations(x, device_registry), stations)) station_codes = [station.code for station in stations] + _LOGGER.debug(f'Adding stations_real_kpi_data entities for stations ({len(station_codes)})') async def async_update_station_real_kpi_data(): """Fetch data""" data = {} - device_registry = dr.async_get(hass) - station_codes_to_get_data_for = [] - for code in station_codes: - device_from_registry = device_registry.async_get_device(identifiers={(DOMAIN, code)}) - if device_from_registry is not None and device_from_registry.disabled: - _LOGGER.debug(f'Station {code} is disabled by the user.') - continue - - station_codes_to_get_data_for.append(code) - - if station_codes_to_get_data_for is None or len(station_codes_to_get_data_for) == 0: + if station_codes is None or len(station_codes) == 0: return data - response = await hass.async_add_executor_job(api.get_station_real_kpi, station_codes_to_get_data_for) + response = await hass.async_add_executor_job(api.get_station_real_kpi, station_codes) for response_data in response: data[f'{DOMAIN}-{response_data[ATTR_STATION_CODE]}'] = response_data[ATTR_STATION_REAL_KPI_DATA_ITEM_MAP] @@ -664,25 +668,18 @@ async def async_update_station_real_kpi_data(): async def _add_entities_for_stations_year_kpi_data(hass, async_add_entities, stations, api: FusionSolarOpenApi): + device_registry = dr.async_get(hass) + stations = list(filter(lambda x: filter_for_enabled_stations(x, device_registry), stations)) station_codes = [station.code for station in stations] + _LOGGER.debug(f'Adding stations_year_kpi_data entities for stations ({len(station_codes)})') async def async_update_station_year_kpi_data(): data = {} - device_registry = dr.async_get(hass) - station_codes_to_get_data_for = [] - for code in station_codes: - device_from_registry = device_registry.async_get_device(identifiers={(DOMAIN, code)}) - if device_from_registry is not None and device_from_registry.disabled: - _LOGGER.debug(f'Station {code} is disabled by the user.') - continue - - station_codes_to_get_data_for.append(code) - - if station_codes_to_get_data_for is None or len(station_codes_to_get_data_for) == 0: + if station_codes is None or len(station_codes) == 0: return data - response = await hass.async_add_executor_job(api.get_kpi_station_year, station_codes_to_get_data_for) + response = await hass.async_add_executor_job(api.get_kpi_station_year, station_codes) for response_data in response: key = f'{DOMAIN}-{response_data[ATTR_STATION_CODE]}'