mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Use EntityDescription - climacell (#53573)
* Use EntityDescription - climacell * Fix tests * Fix coverage ignore comment
This commit is contained in:
@@ -14,7 +14,12 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.util import slugify
|
||||
|
||||
from . import ClimaCellDataUpdateCoordinator, ClimaCellEntity
|
||||
from .const import CC_SENSOR_TYPES, CC_V3_SENSOR_TYPES, DOMAIN, ClimaCellSensorMetadata
|
||||
from .const import (
|
||||
CC_SENSOR_TYPES,
|
||||
CC_V3_SENSOR_TYPES,
|
||||
DOMAIN,
|
||||
ClimaCellSensorEntityDescription,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -35,8 +40,8 @@ async def async_setup_entry(
|
||||
api_class = ClimaCellSensorEntity
|
||||
sensor_types = CC_SENSOR_TYPES
|
||||
entities = [
|
||||
api_class(hass, config_entry, coordinator, api_version, sensor_type)
|
||||
for sensor_type in sensor_types
|
||||
api_class(hass, config_entry, coordinator, api_version, description)
|
||||
for description in sensor_types
|
||||
]
|
||||
async_add_entities(entities)
|
||||
|
||||
@@ -44,30 +49,29 @@ async def async_setup_entry(
|
||||
class BaseClimaCellSensorEntity(ClimaCellEntity, SensorEntity):
|
||||
"""Base ClimaCell sensor entity."""
|
||||
|
||||
entity_description: ClimaCellSensorEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
coordinator: ClimaCellDataUpdateCoordinator,
|
||||
api_version: int,
|
||||
sensor_type: ClimaCellSensorMetadata,
|
||||
description: ClimaCellSensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize ClimaCell Sensor Entity."""
|
||||
super().__init__(config_entry, coordinator, api_version)
|
||||
self.sensor_type = sensor_type
|
||||
self._attr_device_class = self.sensor_type.device_class
|
||||
self.entity_description = description
|
||||
self._attr_entity_registry_enabled_default = False
|
||||
self._attr_name = (
|
||||
f"{self._config_entry.data[CONF_NAME]} - {self.sensor_type.name}"
|
||||
)
|
||||
self._attr_name = f"{self._config_entry.data[CONF_NAME]} - {description.name}"
|
||||
self._attr_unique_id = (
|
||||
f"{self._config_entry.unique_id}_{slugify(self.sensor_type.name)}"
|
||||
f"{self._config_entry.unique_id}_{slugify(description.name_)}"
|
||||
)
|
||||
self._attr_extra_state_attributes = {ATTR_ATTRIBUTION: self.attribution}
|
||||
self._attr_unit_of_measurement = (
|
||||
self.sensor_type.unit_metric
|
||||
description.unit_metric
|
||||
if hass.config.units.is_metric
|
||||
else self.sensor_type.unit_imperial
|
||||
else description.unit_imperial
|
||||
)
|
||||
|
||||
@property
|
||||
@@ -81,20 +85,21 @@ class BaseClimaCellSensorEntity(ClimaCellEntity, SensorEntity):
|
||||
state = self._state
|
||||
if (
|
||||
state is not None
|
||||
and self.sensor_type.unit_imperial is not None
|
||||
and self.sensor_type.metric_conversion != 1.0
|
||||
and self.sensor_type.is_metric_check is not None
|
||||
and self.hass.config.units.is_metric == self.sensor_type.is_metric_check
|
||||
and self.entity_description.unit_imperial is not None
|
||||
and self.entity_description.metric_conversion != 1.0
|
||||
and self.entity_description.is_metric_check is not None
|
||||
and self.hass.config.units.is_metric
|
||||
== self.entity_description.is_metric_check
|
||||
):
|
||||
conversion = self.sensor_type.metric_conversion
|
||||
conversion = self.entity_description.metric_conversion
|
||||
# When conversion is a callable, we assume it's a single input function
|
||||
if callable(conversion):
|
||||
return round(conversion(state), 4)
|
||||
|
||||
return round(state * conversion, 4)
|
||||
|
||||
if self.sensor_type.value_map is not None and state is not None:
|
||||
return self.sensor_type.value_map(state).name.lower()
|
||||
if self.entity_description.value_map is not None and state is not None:
|
||||
return self.entity_description.value_map(state).name.lower()
|
||||
|
||||
return state
|
||||
|
||||
@@ -105,7 +110,7 @@ class ClimaCellSensorEntity(BaseClimaCellSensorEntity):
|
||||
@property
|
||||
def _state(self) -> str | int | float | None:
|
||||
"""Return the raw state."""
|
||||
return self._get_current_property(self.sensor_type.field)
|
||||
return self._get_current_property(self.entity_description.key)
|
||||
|
||||
|
||||
class ClimaCellV3SensorEntity(BaseClimaCellSensorEntity):
|
||||
@@ -115,5 +120,5 @@ class ClimaCellV3SensorEntity(BaseClimaCellSensorEntity):
|
||||
def _state(self) -> str | int | float | None:
|
||||
"""Return the raw state."""
|
||||
return self._get_cc_value(
|
||||
self.coordinator.data[CURRENT], self.sensor_type.field
|
||||
self.coordinator.data[CURRENT], self.entity_description.key
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user