Files
epenetGitHubcopilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>frenck
d766aae436 Remove import annotations from components (#169536)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: frenck <195327+frenck@users.noreply.github.com>
2026-04-30 21:14:48 +02:00

34 lines
1022 B
Python

"""Helper functions for the CO2 Signal integration."""
from collections.abc import Mapping
from typing import Any
from aioelectricitymaps import (
CoordinatesRequest,
ElectricityMaps,
HomeAssistantCarbonIntensityResponse,
ZoneRequest,
)
from homeassistant.const import CONF_COUNTRY_CODE, CONF_LATITUDE, CONF_LONGITUDE
from homeassistant.core import HomeAssistant
async def fetch_latest_carbon_intensity(
hass: HomeAssistant,
em: ElectricityMaps,
config: Mapping[str, Any],
) -> HomeAssistantCarbonIntensityResponse:
"""Fetch the latest carbon intensity based on zone key or location 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)