2021-10-26 15:53:13 +00:00
|
|
|
"""Support for sensors."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-05-17 06:32:28 +00:00
|
|
|
from fjaraskupan import Device
|
2021-10-26 15:53:13 +00:00
|
|
|
|
|
|
|
from homeassistant.components.number import NumberEntity
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-10 13:53:58 +00:00
|
|
|
from homeassistant.const import TIME_MINUTES
|
2021-10-26 15:53:13 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-12-10 13:53:58 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo, Entity, EntityCategory
|
2021-10-26 15:53:13 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2022-05-17 06:32:28 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2021-10-26 15:53:13 +00:00
|
|
|
|
2022-05-17 06:32:28 +00:00
|
|
|
from . import Coordinator, async_setup_entry_platform
|
2021-10-26 15:53:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2021-10-31 10:47:25 +00:00
|
|
|
"""Set up number entities dynamically through discovery."""
|
2021-10-26 15:53:13 +00:00
|
|
|
|
2022-05-17 06:32:28 +00:00
|
|
|
def _constructor(coordinator: Coordinator) -> list[Entity]:
|
2021-10-26 15:53:13 +00:00
|
|
|
return [
|
|
|
|
PeriodicVentingTime(
|
2022-05-17 06:32:28 +00:00
|
|
|
coordinator, coordinator.device, coordinator.device_info
|
2021-10-26 15:53:13 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
async_setup_entry_platform(hass, config_entry, async_add_entities, _constructor)
|
|
|
|
|
|
|
|
|
2022-05-17 06:32:28 +00:00
|
|
|
class PeriodicVentingTime(CoordinatorEntity[Coordinator], NumberEntity):
|
2021-10-26 15:53:13 +00:00
|
|
|
"""Periodic Venting."""
|
|
|
|
|
2022-07-09 17:46:53 +00:00
|
|
|
_attr_has_entity_name = True
|
|
|
|
|
2022-06-14 18:16:36 +00:00
|
|
|
_attr_native_max_value: float = 59
|
|
|
|
_attr_native_min_value: float = 0
|
|
|
|
_attr_native_step: float = 1
|
2021-12-10 13:53:58 +00:00
|
|
|
_attr_entity_category = EntityCategory.CONFIG
|
2022-06-14 18:16:36 +00:00
|
|
|
_attr_native_unit_of_measurement = TIME_MINUTES
|
2021-10-26 15:53:13 +00:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2022-05-17 06:32:28 +00:00
|
|
|
coordinator: Coordinator,
|
2021-10-26 15:53:13 +00:00
|
|
|
device: Device,
|
|
|
|
device_info: DeviceInfo,
|
|
|
|
) -> None:
|
2021-10-31 10:47:25 +00:00
|
|
|
"""Init number entities."""
|
2021-10-26 15:53:13 +00:00
|
|
|
super().__init__(coordinator)
|
|
|
|
self._device = device
|
|
|
|
self._attr_unique_id = f"{device.address}-periodic-venting"
|
|
|
|
self._attr_device_info = device_info
|
2022-07-09 17:46:53 +00:00
|
|
|
self._attr_name = "Periodic venting"
|
2021-10-26 15:53:13 +00:00
|
|
|
|
|
|
|
@property
|
2022-06-14 18:16:36 +00:00
|
|
|
def native_value(self) -> float | None:
|
2021-10-26 15:53:13 +00:00
|
|
|
"""Return the entity value to represent the entity state."""
|
|
|
|
if data := self.coordinator.data:
|
|
|
|
return data.periodic_venting
|
|
|
|
return None
|
|
|
|
|
2022-06-14 18:16:36 +00:00
|
|
|
async def async_set_native_value(self, value: float) -> None:
|
2021-10-26 15:53:13 +00:00
|
|
|
"""Set new value."""
|
|
|
|
await self._device.send_periodic_venting(int(value))
|
|
|
|
self.coordinator.async_set_updated_data(self._device.state)
|