hOn/custom_components/hon/number.py

194 lines
6.8 KiB
Python
Raw Normal View History

2023-02-19 01:58:21 +00:00
from __future__ import annotations
2023-04-10 05:09:54 +00:00
from pyhon import Hon
2023-04-16 11:55:08 +00:00
from pyhon.parameter.range import HonParameterRange
2023-02-19 01:58:21 +00:00
from homeassistant.components.number import (
NumberEntity,
NumberEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
2023-04-07 11:52:55 +00:00
from homeassistant.const import UnitOfTime, UnitOfTemperature
2023-02-19 01:58:21 +00:00
from homeassistant.core import callback
from homeassistant.helpers.entity import EntityCategory
2023-03-03 17:23:30 +00:00
from .const import DOMAIN
from .hon import HonEntity, HonCoordinator
2023-02-19 01:58:21 +00:00
NUMBERS: dict[str, tuple[NumberEntityDescription, ...]] = {
"WM": (
NumberEntityDescription(
2023-03-03 17:23:30 +00:00
key="startProgram.delayTime",
name="Delay Time",
2023-03-08 22:00:55 +00:00
icon="mdi:timer-plus",
2023-03-03 17:23:30 +00:00
entity_category=EntityCategory.CONFIG,
2023-04-10 17:51:16 +00:00
native_unit_of_measurement=UnitOfTime.MINUTES,
2023-04-23 00:01:14 +00:00
translation_key="delay_time",
2023-02-19 01:58:21 +00:00
),
NumberEntityDescription(
2023-03-03 17:23:30 +00:00
key="startProgram.rinseIterations",
name="Rinse Iterations",
2023-03-08 22:00:55 +00:00
icon="mdi:rotate-right",
2023-04-10 17:51:16 +00:00
entity_category=EntityCategory.CONFIG,
2023-04-23 00:01:14 +00:00
translation_key="rinse_iterations",
2023-02-19 01:58:21 +00:00
),
NumberEntityDescription(
2023-03-03 17:23:30 +00:00
key="startProgram.mainWashTime",
name="Main Wash Time",
2023-03-08 22:00:55 +00:00
icon="mdi:clock-start",
2023-03-03 17:23:30 +00:00
entity_category=EntityCategory.CONFIG,
2023-04-10 17:51:16 +00:00
native_unit_of_measurement=UnitOfTime.MINUTES,
2023-04-23 00:01:14 +00:00
translation_key="wash_time",
2023-02-19 01:58:21 +00:00
),
),
2023-03-21 12:03:39 +00:00
"TD": (
NumberEntityDescription(
key="startProgram.delayTime",
name="Delay time",
icon="mdi:timer-plus",
entity_category=EntityCategory.CONFIG,
2023-04-10 17:51:16 +00:00
native_unit_of_measurement=UnitOfTime.MINUTES,
2023-04-23 00:01:14 +00:00
translation_key="delay_time",
2023-03-21 12:03:39 +00:00
),
NumberEntityDescription(
key="startProgram.tempLevel",
name="Temperature level",
entity_category=EntityCategory.CONFIG,
icon="mdi:thermometer",
2023-04-10 17:51:16 +00:00
translation_key="tumbledryertemplevel",
2023-03-21 12:03:39 +00:00
),
NumberEntityDescription(
key="startProgram.dryTime",
name="Dry Time",
entity_category=EntityCategory.CONFIG,
translation_key="dry_time",
),
2023-03-21 12:03:39 +00:00
),
2023-04-08 02:44:47 +00:00
"WD": (
NumberEntityDescription(
key="startProgram.delayTime",
name="Delay Time",
icon="mdi:timer-plus",
entity_category=EntityCategory.CONFIG,
2023-04-10 17:51:16 +00:00
native_unit_of_measurement=UnitOfTime.MINUTES,
2023-04-23 00:01:14 +00:00
translation_key="delay_time",
2023-04-08 02:44:47 +00:00
),
),
2023-04-07 11:52:55 +00:00
"OV": (
NumberEntityDescription(
key="startProgram.delayTime",
name="Delay time",
icon="mdi:timer-plus",
entity_category=EntityCategory.CONFIG,
2023-04-10 17:51:16 +00:00
native_unit_of_measurement=UnitOfTime.MINUTES,
2023-04-23 00:01:14 +00:00
translation_key="delay_time",
2023-04-07 11:52:55 +00:00
),
NumberEntityDescription(
key="startProgram.tempSel",
name="Target Temperature",
entity_category=EntityCategory.CONFIG,
icon="mdi:thermometer",
2023-04-10 17:51:16 +00:00
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
2023-04-23 00:01:14 +00:00
translation_key="target_temperature",
2023-04-07 11:52:55 +00:00
),
NumberEntityDescription(
key="startProgram.prTime",
name="Program Duration",
entity_category=EntityCategory.CONFIG,
icon="mdi:timelapse",
2023-04-10 17:51:16 +00:00
native_unit_of_measurement=UnitOfTime.MINUTES,
2023-04-23 00:01:14 +00:00
translation_key="program_duration",
2023-04-07 11:52:55 +00:00
),
2023-04-08 02:44:47 +00:00
),
2023-04-16 11:55:08 +00:00
"IH": (
2023-04-15 02:27:40 +00:00
NumberEntityDescription(
key="startProgram.temp",
name="Temperature",
entity_category=EntityCategory.CONFIG,
icon="mdi:thermometer",
2023-04-23 00:01:14 +00:00
translation_key="temperature",
2023-04-15 02:27:40 +00:00
),
NumberEntityDescription(
key="startProgram.powerManagement",
name="Power Management",
entity_category=EntityCategory.CONFIG,
icon="mdi:timelapse",
2023-04-23 00:01:14 +00:00
translation_key="power_management",
2023-04-15 02:27:40 +00:00
),
),
2023-04-16 19:46:17 +00:00
"DW": (
NumberEntityDescription(
key="startProgram.delayTime",
name="Delay time",
icon="mdi:timer-plus",
entity_category=EntityCategory.CONFIG,
native_unit_of_measurement=UnitOfTime.MINUTES,
2023-04-23 00:01:14 +00:00
translation_key="delay_time",
2023-04-16 19:46:17 +00:00
),
NumberEntityDescription(
key="startProgram.waterHard",
name="Water hard",
icon="mdi:water",
entity_category=EntityCategory.CONFIG,
2023-04-23 00:01:14 +00:00
translation_key="water_hard",
2023-04-16 19:46:17 +00:00
),
),
2023-02-19 01:58:21 +00:00
}
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
2023-04-10 05:09:54 +00:00
hon: Hon = hass.data[DOMAIN][entry.unique_id]
2023-02-19 01:58:21 +00:00
coordinators = hass.data[DOMAIN]["coordinators"]
appliances = []
2023-04-10 05:09:54 +00:00
for device in hon.appliances:
if device.unique_id in coordinators:
coordinator = hass.data[DOMAIN]["coordinators"][device.unique_id]
2023-02-19 01:58:21 +00:00
else:
coordinator = HonCoordinator(hass, device)
hass.data[DOMAIN]["coordinators"][device.unique_id] = coordinator
2023-02-19 01:58:21 +00:00
await coordinator.async_config_entry_first_refresh()
2023-03-08 22:00:55 +00:00
if descriptions := NUMBERS.get(device.appliance_type):
2023-02-19 01:58:21 +00:00
for description in descriptions:
2023-04-08 04:15:33 +00:00
if not device.settings.get(description.key):
continue
2023-04-10 17:51:16 +00:00
appliances.extend(
[HonNumberEntity(hass, coordinator, entry, device, description)]
2023-02-19 01:58:21 +00:00
)
async_add_entities(appliances)
class HonNumberEntity(HonEntity, NumberEntity):
def __init__(self, hass, coordinator, entry, device, description) -> None:
super().__init__(hass, entry, coordinator, device)
self._coordinator = coordinator
2023-03-08 22:00:55 +00:00
self._device = device
2023-02-19 01:58:21 +00:00
self._data = device.settings[description.key]
self.entity_description = description
self._attr_unique_id = f"{super().unique_id}{description.key}"
if isinstance(self._data, HonParameterRange):
self._attr_native_max_value = self._data.max
self._attr_native_min_value = self._data.min
self._attr_native_step = self._data.step
@property
def native_value(self) -> float | None:
2023-03-08 22:00:55 +00:00
return self._device.get(self.entity_description.key)
2023-02-19 01:58:21 +00:00
async def async_set_native_value(self, value: float) -> None:
2023-03-08 22:00:55 +00:00
self._device.settings[self.entity_description.key].value = value
2023-04-23 18:04:19 +00:00
await self.coordinator.async_refresh()
2023-02-19 01:58:21 +00:00
@callback
def _handle_coordinator_update(self):
2023-03-08 22:00:55 +00:00
setting = self._device.settings[self.entity_description.key]
if isinstance(setting, HonParameterRange):
self._attr_native_max_value = setting.max
self._attr_native_min_value = setting.min
self._attr_native_step = setting.step
self._attr_native_value = setting.value
2023-02-19 01:58:21 +00:00
self.async_write_ha_state()