Bump aioelectricitymaps to v1.1.1 (#150928)

This commit is contained in:
Jan-Philipp Benecke
2025-08-26 23:16:17 +02:00
committed by GitHub
parent 8c7e9bcf7c
commit efce6c8468
10 changed files with 39 additions and 41 deletions

View File

@@ -6,10 +6,10 @@ from datetime import timedelta
import logging import logging
from aioelectricitymaps import ( from aioelectricitymaps import (
CarbonIntensityResponse,
ElectricityMaps, ElectricityMaps,
ElectricityMapsError, ElectricityMapsError,
ElectricityMapsInvalidTokenError, ElectricityMapsInvalidTokenError,
HomeAssistantCarbonIntensityResponse,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
@@ -25,7 +25,7 @@ _LOGGER = logging.getLogger(__name__)
type CO2SignalConfigEntry = ConfigEntry[CO2SignalCoordinator] type CO2SignalConfigEntry = ConfigEntry[CO2SignalCoordinator]
class CO2SignalCoordinator(DataUpdateCoordinator[CarbonIntensityResponse]): class CO2SignalCoordinator(DataUpdateCoordinator[HomeAssistantCarbonIntensityResponse]):
"""Data update coordinator.""" """Data update coordinator."""
config_entry: CO2SignalConfigEntry config_entry: CO2SignalConfigEntry
@@ -51,7 +51,7 @@ class CO2SignalCoordinator(DataUpdateCoordinator[CarbonIntensityResponse]):
"""Return entry ID.""" """Return entry ID."""
return self.config_entry.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.""" """Fetch the latest data from the source."""
try: try:

View File

@@ -5,8 +5,12 @@ from __future__ import annotations
from collections.abc import Mapping from collections.abc import Mapping
from typing import Any from typing import Any
from aioelectricitymaps import ElectricityMaps from aioelectricitymaps import (
from aioelectricitymaps.models import CarbonIntensityResponse CoordinatesRequest,
ElectricityMaps,
HomeAssistantCarbonIntensityResponse,
ZoneRequest,
)
from homeassistant.const import CONF_COUNTRY_CODE, CONF_LATITUDE, CONF_LONGITUDE from homeassistant.const import CONF_COUNTRY_CODE, CONF_LATITUDE, CONF_LONGITUDE
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@@ -16,14 +20,16 @@ async def fetch_latest_carbon_intensity(
hass: HomeAssistant, hass: HomeAssistant,
em: ElectricityMaps, em: ElectricityMaps,
config: Mapping[str, Any], config: Mapping[str, Any],
) -> CarbonIntensityResponse: ) -> HomeAssistantCarbonIntensityResponse:
"""Fetch the latest carbon intensity based on country code or location coordinates.""" """Fetch the latest carbon intensity based on country code or location coordinates."""
if CONF_COUNTRY_CODE in config: request: CoordinatesRequest | ZoneRequest = CoordinatesRequest(
return await em.latest_carbon_intensity_by_country_code(
code=config[CONF_COUNTRY_CODE]
)
return await em.latest_carbon_intensity_by_coordinates(
lat=config.get(CONF_LATITUDE, hass.config.latitude), lat=config.get(CONF_LATITUDE, hass.config.latitude),
lon=config.get(CONF_LONGITUDE, hass.config.longitude), 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)

View File

@@ -7,5 +7,5 @@
"integration_type": "service", "integration_type": "service",
"iot_class": "cloud_polling", "iot_class": "cloud_polling",
"loggers": ["aioelectricitymaps"], "loggers": ["aioelectricitymaps"],
"requirements": ["aioelectricitymaps==0.4.0"] "requirements": ["aioelectricitymaps==1.1.1"]
} }

View File

@@ -5,7 +5,7 @@ from __future__ import annotations
from collections.abc import Callable from collections.abc import Callable
from dataclasses import dataclass from dataclasses import dataclass
from aioelectricitymaps.models import CarbonIntensityResponse from aioelectricitymaps import HomeAssistantCarbonIntensityResponse
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
SensorEntity, SensorEntity,
@@ -28,10 +28,10 @@ class CO2SensorEntityDescription(SensorEntityDescription):
# For backwards compat, allow description to override unique ID key to use # For backwards compat, allow description to override unique ID key to use
unique_id: str | None = None unique_id: str | None = None
unit_of_measurement_fn: Callable[[CarbonIntensityResponse], str | None] | None = ( unit_of_measurement_fn: (
None Callable[[HomeAssistantCarbonIntensityResponse], str | None] | None
) ) = None
value_fn: Callable[[CarbonIntensityResponse], float | None] value_fn: Callable[[HomeAssistantCarbonIntensityResponse], float | None]
SENSORS = ( SENSORS = (

2
requirements_all.txt generated
View File

@@ -241,7 +241,7 @@ aioeagle==1.1.0
aioecowitt==2025.3.1 aioecowitt==2025.3.1
# homeassistant.components.co2signal # homeassistant.components.co2signal
aioelectricitymaps==0.4.0 aioelectricitymaps==1.1.1
# homeassistant.components.emonitor # homeassistant.components.emonitor
aioemonitor==1.0.5 aioemonitor==1.0.5

View File

@@ -229,7 +229,7 @@ aioeagle==1.1.0
aioecowitt==2025.3.1 aioecowitt==2025.3.1
# homeassistant.components.co2signal # homeassistant.components.co2signal
aioelectricitymaps==0.4.0 aioelectricitymaps==1.1.1
# homeassistant.components.emonitor # homeassistant.components.emonitor
aioemonitor==1.0.5 aioemonitor==1.0.5

View File

@@ -1,19 +1,19 @@
"""Tests for the CO2 Signal integration.""" """Tests for the CO2 Signal integration."""
from aioelectricitymaps.models import ( from aioelectricitymaps import HomeAssistantCarbonIntensityResponse
CarbonIntensityData, from aioelectricitymaps.models.home_assistant import (
CarbonIntensityResponse, HomeAssistantCarbonIntensityData,
CarbonIntensityUnit, HomeAssistantCarbonIntensityUnit,
) )
VALID_RESPONSE = CarbonIntensityResponse( VALID_RESPONSE = HomeAssistantCarbonIntensityResponse(
status="ok", status="ok",
country_code="FR", country_code="FR",
data=CarbonIntensityData( data=HomeAssistantCarbonIntensityData(
carbon_intensity=45.98623190095805, carbon_intensity=45.98623190095805,
fossil_fuel_percentage=5.461182741937103, fossil_fuel_percentage=5.461182741937103,
), ),
units=CarbonIntensityUnit( units=HomeAssistantCarbonIntensityUnit(
carbon_intensity="gCO2eq/kWh", carbon_intensity="gCO2eq/kWh",
), ),
) )

View File

@@ -30,8 +30,7 @@ def mock_electricity_maps() -> Generator[MagicMock]:
), ),
): ):
client = electricity_maps.return_value client = electricity_maps.return_value
client.latest_carbon_intensity_by_coordinates.return_value = VALID_RESPONSE client.carbon_intensity_for_home_assistant.return_value = VALID_RESPONSE
client.latest_carbon_intensity_by_country_code.return_value = VALID_RESPONSE
yield client yield client

View File

@@ -157,8 +157,7 @@ async def test_form_error_handling(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
) )
electricity_maps.latest_carbon_intensity_by_coordinates.side_effect = side_effect electricity_maps.carbon_intensity_for_home_assistant.side_effect = side_effect
electricity_maps.latest_carbon_intensity_by_country_code.side_effect = side_effect
result = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
@@ -172,8 +171,7 @@ async def test_form_error_handling(
assert result["errors"] == {"base": err_code} assert result["errors"] == {"base": err_code}
# reset mock and test if now succeeds # reset mock and test if now succeeds
electricity_maps.latest_carbon_intensity_by_coordinates.side_effect = None electricity_maps.carbon_intensity_for_home_assistant.side_effect = None
electricity_maps.latest_carbon_intensity_by_country_code.side_effect = None
result = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],

View File

@@ -62,8 +62,7 @@ async def test_sensor_update_fail(
assert state.state == "45.9862319009581" assert state.state == "45.9862319009581"
assert len(electricity_maps.mock_calls) == 1 assert len(electricity_maps.mock_calls) == 1
electricity_maps.latest_carbon_intensity_by_coordinates.side_effect = error electricity_maps.carbon_intensity_for_home_assistant.side_effect = error
electricity_maps.latest_carbon_intensity_by_country_code.side_effect = error
freezer.tick(timedelta(minutes=20)) freezer.tick(timedelta(minutes=20))
async_fire_time_changed(hass) async_fire_time_changed(hass)
@@ -74,8 +73,7 @@ async def test_sensor_update_fail(
assert len(electricity_maps.mock_calls) == 2 assert len(electricity_maps.mock_calls) == 2
# reset mock and test if entity is available again # reset mock and test if entity is available again
electricity_maps.latest_carbon_intensity_by_coordinates.side_effect = None electricity_maps.carbon_intensity_for_home_assistant.side_effect = None
electricity_maps.latest_carbon_intensity_by_country_code.side_effect = None
freezer.tick(timedelta(minutes=20)) freezer.tick(timedelta(minutes=20))
async_fire_time_changed(hass) 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 := hass.states.get("sensor.electricity_maps_co2_intensity"))
assert state.state == "45.9862319009581" assert state.state == "45.9862319009581"
electricity_maps.latest_carbon_intensity_by_coordinates.side_effect = ( electricity_maps.carbon_intensity_for_home_assistant.side_effect = (
ElectricityMapsInvalidTokenError
)
electricity_maps.latest_carbon_intensity_by_country_code.side_effect = (
ElectricityMapsInvalidTokenError ElectricityMapsInvalidTokenError
) )