Migrate CO2 Signal to new entity naming style (#74696)
parent
3aff5fd2e6
commit
1cf8b76124
|
@ -15,7 +15,6 @@ from homeassistant.exceptions import ConfigEntryAuthFailed, HomeAssistantError
|
|||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
|
||||
from .const import CONF_COUNTRY_CODE, DOMAIN
|
||||
from .util import get_extra_name
|
||||
|
||||
PLATFORMS = [Platform.SENSOR]
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -73,10 +72,6 @@ class CO2SignalCoordinator(DataUpdateCoordinator[CO2SignalResponse]):
|
|||
"""Return entry ID."""
|
||||
return self._entry.entry_id
|
||||
|
||||
def get_extra_name(self) -> str | None:
|
||||
"""Return the extra name describing the location if not home."""
|
||||
return get_extra_name(self._entry.data)
|
||||
|
||||
async def _async_update_data(self) -> CO2SignalResponse:
|
||||
"""Fetch the latest data from the source."""
|
||||
try:
|
||||
|
|
|
@ -5,14 +5,17 @@ from dataclasses import dataclass
|
|||
from datetime import timedelta
|
||||
from typing import cast
|
||||
|
||||
from homeassistant.components.sensor import SensorEntity, SensorStateClass
|
||||
from homeassistant.components.sensor import (
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import ATTR_ATTRIBUTION, PERCENTAGE
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.device_registry import DeviceEntryType
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import StateType
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from . import CO2SignalCoordinator
|
||||
|
@ -22,12 +25,9 @@ SCAN_INTERVAL = timedelta(minutes=3)
|
|||
|
||||
|
||||
@dataclass
|
||||
class CO2SensorEntityDescription:
|
||||
class CO2SensorEntityDescription(SensorEntityDescription):
|
||||
"""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
|
||||
|
||||
|
@ -42,7 +42,7 @@ SENSORS = (
|
|||
CO2SensorEntityDescription(
|
||||
key="fossilFuelPercentage",
|
||||
name="Grid fossil fuel percentage",
|
||||
unit_of_measurement=PERCENTAGE,
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
),
|
||||
)
|
||||
|
||||
|
@ -58,21 +58,18 @@ async def async_setup_entry(
|
|||
class CO2Sensor(CoordinatorEntity[CO2SignalCoordinator], SensorEntity):
|
||||
"""Implementation of the CO2Signal sensor."""
|
||||
|
||||
_attr_state_class = SensorStateClass.MEASUREMENT
|
||||
entity_description: CO2SensorEntityDescription
|
||||
_attr_has_entity_name = True
|
||||
_attr_icon = "mdi:molecule-co2"
|
||||
_attr_state_class = SensorStateClass.MEASUREMENT
|
||||
|
||||
def __init__(
|
||||
self, coordinator: CO2SignalCoordinator, description: CO2SensorEntityDescription
|
||||
) -> None:
|
||||
"""Initialize the sensor."""
|
||||
super().__init__(coordinator)
|
||||
self._description = description
|
||||
self.entity_description = description
|
||||
|
||||
name = description.name
|
||||
if extra_name := coordinator.get_extra_name():
|
||||
name = f"{extra_name} - {name}"
|
||||
|
||||
self._attr_name = name
|
||||
self._attr_extra_state_attributes = {
|
||||
"country_code": coordinator.data["countryCode"],
|
||||
ATTR_ATTRIBUTION: ATTRIBUTION,
|
||||
|
@ -92,19 +89,22 @@ class CO2Sensor(CoordinatorEntity[CO2SignalCoordinator], SensorEntity):
|
|||
def available(self) -> bool:
|
||||
"""Return True if entity is available."""
|
||||
return (
|
||||
super().available and self._description.key in self.coordinator.data["data"]
|
||||
super().available
|
||||
and self.entity_description.key in self.coordinator.data["data"]
|
||||
)
|
||||
|
||||
@property
|
||||
def native_value(self) -> StateType:
|
||||
def native_value(self) -> float | None:
|
||||
"""Return sensor state."""
|
||||
if (value := self.coordinator.data["data"][self._description.key]) is None: # type: ignore[literal-required]
|
||||
if (value := self.coordinator.data["data"][self.entity_description.key]) is None: # type: ignore[literal-required]
|
||||
return None
|
||||
return round(value, 2)
|
||||
|
||||
@property
|
||||
def native_unit_of_measurement(self) -> str | None:
|
||||
"""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))
|
||||
if self.entity_description.unit_of_measurement:
|
||||
return self.entity_description.unit_of_measurement
|
||||
return cast(
|
||||
str, self.coordinator.data["units"].get(self.entity_description.key)
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue