2021-04-15 20:31:59 +00:00
|
|
|
"""Sensor component that handles additional ClimaCell data for your location."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from pyclimacell.const import CURRENT
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import SensorEntity
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-07-20 04:22:41 +00:00
|
|
|
from homeassistant.const import ATTR_ATTRIBUTION, CONF_API_VERSION, CONF_NAME
|
2021-04-17 10:48:03 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-04-30 18:38:59 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-04-15 20:31:59 +00:00
|
|
|
from homeassistant.util import slugify
|
|
|
|
|
|
|
|
from . import ClimaCellDataUpdateCoordinator, ClimaCellEntity
|
2022-03-19 07:42:22 +00:00
|
|
|
from .const import CC_V3_SENSOR_TYPES, DOMAIN, ClimaCellSensorEntityDescription
|
2021-04-15 20:31:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
2021-04-17 10:48:03 +00:00
|
|
|
hass: HomeAssistant,
|
2021-04-15 20:31:59 +00:00
|
|
|
config_entry: ConfigEntry,
|
2021-04-30 18:38:59 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2021-04-15 20:31:59 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set up a config entry."""
|
|
|
|
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
2022-03-19 07:42:22 +00:00
|
|
|
api_version = config_entry.data[CONF_API_VERSION]
|
2021-04-15 20:31:59 +00:00
|
|
|
entities = [
|
2022-03-19 07:42:22 +00:00
|
|
|
ClimaCellV3SensorEntity(
|
|
|
|
hass, config_entry, coordinator, api_version, description
|
|
|
|
)
|
|
|
|
for description in CC_V3_SENSOR_TYPES
|
2021-04-15 20:31:59 +00:00
|
|
|
]
|
|
|
|
async_add_entities(entities)
|
|
|
|
|
|
|
|
|
2022-03-19 07:42:22 +00:00
|
|
|
class ClimaCellV3SensorEntity(ClimaCellEntity, SensorEntity):
|
|
|
|
"""Sensor entity that talks to ClimaCell v3 API to retrieve non-weather data."""
|
2021-04-15 20:31:59 +00:00
|
|
|
|
2021-07-27 21:47:29 +00:00
|
|
|
entity_description: ClimaCellSensorEntityDescription
|
|
|
|
|
2021-04-15 20:31:59 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2021-07-20 04:22:41 +00:00
|
|
|
hass: HomeAssistant,
|
2021-04-15 20:31:59 +00:00
|
|
|
config_entry: ConfigEntry,
|
|
|
|
coordinator: ClimaCellDataUpdateCoordinator,
|
|
|
|
api_version: int,
|
2021-07-27 21:47:29 +00:00
|
|
|
description: ClimaCellSensorEntityDescription,
|
2021-04-15 20:31:59 +00:00
|
|
|
) -> None:
|
|
|
|
"""Initialize ClimaCell Sensor Entity."""
|
|
|
|
super().__init__(config_entry, coordinator, api_version)
|
2021-07-27 21:47:29 +00:00
|
|
|
self.entity_description = description
|
2021-07-20 04:22:41 +00:00
|
|
|
self._attr_entity_registry_enabled_default = False
|
2021-07-27 21:47:29 +00:00
|
|
|
self._attr_name = f"{self._config_entry.data[CONF_NAME]} - {description.name}"
|
2021-07-20 04:22:41 +00:00
|
|
|
self._attr_unique_id = (
|
2021-07-28 09:50:13 +00:00
|
|
|
f"{self._config_entry.unique_id}_{slugify(description.name)}"
|
2021-07-20 04:22:41 +00:00
|
|
|
)
|
|
|
|
self._attr_extra_state_attributes = {ATTR_ATTRIBUTION: self.attribution}
|
2021-08-11 08:45:05 +00:00
|
|
|
self._attr_native_unit_of_measurement = (
|
2021-07-27 21:47:29 +00:00
|
|
|
description.unit_metric
|
2021-07-20 04:22:41 +00:00
|
|
|
if hass.config.units.is_metric
|
2021-07-27 21:47:29 +00:00
|
|
|
else description.unit_imperial
|
2021-07-20 04:22:41 +00:00
|
|
|
)
|
2021-04-15 20:31:59 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-11 08:45:05 +00:00
|
|
|
def native_value(self) -> str | int | float | None:
|
2021-04-15 20:31:59 +00:00
|
|
|
"""Return the state."""
|
2022-03-19 07:42:22 +00:00
|
|
|
state = self._get_cc_value(
|
|
|
|
self.coordinator.data[CURRENT], self.entity_description.key
|
|
|
|
)
|
2021-04-15 20:31:59 +00:00
|
|
|
if (
|
2021-06-27 19:03:20 +00:00
|
|
|
state is not None
|
2021-12-03 18:08:23 +00:00
|
|
|
and not isinstance(state, str)
|
2021-07-27 21:47:29 +00:00
|
|
|
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
|
2021-04-15 20:31:59 +00:00
|
|
|
):
|
2021-07-27 21:47:29 +00:00
|
|
|
conversion = self.entity_description.metric_conversion
|
2021-06-27 19:03:20 +00:00
|
|
|
# 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)
|
|
|
|
|
2021-07-27 21:47:29 +00:00
|
|
|
if self.entity_description.value_map is not None and state is not None:
|
2021-12-03 18:08:23 +00:00
|
|
|
# mypy bug: "Literal[IntEnum.value]" not callable
|
|
|
|
return self.entity_description.value_map(state).name.lower() # type: ignore[misc]
|
2021-04-15 20:31:59 +00:00
|
|
|
|
2021-06-27 19:03:20 +00:00
|
|
|
return state
|