2022-02-21 21:50:50 +00:00
|
|
|
"""Number platform for Sensibo integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
|
|
from homeassistant.components.number import NumberEntity, NumberEntityDescription
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2022-03-05 21:37:44 +00:00
|
|
|
from homeassistant.helpers.entity import EntityCategory
|
2022-02-21 21:50:50 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
2022-03-05 21:37:44 +00:00
|
|
|
from .const import DOMAIN
|
2022-02-21 21:50:50 +00:00
|
|
|
from .coordinator import SensiboDataUpdateCoordinator
|
2022-03-05 21:37:44 +00:00
|
|
|
from .entity import SensiboBaseEntity
|
2022-02-21 21:50:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class SensiboEntityDescriptionMixin:
|
|
|
|
"""Mixin values for Sensibo entities."""
|
|
|
|
|
|
|
|
remote_key: str
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class SensiboNumberEntityDescription(
|
|
|
|
NumberEntityDescription, SensiboEntityDescriptionMixin
|
|
|
|
):
|
|
|
|
"""Class describing Sensibo Number entities."""
|
|
|
|
|
|
|
|
|
|
|
|
NUMBER_TYPES = (
|
|
|
|
SensiboNumberEntityDescription(
|
|
|
|
key="calibration_temp",
|
|
|
|
remote_key="temperature",
|
|
|
|
name="Temperature calibration",
|
|
|
|
icon="mdi:thermometer",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
min_value=-10,
|
|
|
|
max_value=10,
|
|
|
|
step=0.1,
|
|
|
|
),
|
|
|
|
SensiboNumberEntityDescription(
|
|
|
|
key="calibration_hum",
|
|
|
|
remote_key="humidity",
|
|
|
|
name="Humidity calibration",
|
|
|
|
icon="mdi:water",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
min_value=-10,
|
|
|
|
max_value=10,
|
|
|
|
step=0.1,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
|
|
|
"""Set up Sensibo number platform."""
|
|
|
|
|
|
|
|
coordinator: SensiboDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
|
|
|
|
async_add_entities(
|
|
|
|
SensiboNumber(coordinator, device_id, description)
|
|
|
|
for device_id, device_data in coordinator.data.items()
|
|
|
|
for description in NUMBER_TYPES
|
|
|
|
if device_data["hvac_modes"] and device_data["temp"]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-03-05 21:37:44 +00:00
|
|
|
class SensiboNumber(SensiboBaseEntity, NumberEntity):
|
2022-02-21 21:50:50 +00:00
|
|
|
"""Representation of a Sensibo numbers."""
|
|
|
|
|
|
|
|
entity_description: SensiboNumberEntityDescription
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: SensiboDataUpdateCoordinator,
|
|
|
|
device_id: str,
|
|
|
|
entity_description: SensiboNumberEntityDescription,
|
|
|
|
) -> None:
|
|
|
|
"""Initiate Sensibo Number."""
|
2022-03-05 21:37:44 +00:00
|
|
|
super().__init__(coordinator, device_id)
|
2022-02-21 21:50:50 +00:00
|
|
|
self.entity_description = entity_description
|
|
|
|
self._attr_unique_id = f"{device_id}-{entity_description.key}"
|
|
|
|
self._attr_name = (
|
|
|
|
f"{coordinator.data[device_id]['name']} {entity_description.name}"
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def value(self) -> float:
|
|
|
|
"""Return the value from coordinator data."""
|
|
|
|
return self.coordinator.data[self._device_id][self.entity_description.key]
|
|
|
|
|
|
|
|
async def async_set_value(self, value: float) -> None:
|
|
|
|
"""Set value for calibration."""
|
|
|
|
data = {self.entity_description.remote_key: value}
|
2022-03-05 21:37:44 +00:00
|
|
|
result = await self.async_send_command("set_calibration", {"data": data})
|
2022-02-21 21:50:50 +00:00
|
|
|
if result["status"] == "success":
|
|
|
|
self.coordinator.data[self._device_id][self.entity_description.key] = value
|
|
|
|
self.async_write_ha_state()
|
|
|
|
return
|
|
|
|
raise HomeAssistantError(f"Could not set calibration for device {self.name}")
|