From efce6c846875e77d342b5e810f84244f4df5dd91 Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Tue, 26 Aug 2025 23:16:17 +0200 Subject: [PATCH] Bump aioelectricitymaps to v1.1.1 (#150928) --- .../components/co2signal/coordinator.py | 6 ++--- homeassistant/components/co2signal/helpers.py | 24 ++++++++++++------- .../components/co2signal/manifest.json | 2 +- homeassistant/components/co2signal/sensor.py | 10 ++++---- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/co2signal/__init__.py | 14 +++++------ tests/components/co2signal/conftest.py | 3 +-- .../components/co2signal/test_config_flow.py | 6 ++--- tests/components/co2signal/test_sensor.py | 11 +++------ 10 files changed, 39 insertions(+), 41 deletions(-) diff --git a/homeassistant/components/co2signal/coordinator.py b/homeassistant/components/co2signal/coordinator.py index be2036292e3..f29f3c72f1f 100644 --- a/homeassistant/components/co2signal/coordinator.py +++ b/homeassistant/components/co2signal/coordinator.py @@ -6,10 +6,10 @@ from datetime import timedelta import logging from aioelectricitymaps import ( - CarbonIntensityResponse, ElectricityMaps, ElectricityMapsError, ElectricityMapsInvalidTokenError, + HomeAssistantCarbonIntensityResponse, ) from homeassistant.config_entries import ConfigEntry @@ -25,7 +25,7 @@ _LOGGER = logging.getLogger(__name__) type CO2SignalConfigEntry = ConfigEntry[CO2SignalCoordinator] -class CO2SignalCoordinator(DataUpdateCoordinator[CarbonIntensityResponse]): +class CO2SignalCoordinator(DataUpdateCoordinator[HomeAssistantCarbonIntensityResponse]): """Data update coordinator.""" config_entry: CO2SignalConfigEntry @@ -51,7 +51,7 @@ class CO2SignalCoordinator(DataUpdateCoordinator[CarbonIntensityResponse]): """Return entry ID.""" return self.config_entry.entry_id - async def _async_update_data(self) -> CarbonIntensityResponse: + async def _async_update_data(self) -> HomeAssistantCarbonIntensityResponse: """Fetch the latest data from the source.""" try: diff --git a/homeassistant/components/co2signal/helpers.py b/homeassistant/components/co2signal/helpers.py index 3feabef2fdd..b76e990c8f3 100644 --- a/homeassistant/components/co2signal/helpers.py +++ b/homeassistant/components/co2signal/helpers.py @@ -5,8 +5,12 @@ from __future__ import annotations from collections.abc import Mapping from typing import Any -from aioelectricitymaps import ElectricityMaps -from aioelectricitymaps.models import CarbonIntensityResponse +from aioelectricitymaps import ( + CoordinatesRequest, + ElectricityMaps, + HomeAssistantCarbonIntensityResponse, + ZoneRequest, +) from homeassistant.const import CONF_COUNTRY_CODE, CONF_LATITUDE, CONF_LONGITUDE from homeassistant.core import HomeAssistant @@ -16,14 +20,16 @@ async def fetch_latest_carbon_intensity( hass: HomeAssistant, em: ElectricityMaps, config: Mapping[str, Any], -) -> CarbonIntensityResponse: +) -> HomeAssistantCarbonIntensityResponse: """Fetch the latest carbon intensity based on country code or location coordinates.""" - if CONF_COUNTRY_CODE in config: - return await em.latest_carbon_intensity_by_country_code( - code=config[CONF_COUNTRY_CODE] - ) - - return await em.latest_carbon_intensity_by_coordinates( + request: CoordinatesRequest | ZoneRequest = CoordinatesRequest( lat=config.get(CONF_LATITUDE, hass.config.latitude), lon=config.get(CONF_LONGITUDE, hass.config.longitude), ) + + if CONF_COUNTRY_CODE in config: + request = ZoneRequest( + zone=config[CONF_COUNTRY_CODE], + ) + + return await em.carbon_intensity_for_home_assistant(request) diff --git a/homeassistant/components/co2signal/manifest.json b/homeassistant/components/co2signal/manifest.json index ff6d5bdb18b..106fd0686af 100644 --- a/homeassistant/components/co2signal/manifest.json +++ b/homeassistant/components/co2signal/manifest.json @@ -7,5 +7,5 @@ "integration_type": "service", "iot_class": "cloud_polling", "loggers": ["aioelectricitymaps"], - "requirements": ["aioelectricitymaps==0.4.0"] + "requirements": ["aioelectricitymaps==1.1.1"] } diff --git a/homeassistant/components/co2signal/sensor.py b/homeassistant/components/co2signal/sensor.py index a8e962532b8..9cf5ae4c9a7 100644 --- a/homeassistant/components/co2signal/sensor.py +++ b/homeassistant/components/co2signal/sensor.py @@ -5,7 +5,7 @@ from __future__ import annotations from collections.abc import Callable from dataclasses import dataclass -from aioelectricitymaps.models import CarbonIntensityResponse +from aioelectricitymaps import HomeAssistantCarbonIntensityResponse from homeassistant.components.sensor import ( SensorEntity, @@ -28,10 +28,10 @@ class CO2SensorEntityDescription(SensorEntityDescription): # For backwards compat, allow description to override unique ID key to use unique_id: str | None = None - unit_of_measurement_fn: Callable[[CarbonIntensityResponse], str | None] | None = ( - None - ) - value_fn: Callable[[CarbonIntensityResponse], float | None] + unit_of_measurement_fn: ( + Callable[[HomeAssistantCarbonIntensityResponse], str | None] | None + ) = None + value_fn: Callable[[HomeAssistantCarbonIntensityResponse], float | None] SENSORS = ( diff --git a/requirements_all.txt b/requirements_all.txt index e180406a123..ee8e847cd21 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -241,7 +241,7 @@ aioeagle==1.1.0 aioecowitt==2025.3.1 # homeassistant.components.co2signal -aioelectricitymaps==0.4.0 +aioelectricitymaps==1.1.1 # homeassistant.components.emonitor aioemonitor==1.0.5 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 1a2df46eadd..d71f5611b3c 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -229,7 +229,7 @@ aioeagle==1.1.0 aioecowitt==2025.3.1 # homeassistant.components.co2signal -aioelectricitymaps==0.4.0 +aioelectricitymaps==1.1.1 # homeassistant.components.emonitor aioemonitor==1.0.5 diff --git a/tests/components/co2signal/__init__.py b/tests/components/co2signal/__init__.py index 394db24347b..54d132fdc76 100644 --- a/tests/components/co2signal/__init__.py +++ b/tests/components/co2signal/__init__.py @@ -1,19 +1,19 @@ """Tests for the CO2 Signal integration.""" -from aioelectricitymaps.models import ( - CarbonIntensityData, - CarbonIntensityResponse, - CarbonIntensityUnit, +from aioelectricitymaps import HomeAssistantCarbonIntensityResponse +from aioelectricitymaps.models.home_assistant import ( + HomeAssistantCarbonIntensityData, + HomeAssistantCarbonIntensityUnit, ) -VALID_RESPONSE = CarbonIntensityResponse( +VALID_RESPONSE = HomeAssistantCarbonIntensityResponse( status="ok", country_code="FR", - data=CarbonIntensityData( + data=HomeAssistantCarbonIntensityData( carbon_intensity=45.98623190095805, fossil_fuel_percentage=5.461182741937103, ), - units=CarbonIntensityUnit( + units=HomeAssistantCarbonIntensityUnit( carbon_intensity="gCO2eq/kWh", ), ) diff --git a/tests/components/co2signal/conftest.py b/tests/components/co2signal/conftest.py index 680465c2537..48b3cf35e64 100644 --- a/tests/components/co2signal/conftest.py +++ b/tests/components/co2signal/conftest.py @@ -30,8 +30,7 @@ def mock_electricity_maps() -> Generator[MagicMock]: ), ): client = electricity_maps.return_value - client.latest_carbon_intensity_by_coordinates.return_value = VALID_RESPONSE - client.latest_carbon_intensity_by_country_code.return_value = VALID_RESPONSE + client.carbon_intensity_for_home_assistant.return_value = VALID_RESPONSE yield client diff --git a/tests/components/co2signal/test_config_flow.py b/tests/components/co2signal/test_config_flow.py index f8f94d44126..6c8b6a977fa 100644 --- a/tests/components/co2signal/test_config_flow.py +++ b/tests/components/co2signal/test_config_flow.py @@ -157,8 +157,7 @@ async def test_form_error_handling( DOMAIN, context={"source": config_entries.SOURCE_USER} ) - electricity_maps.latest_carbon_intensity_by_coordinates.side_effect = side_effect - electricity_maps.latest_carbon_intensity_by_country_code.side_effect = side_effect + electricity_maps.carbon_intensity_for_home_assistant.side_effect = side_effect result = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -172,8 +171,7 @@ async def test_form_error_handling( assert result["errors"] == {"base": err_code} # reset mock and test if now succeeds - electricity_maps.latest_carbon_intensity_by_coordinates.side_effect = None - electricity_maps.latest_carbon_intensity_by_country_code.side_effect = None + electricity_maps.carbon_intensity_for_home_assistant.side_effect = None result = await hass.config_entries.flow.async_configure( result["flow_id"], diff --git a/tests/components/co2signal/test_sensor.py b/tests/components/co2signal/test_sensor.py index 2154782f62d..16c9763a222 100644 --- a/tests/components/co2signal/test_sensor.py +++ b/tests/components/co2signal/test_sensor.py @@ -62,8 +62,7 @@ async def test_sensor_update_fail( assert state.state == "45.9862319009581" assert len(electricity_maps.mock_calls) == 1 - electricity_maps.latest_carbon_intensity_by_coordinates.side_effect = error - electricity_maps.latest_carbon_intensity_by_country_code.side_effect = error + electricity_maps.carbon_intensity_for_home_assistant.side_effect = error freezer.tick(timedelta(minutes=20)) async_fire_time_changed(hass) @@ -74,8 +73,7 @@ async def test_sensor_update_fail( assert len(electricity_maps.mock_calls) == 2 # reset mock and test if entity is available again - electricity_maps.latest_carbon_intensity_by_coordinates.side_effect = None - electricity_maps.latest_carbon_intensity_by_country_code.side_effect = None + electricity_maps.carbon_intensity_for_home_assistant.side_effect = None freezer.tick(timedelta(minutes=20)) async_fire_time_changed(hass) @@ -96,10 +94,7 @@ async def test_sensor_reauth_triggered( assert (state := hass.states.get("sensor.electricity_maps_co2_intensity")) assert state.state == "45.9862319009581" - electricity_maps.latest_carbon_intensity_by_coordinates.side_effect = ( - ElectricityMapsInvalidTokenError - ) - electricity_maps.latest_carbon_intensity_by_country_code.side_effect = ( + electricity_maps.carbon_intensity_for_home_assistant.side_effect = ( ElectricityMapsInvalidTokenError )