mirror of
https://github.com/home-assistant/core.git
synced 2025-08-30 01:42:21 +02:00
Bump aioelectricitymaps to v1.1.1 (#150928)
This commit is contained in:
committed by
GitHub
parent
8c7e9bcf7c
commit
efce6c8468
@@ -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:
|
||||
|
@@ -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)
|
||||
|
@@ -7,5 +7,5 @@
|
||||
"integration_type": "service",
|
||||
"iot_class": "cloud_polling",
|
||||
"loggers": ["aioelectricitymaps"],
|
||||
"requirements": ["aioelectricitymaps==0.4.0"]
|
||||
"requirements": ["aioelectricitymaps==1.1.1"]
|
||||
}
|
||||
|
@@ -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 = (
|
||||
|
2
requirements_all.txt
generated
2
requirements_all.txt
generated
@@ -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
|
||||
|
2
requirements_test_all.txt
generated
2
requirements_test_all.txt
generated
@@ -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
|
||||
|
@@ -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",
|
||||
),
|
||||
)
|
||||
|
@@ -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
|
||||
|
||||
|
@@ -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"],
|
||||
|
@@ -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
|
||||
)
|
||||
|
||||
|
Reference in New Issue
Block a user