From f91db2724dacd89c994f5ee3127d28f644e9ca99 Mon Sep 17 00:00:00 2001 From: Jacques Rascagneres Date: Mon, 30 Oct 2023 01:27:41 +0000 Subject: [PATCH] Grid generation percentages --- README.md | 9 ++++ .../coordinators/national_grid.py | 50 +++++++++++++++++-- custom_components/national_grid/manifest.json | 2 +- custom_components/national_grid/models.py | 4 ++ custom_components/national_grid/sensor.py | 28 +++++++++++ 5 files changed, 89 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a011b88..636a23d 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,11 @@ Then follow the same steps that are using to setup most integrations: | National Grid Total Generation | sensor.total_generation_mwh | Total generation in MWh | | National Grid Total Demand MWh | sensor.national_grid_total_demand_mwh | Total electricity demand in MWh. This is all generation with the inclusion of interconnectors and storage. | | National Grid Total Transfers MWh | sensor.national_grid_total_transfers_mwh | Total electricity transfers in MWh. This is all the transfers which are interconnectors and storage. | +National Grid Grid Generation Fossil Fuel Percentage | sensor.national_grid_fossil_fuel_percentage_generation | Percentage of total grid generation that is generated from fossil fuel sources: Gas, Oil & Coal +National Grid Grid Generation Renewable Percentage | sensor.national_grid_renewable_percentage_generation | Percentage of total grid generation that is generated from renewable sources: Solar, Wind & Hydro +National Grid Grid Generation Low Carbon Percentage | sensor.national_grid_low_carbon_percentage_generation | Percentage of total grid generation that is generated from renewable & low carbon sources: Solar, Wind, Hydro & Nuclear +National Grid Grid Generation Other Percentage | sensor.national_grid_other_percentage_generation | Percentage of total grid generation that is generated from 'other' sources: Nuclear, Biomass & Unknown / Other + Note that the associated time sensors are important. Updates can lag by a few minutes and are in UTC so its possible that 'today' and 'tomorrow' aren't entirely accurate for a period of time. @@ -99,6 +104,10 @@ biomass_mwh belgium_mwh norway_mwh total_generation_mwh +fossil_fuel_percentage_generation +renewable_percentage_generation +low_carbon_percentage_generation +other_percentage_generation grid_collection_time ``` diff --git a/custom_components/national_grid/coordinators/national_grid.py b/custom_components/national_grid/coordinators/national_grid.py index edd20e3..10d7872 100644 --- a/custom_components/national_grid/coordinators/national_grid.py +++ b/custom_components/national_grid/coordinators/national_grid.py @@ -197,7 +197,7 @@ def get_hourly_wind_forecast(now_utc: datetime) -> NationalGridWindForecast: current_generation = int(item["generation"]) if current_generation == 0: - raise UnexpectedDataError + raise UnexpectedDataError("Hourly wind forecast 'current' is 0") return NationalGridWindForecast( forecast=wind_forecast, current_value=current_generation @@ -256,7 +256,7 @@ def get_hourly_wind_forecast_earliest(now_utc: datetime) -> NationalGridWindFore current_generation = int(item["generation"]) if current_generation == 0: - raise UnexpectedDataError + raise UnexpectedDataError("Earliest hourly wind forecast 'current' is 0") return NationalGridWindForecast( forecast=wind_forecast_earliest, current_value=current_generation @@ -449,6 +449,9 @@ def get_generation(utc_now: datetime) -> NationalGridGeneration: belgium_mwh=0, norway_mwh=0, total_generation_mwh=0, + fossil_fuel_percentage_generation=0, + renewable_percentage_generation=0, + other_percentage_generation=0, grid_collection_time=latest_publish_time, ) @@ -497,7 +500,7 @@ def get_generation(utc_now: datetime) -> NationalGridGeneration: and national_grid_generation["nuclear_mwh"] == 0 and national_grid_generation["hydro_mwh"] == 0 ): - raise UnexpectedDataError(url) + raise UnexpectedDataError("Getting generation returned numerous zero values") return national_grid_generation @@ -534,6 +537,43 @@ def get_generation_combined(api_key: str, now_utc_full: datetime, today_utc: str + grid_generation["other_mwh"] ) + grid_generation["fossil_fuel_percentage_generation"] = percentage_calc( + ( + grid_generation["gas_mwh"] + + grid_generation["oil_mwh"] + + grid_generation["coal_mwh"] + ), + grid_generation["total_generation_mwh"], + ) + + grid_generation["renewable_percentage_generation"] = percentage_calc( + ( + grid_generation["solar_mwh"] + + grid_generation["wind_mwh"] + + grid_generation["hydro_mwh"] + ), + grid_generation["total_generation_mwh"], + ) + + grid_generation["low_carbon_percentage_generation"] = percentage_calc( + ( + grid_generation["solar_mwh"] + + grid_generation["wind_mwh"] + + grid_generation["hydro_mwh"] + + grid_generation["nuclear_mwh"] + ), + grid_generation["total_generation_mwh"], + ) + + grid_generation["other_percentage_generation"] = percentage_calc( + ( + grid_generation["nuclear_mwh"] + + grid_generation["biomass_mwh"] + + grid_generation["other_mwh"] + ), + grid_generation["total_generation_mwh"], + ) + return grid_generation @@ -627,3 +667,7 @@ def obtain_data_with_fallback(current_data, key, func, *args): except Exception as e: # pylint: disable=broad-except _LOGGER.exception("Failed to obtain data") return get_data_if_exists(current_data, key) + + +def percentage_calc(int_sum, int_total): + return round(int_sum / int_total * 100, 2) diff --git a/custom_components/national_grid/manifest.json b/custom_components/national_grid/manifest.json index 889795b..628db78 100644 --- a/custom_components/national_grid/manifest.json +++ b/custom_components/national_grid/manifest.json @@ -7,6 +7,6 @@ "dependencies": [], "iot_class": "cloud_polling", "requirements": [], - "version": "0.0.27", + "version": "0.0.28", "config_flow": true } \ No newline at end of file diff --git a/custom_components/national_grid/models.py b/custom_components/national_grid/models.py index dfcdaef..1f02720 100644 --- a/custom_components/national_grid/models.py +++ b/custom_components/national_grid/models.py @@ -21,6 +21,10 @@ class NationalGridGeneration(TypedDict): belgium_mwh: int # intnem ( Nemo ) norway_mwh: int # intnsl ( North Sea Link ) total_generation_mwh: int # total generation + fossil_fuel_percentage_generation: int # Counts gas, oil, coal + renewable_percentage_generation: int # Counts solar, wind, hydro + low_carbon_percentage_generation: int # Counts renewable, nuclear + other_percentage_generation: int # Counts nuclear, biomass grid_collection_time: datetime diff --git a/custom_components/national_grid/sensor.py b/custom_components/national_grid/sensor.py index 73bf6f0..09ad740 100644 --- a/custom_components/national_grid/sensor.py +++ b/custom_components/national_grid/sensor.py @@ -284,6 +284,34 @@ class NationalGridSensorEntityDescription(SensorEntityDescription): icon="mdi:transmission-tower", extra_attributes_key="grid_generation", ), + NationalGridSensorEntityDescription( + key="grid_generation.fossil_fuel_percentage_generation", + name="Grid Generation Fossil Fuel Percentage", + unique_id="fossil_fuel_percentage_generation", + native_unit_of_measurement="%", + icon="mdi:molecule-co2", + ), + NationalGridSensorEntityDescription( + key="grid_generation.renewable_percentage_generation", + name="Grid Generation Renewable Percentage", + unique_id="renewable_percentage_generation", + native_unit_of_measurement="%", + icon="mdi:leaf", + ), + NationalGridSensorEntityDescription( + key="grid_generation.low_carbon_percentage_generation", + name="Grid Generation Low Carbon Percentage", + unique_id="low_carbon_percentage_generation", + native_unit_of_measurement="%", + icon="mdi:leaf", + ), + NationalGridSensorEntityDescription( + key="grid_generation.other_percentage_generation", + name="Grid Generation Other Percentage", + unique_id="other_percentage_generation", + native_unit_of_measurement="%", + icon="mdi:help", + ), )