2019-04-03 15:40:03 +00:00
|
|
|
"""Support for the CO2signal platform."""
|
2021-07-23 21:35:11 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from dataclasses import dataclass
|
2021-07-14 17:58:02 +00:00
|
|
|
from datetime import timedelta
|
2021-07-23 21:35:11 +00:00
|
|
|
from typing import cast
|
2019-01-26 18:02:46 +00:00
|
|
|
|
2021-12-20 12:49:15 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity, SensorStateClass
|
2022-01-03 18:16:42 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-20 12:49:15 +00:00
|
|
|
from homeassistant.const import ATTR_ATTRIBUTION, PERCENTAGE
|
2022-01-03 18:16:42 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-11-22 17:14:15 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceEntryType
|
2021-10-22 15:40:13 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
2022-01-03 18:16:42 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-07-23 21:35:11 +00:00
|
|
|
from homeassistant.helpers.typing import StateType
|
2022-03-21 09:22:30 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2019-01-26 18:02:46 +00:00
|
|
|
|
2022-03-21 09:22:30 +00:00
|
|
|
from . import CO2SignalCoordinator
|
2021-12-20 12:49:15 +00:00
|
|
|
from .const import ATTRIBUTION, DOMAIN
|
2019-01-26 18:02:46 +00:00
|
|
|
|
2021-07-14 17:58:02 +00:00
|
|
|
SCAN_INTERVAL = timedelta(minutes=3)
|
2019-01-26 18:02:46 +00:00
|
|
|
|
|
|
|
|
2021-07-23 21:35:11 +00:00
|
|
|
@dataclass
|
|
|
|
class CO2SensorEntityDescription:
|
|
|
|
"""Provide a description of a CO2 sensor."""
|
|
|
|
|
|
|
|
key: str
|
|
|
|
name: str
|
|
|
|
unit_of_measurement: str | None = None
|
|
|
|
# For backwards compat, allow description to override unique ID key to use
|
|
|
|
unique_id: str | None = None
|
|
|
|
|
|
|
|
|
|
|
|
SENSORS = (
|
|
|
|
CO2SensorEntityDescription(
|
|
|
|
key="carbonIntensity",
|
|
|
|
name="CO2 intensity",
|
|
|
|
unique_id="co2intensity",
|
|
|
|
# No unit, it's extracted from response.
|
|
|
|
),
|
|
|
|
CO2SensorEntityDescription(
|
|
|
|
key="fossilFuelPercentage",
|
|
|
|
name="Grid fossil fuel percentage",
|
|
|
|
unit_of_measurement=PERCENTAGE,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-01-03 18:16:42 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2021-07-20 04:30:00 +00:00
|
|
|
"""Set up the CO2signal sensor."""
|
2021-07-23 21:35:11 +00:00
|
|
|
coordinator: CO2SignalCoordinator = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
async_add_entities(CO2Sensor(coordinator, description) for description in SENSORS)
|
2019-01-26 18:02:46 +00:00
|
|
|
|
|
|
|
|
2022-03-21 09:22:30 +00:00
|
|
|
class CO2Sensor(CoordinatorEntity[CO2SignalCoordinator], SensorEntity):
|
2019-01-26 18:02:46 +00:00
|
|
|
"""Implementation of the CO2Signal sensor."""
|
|
|
|
|
2021-12-09 08:22:02 +00:00
|
|
|
_attr_state_class = SensorStateClass.MEASUREMENT
|
2021-05-31 08:50:11 +00:00
|
|
|
_attr_icon = "mdi:molecule-co2"
|
|
|
|
|
2021-07-23 21:35:11 +00:00
|
|
|
def __init__(
|
|
|
|
self, coordinator: CO2SignalCoordinator, description: CO2SensorEntityDescription
|
|
|
|
) -> None:
|
2019-01-26 18:02:46 +00:00
|
|
|
"""Initialize the sensor."""
|
2021-07-23 21:35:11 +00:00
|
|
|
super().__init__(coordinator)
|
|
|
|
self._description = description
|
|
|
|
|
|
|
|
name = description.name
|
|
|
|
if extra_name := coordinator.get_extra_name():
|
|
|
|
name = f"{extra_name} - {name}"
|
|
|
|
|
2021-07-20 04:30:00 +00:00
|
|
|
self._attr_name = name
|
2021-07-26 18:10:39 +00:00
|
|
|
self._attr_extra_state_attributes = {
|
|
|
|
"country_code": coordinator.data["countryCode"],
|
|
|
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
|
|
|
}
|
2021-10-22 15:40:13 +00:00
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
configuration_url="https://www.electricitymap.org/",
|
2021-11-22 17:14:15 +00:00
|
|
|
entry_type=DeviceEntryType.SERVICE,
|
2021-10-22 15:40:13 +00:00
|
|
|
identifiers={(DOMAIN, coordinator.entry_id)},
|
|
|
|
manufacturer="Tmrow.com",
|
|
|
|
name="CO2 signal",
|
|
|
|
)
|
2021-07-23 21:35:11 +00:00
|
|
|
self._attr_unique_id = (
|
|
|
|
f"{coordinator.entry_id}_{description.unique_id or description.key}"
|
2021-07-20 04:30:00 +00:00
|
|
|
)
|
2021-07-23 21:35:11 +00:00
|
|
|
|
2021-07-28 06:43:24 +00:00
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return True if entity is available."""
|
|
|
|
return (
|
2022-03-02 00:06:36 +00:00
|
|
|
super().available and self._description.key in self.coordinator.data["data"]
|
2021-07-28 06:43:24 +00:00
|
|
|
)
|
|
|
|
|
2021-07-23 21:35:11 +00:00
|
|
|
@property
|
2021-08-11 08:45:05 +00:00
|
|
|
def native_value(self) -> StateType:
|
2021-07-23 21:35:11 +00:00
|
|
|
"""Return sensor state."""
|
2022-03-11 23:57:38 +00:00
|
|
|
if (value := self.coordinator.data["data"][self._description.key]) is None: # type: ignore[literal-required]
|
2022-03-02 00:06:36 +00:00
|
|
|
return None
|
|
|
|
return round(value, 2)
|
2021-07-23 21:35:11 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-11 08:45:05 +00:00
|
|
|
def native_unit_of_measurement(self) -> str | None:
|
2021-07-23 21:35:11 +00:00
|
|
|
"""Return the unit of measurement."""
|
|
|
|
if self._description.unit_of_measurement:
|
|
|
|
return self._description.unit_of_measurement
|
|
|
|
return cast(str, self.coordinator.data["units"].get(self._description.key))
|