core/homeassistant/components/sensibo/number.py

114 lines
3.5 KiB
Python
Raw Normal View History

"""Number platform for Sensibo integration."""
from __future__ import annotations
2022-09-04 19:42:08 +00:00
from collections.abc import Callable
from dataclasses import dataclass
2022-09-04 19:42:08 +00:00
from typing import Any
from pysensibo.model import SensiboDevice
from homeassistant.components.number import NumberEntity, NumberEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
2022-03-05 21:37:44 +00:00
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
2022-03-05 21:37:44 +00:00
from .const import DOMAIN
from .coordinator import SensiboDataUpdateCoordinator
2022-09-04 19:42:08 +00:00
from .entity import SensiboDeviceBaseEntity, async_handle_api_call
2022-05-28 23:26:50 +00:00
PARALLEL_UPDATES = 0
@dataclass
class SensiboEntityDescriptionMixin:
"""Mixin values for Sensibo entities."""
remote_key: str
2022-09-04 19:42:08 +00:00
value_fn: Callable[[SensiboDevice], float | None]
@dataclass
class SensiboNumberEntityDescription(
NumberEntityDescription, SensiboEntityDescriptionMixin
):
"""Class describing Sensibo Number entities."""
2022-05-25 00:40:26 +00:00
DEVICE_NUMBER_TYPES = (
SensiboNumberEntityDescription(
key="calibration_temp",
remote_key="temperature",
name="Temperature calibration",
icon="mdi:thermometer",
entity_category=EntityCategory.CONFIG,
entity_registry_enabled_default=False,
native_min_value=-10,
native_max_value=10,
native_step=0.1,
2022-09-04 19:42:08 +00:00
value_fn=lambda data: data.calibration_temp,
),
SensiboNumberEntityDescription(
key="calibration_hum",
remote_key="humidity",
name="Humidity calibration",
icon="mdi:water",
entity_category=EntityCategory.CONFIG,
entity_registry_enabled_default=False,
native_min_value=-10,
native_max_value=10,
native_step=0.1,
2022-09-04 19:42:08 +00:00
value_fn=lambda data: data.calibration_hum,
),
)
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.parsed.items()
2022-05-25 00:40:26 +00:00
for description in DEVICE_NUMBER_TYPES
)
class SensiboNumber(SensiboDeviceBaseEntity, NumberEntity):
"""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)
self.entity_description = entity_description
self._attr_unique_id = f"{device_id}-{entity_description.key}"
@property
def native_value(self) -> float | None:
"""Return the value from coordinator data."""
2022-09-04 19:42:08 +00:00
return self.entity_description.value_fn(self.device_data)
async def async_set_native_value(self, value: float) -> None:
"""Set value for calibration."""
await self.async_send_api_call(key=self.entity_description.key, value=value)
2022-09-04 19:42:08 +00:00
@async_handle_api_call
async def async_send_api_call(self, key: str, value: Any) -> bool:
2022-09-04 19:42:08 +00:00
"""Make service call to api."""
data = {self.entity_description.remote_key: value}
2022-09-04 19:42:08 +00:00
result = await self._client.async_set_calibration(
self._device_id,
data,
)
return bool(result.get("status") == "success")