Home Assistant Unofficial Reference 2024.12.1
helpers.py
Go to the documentation of this file.
1 """Helper functions for the CO2 Signal integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 from aioelectricitymaps import ElectricityMaps
9 from aioelectricitymaps.models import CarbonIntensityResponse
10 
11 from homeassistant.const import CONF_COUNTRY_CODE, CONF_LATITUDE, CONF_LONGITUDE
12 from homeassistant.core import HomeAssistant
13 
14 
16  hass: HomeAssistant,
17  em: ElectricityMaps,
18  config: Mapping[str, Any],
19 ) -> CarbonIntensityResponse:
20  """Fetch the latest carbon intensity based on country code or location coordinates."""
21  if CONF_COUNTRY_CODE in config:
22  return await em.latest_carbon_intensity_by_country_code(
23  code=config[CONF_COUNTRY_CODE]
24  )
25 
26  return await em.latest_carbon_intensity_by_coordinates(
27  lat=config.get(CONF_LATITUDE, hass.config.latitude),
28  lon=config.get(CONF_LONGITUDE, hass.config.longitude),
29  )
CarbonIntensityResponse fetch_latest_carbon_intensity(HomeAssistant hass, ElectricityMaps em, Mapping[str, Any] config)
Definition: helpers.py:19