mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
Migrate CO2 Signal to new entity naming style (#74696)
This commit is contained in:
parent
3aff5fd2e6
commit
1cf8b76124
@ -15,7 +15,6 @@ from homeassistant.exceptions import ConfigEntryAuthFailed, HomeAssistantError
|
|||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
|
|
||||||
from .const import CONF_COUNTRY_CODE, DOMAIN
|
from .const import CONF_COUNTRY_CODE, DOMAIN
|
||||||
from .util import get_extra_name
|
|
||||||
|
|
||||||
PLATFORMS = [Platform.SENSOR]
|
PLATFORMS = [Platform.SENSOR]
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -73,10 +72,6 @@ class CO2SignalCoordinator(DataUpdateCoordinator[CO2SignalResponse]):
|
|||||||
"""Return entry ID."""
|
"""Return entry ID."""
|
||||||
return self._entry.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:
|
async def _async_update_data(self) -> CO2SignalResponse:
|
||||||
"""Fetch the latest data from the source."""
|
"""Fetch the latest data from the source."""
|
||||||
try:
|
try:
|
||||||
|
@ -5,14 +5,17 @@ from dataclasses import dataclass
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from typing import cast
|
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.config_entries import ConfigEntry
|
||||||
from homeassistant.const import ATTR_ATTRIBUTION, PERCENTAGE
|
from homeassistant.const import ATTR_ATTRIBUTION, PERCENTAGE
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.device_registry import DeviceEntryType
|
from homeassistant.helpers.device_registry import DeviceEntryType
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import StateType
|
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from . import CO2SignalCoordinator
|
from . import CO2SignalCoordinator
|
||||||
@ -22,12 +25,9 @@ SCAN_INTERVAL = timedelta(minutes=3)
|
|||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class CO2SensorEntityDescription:
|
class CO2SensorEntityDescription(SensorEntityDescription):
|
||||||
"""Provide a description of a CO2 sensor."""
|
"""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
|
# For backwards compat, allow description to override unique ID key to use
|
||||||
unique_id: str | None = None
|
unique_id: str | None = None
|
||||||
|
|
||||||
@ -42,7 +42,7 @@ SENSORS = (
|
|||||||
CO2SensorEntityDescription(
|
CO2SensorEntityDescription(
|
||||||
key="fossilFuelPercentage",
|
key="fossilFuelPercentage",
|
||||||
name="Grid fossil fuel percentage",
|
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):
|
class CO2Sensor(CoordinatorEntity[CO2SignalCoordinator], SensorEntity):
|
||||||
"""Implementation of the CO2Signal sensor."""
|
"""Implementation of the CO2Signal sensor."""
|
||||||
|
|
||||||
_attr_state_class = SensorStateClass.MEASUREMENT
|
entity_description: CO2SensorEntityDescription
|
||||||
|
_attr_has_entity_name = True
|
||||||
_attr_icon = "mdi:molecule-co2"
|
_attr_icon = "mdi:molecule-co2"
|
||||||
|
_attr_state_class = SensorStateClass.MEASUREMENT
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, coordinator: CO2SignalCoordinator, description: CO2SensorEntityDescription
|
self, coordinator: CO2SignalCoordinator, description: CO2SensorEntityDescription
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(coordinator)
|
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 = {
|
self._attr_extra_state_attributes = {
|
||||||
"country_code": coordinator.data["countryCode"],
|
"country_code": coordinator.data["countryCode"],
|
||||||
ATTR_ATTRIBUTION: ATTRIBUTION,
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
||||||
@ -92,19 +89,22 @@ class CO2Sensor(CoordinatorEntity[CO2SignalCoordinator], SensorEntity):
|
|||||||
def available(self) -> bool:
|
def available(self) -> bool:
|
||||||
"""Return True if entity is available."""
|
"""Return True if entity is available."""
|
||||||
return (
|
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
|
@property
|
||||||
def native_value(self) -> StateType:
|
def native_value(self) -> float | None:
|
||||||
"""Return sensor state."""
|
"""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 None
|
||||||
return round(value, 2)
|
return round(value, 2)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_unit_of_measurement(self) -> str | None:
|
def native_unit_of_measurement(self) -> str | None:
|
||||||
"""Return the unit of measurement."""
|
"""Return the unit of measurement."""
|
||||||
if self._description.unit_of_measurement:
|
if self.entity_description.unit_of_measurement:
|
||||||
return self._description.unit_of_measurement
|
return self.entity_description.unit_of_measurement
|
||||||
return cast(str, self.coordinator.data["units"].get(self._description.key))
|
return cast(
|
||||||
|
str, self.coordinator.data["units"].get(self.entity_description.key)
|
||||||
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user