1 """Support for the CO2signal platform."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
8 from aioelectricitymaps.models
import CarbonIntensityResponse
12 SensorEntityDescription,
21 from .
import CO2SignalConfigEntry
22 from .const
import ATTRIBUTION, DOMAIN
23 from .coordinator
import CO2SignalCoordinator
26 @dataclass(frozen=True, kw_only=True)
28 """Provide a description of a CO2 sensor."""
31 unique_id: str |
None =
None
32 unit_of_measurement_fn: Callable[[CarbonIntensityResponse], str |
None] |
None = (
35 value_fn: Callable[[CarbonIntensityResponse], float |
None]
40 key=
"carbonIntensity",
41 translation_key=
"carbon_intensity",
42 unique_id=
"co2intensity",
43 value_fn=
lambda response: response.data.carbon_intensity,
44 unit_of_measurement_fn=
lambda response: response.units.carbon_intensity,
47 key=
"fossilFuelPercentage",
48 translation_key=
"fossil_fuel_percentage",
49 native_unit_of_measurement=PERCENTAGE,
50 value_fn=
lambda response: response.data.fossil_fuel_percentage,
57 entry: CO2SignalConfigEntry,
58 async_add_entities: AddEntitiesCallback,
60 """Set up the CO2signal sensor."""
61 coordinator = entry.runtime_data
63 [
CO2Sensor(coordinator, description)
for description
in SENSORS],
False
68 """Implementation of the CO2Signal sensor."""
70 entity_description: CO2SensorEntityDescription
71 _attr_attribution = ATTRIBUTION
72 _attr_has_entity_name =
True
73 _attr_state_class = SensorStateClass.MEASUREMENT
76 self, coordinator: CO2SignalCoordinator, description: CO2SensorEntityDescription
78 """Initialize the sensor."""
83 "country_code": coordinator.data.country_code,
86 configuration_url=
"https://www.electricitymaps.com/",
87 entry_type=DeviceEntryType.SERVICE,
88 identifiers={(DOMAIN, coordinator.entry_id)},
89 manufacturer=
"Electricity Maps",
90 name=
"Electricity Maps",
93 f
"{coordinator.entry_id}_{description.unique_id or description.key}"
98 """Return sensor state."""
103 """Return the unit of measurement."""
105 return self.
entity_descriptionentity_description.unit_of_measurement_fn(self.coordinator.data)
float|None native_value(self)
_attr_extra_state_attributes
str|None native_unit_of_measurement(self)
None __init__(self, CO2SignalCoordinator coordinator, CO2SensorEntityDescription description)
None async_setup_entry(HomeAssistant hass, CO2SignalConfigEntry entry, AddEntitiesCallback async_add_entities)