hOn/custom_components/hon/number.py

248 lines
8.7 KiB
Python
Raw Normal View History

2023-02-19 02:58:21 +01:00
from __future__ import annotations
from homeassistant.components.number import (
NumberEntity,
NumberEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
2023-04-07 13:52:55 +02:00
from homeassistant.const import UnitOfTime, UnitOfTemperature
2023-02-19 02:58:21 +01:00
from homeassistant.core import callback
from homeassistant.helpers.entity import EntityCategory
2023-05-10 18:13:05 +02:00
from pyhon import Hon
from pyhon.parameter.base import HonParameter
from pyhon.parameter.fixed import HonParameterFixed
from pyhon.parameter.range import HonParameterRange
2023-03-03 18:23:30 +01:00
from .const import DOMAIN
2023-05-07 16:39:45 +02:00
from .hon import HonEntity, HonCoordinator, unique_entities
2023-02-19 02:58:21 +01:00
NUMBERS: dict[str, tuple[NumberEntityDescription, ...]] = {
"WM": (
NumberEntityDescription(
2023-03-03 18:23:30 +01:00
key="startProgram.delayTime",
name="Delay Time",
2023-03-08 23:00:55 +01:00
icon="mdi:timer-plus",
2023-03-03 18:23:30 +01:00
entity_category=EntityCategory.CONFIG,
2023-04-10 19:51:16 +02:00
native_unit_of_measurement=UnitOfTime.MINUTES,
2023-04-23 02:01:14 +02:00
translation_key="delay_time",
2023-02-19 02:58:21 +01:00
),
NumberEntityDescription(
2023-03-03 18:23:30 +01:00
key="startProgram.rinseIterations",
name="Rinse Iterations",
2023-03-08 23:00:55 +01:00
icon="mdi:rotate-right",
2023-04-10 19:51:16 +02:00
entity_category=EntityCategory.CONFIG,
2023-04-23 02:01:14 +02:00
translation_key="rinse_iterations",
2023-02-19 02:58:21 +01:00
),
NumberEntityDescription(
2023-03-03 18:23:30 +01:00
key="startProgram.mainWashTime",
name="Main Wash Time",
2023-03-08 23:00:55 +01:00
icon="mdi:clock-start",
2023-03-03 18:23:30 +01:00
entity_category=EntityCategory.CONFIG,
2023-04-10 19:51:16 +02:00
native_unit_of_measurement=UnitOfTime.MINUTES,
2023-04-23 02:01:14 +02:00
translation_key="wash_time",
2023-02-19 02:58:21 +01:00
),
NumberEntityDescription(
key="startProgram.steamLevel",
name="Steam Level",
icon="mdi:weather-dust",
entity_category=EntityCategory.CONFIG,
translation_key="steam_level",
),
NumberEntityDescription(
key="startProgram.waterHard",
name="Water hard",
icon="mdi:water",
entity_category=EntityCategory.CONFIG,
translation_key="water_hard",
),
NumberEntityDescription(
key="startProgram.lang",
name="lang",
entity_category=EntityCategory.CONFIG,
),
2023-02-19 02:58:21 +01:00
),
2023-03-21 13:03:39 +01:00
"TD": (
NumberEntityDescription(
key="startProgram.delayTime",
name="Delay time",
icon="mdi:timer-plus",
entity_category=EntityCategory.CONFIG,
2023-04-10 19:51:16 +02:00
native_unit_of_measurement=UnitOfTime.MINUTES,
2023-04-23 02:01:14 +02:00
translation_key="delay_time",
2023-03-21 13:03:39 +01:00
),
NumberEntityDescription(
key="startProgram.tempLevel",
name="Temperature level",
entity_category=EntityCategory.CONFIG,
icon="mdi:thermometer",
2023-04-10 19:51:16 +02:00
translation_key="tumbledryertemplevel",
2023-03-21 13:03:39 +01:00
),
NumberEntityDescription(
key="startProgram.dryTime",
name="Dry Time",
entity_category=EntityCategory.CONFIG,
translation_key="dry_time",
),
2023-03-21 13:03:39 +01:00
),
2023-04-07 13:52:55 +02:00
"OV": (
NumberEntityDescription(
key="startProgram.delayTime",
name="Delay time",
icon="mdi:timer-plus",
entity_category=EntityCategory.CONFIG,
2023-04-10 19:51:16 +02:00
native_unit_of_measurement=UnitOfTime.MINUTES,
2023-04-23 02:01:14 +02:00
translation_key="delay_time",
2023-04-07 13:52:55 +02:00
),
NumberEntityDescription(
key="startProgram.tempSel",
name="Target Temperature",
entity_category=EntityCategory.CONFIG,
icon="mdi:thermometer",
2023-04-10 19:51:16 +02:00
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
2023-04-23 02:01:14 +02:00
translation_key="target_temperature",
2023-04-07 13:52:55 +02:00
),
NumberEntityDescription(
key="startProgram.prTime",
name="Program Duration",
entity_category=EntityCategory.CONFIG,
icon="mdi:timelapse",
2023-04-10 19:51:16 +02:00
native_unit_of_measurement=UnitOfTime.MINUTES,
2023-04-23 02:01:14 +02:00
translation_key="program_duration",
2023-04-07 13:52:55 +02:00
),
2023-04-08 04:44:47 +02:00
),
2023-04-16 13:55:08 +02:00
"IH": (
2023-04-15 04:27:40 +02:00
NumberEntityDescription(
key="startProgram.temp",
name="Temperature",
entity_category=EntityCategory.CONFIG,
icon="mdi:thermometer",
2023-04-23 02:01:14 +02:00
translation_key="temperature",
2023-04-15 04:27:40 +02:00
),
NumberEntityDescription(
key="startProgram.powerManagement",
name="Power Management",
entity_category=EntityCategory.CONFIG,
icon="mdi:timelapse",
2023-04-23 02:01:14 +02:00
translation_key="power_management",
2023-04-15 04:27:40 +02:00
),
),
2023-04-16 21:46:17 +02: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 02:01:14 +02:00
translation_key="delay_time",
2023-04-16 21:46:17 +02:00
),
NumberEntityDescription(
key="startProgram.waterHard",
name="Water hard",
icon="mdi:water",
entity_category=EntityCategory.CONFIG,
2023-04-23 02:01:14 +02:00
translation_key="water_hard",
2023-04-16 21:46:17 +02:00
),
),
2023-04-26 23:57:44 +02:00
"AC": (
NumberEntityDescription(
2023-05-08 02:05:04 +02:00
key="settings.tempSel",
2023-04-26 23:57:44 +02:00
name="Target Temperature",
icon="mdi:thermometer",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
translation_key="target_temperature",
),
),
2023-05-15 00:38:41 +02:00
"REF": (
NumberEntityDescription(
key="settings.tempSelZ1",
name="Fridge Temperature",
icon="mdi:thermometer",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
translation_key="fridge_temp_sel",
),
NumberEntityDescription(
key="settings.tempSelZ2",
name="Freezer Temperature",
icon="mdi:thermometer",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
translation_key="freezer_temp_sel",
),
),
2023-02-19 02:58:21 +01:00
}
2023-05-07 16:39:45 +02:00
NUMBERS["WD"] = unique_entities(NUMBERS["WM"], NUMBERS["TD"])
2023-02-19 02:58:21 +01:00
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
2023-04-10 07:09:54 +02:00
hon: Hon = hass.data[DOMAIN][entry.unique_id]
2023-02-19 02:58:21 +01:00
coordinators = hass.data[DOMAIN]["coordinators"]
appliances = []
2023-04-10 07:09:54 +02:00
for device in hon.appliances:
if device.unique_id in coordinators:
coordinator = hass.data[DOMAIN]["coordinators"][device.unique_id]
2023-02-19 02:58:21 +01:00
else:
coordinator = HonCoordinator(hass, device)
hass.data[DOMAIN]["coordinators"][device.unique_id] = coordinator
2023-02-19 02:58:21 +01:00
await coordinator.async_config_entry_first_refresh()
2023-03-08 23:00:55 +01:00
if descriptions := NUMBERS.get(device.appliance_type):
2023-02-19 02:58:21 +01:00
for description in descriptions:
2023-05-07 00:52:54 +02:00
if description.key not in device.available_settings:
2023-04-08 06:15:33 +02:00
continue
2023-04-10 19:51:16 +02:00
appliances.extend(
[HonNumberEntity(hass, coordinator, entry, device, description)]
2023-02-19 02:58:21 +01: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._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 23:00:55 +01:00
return self._device.get(self.entity_description.key)
2023-02-19 02:58:21 +01:00
async def async_set_native_value(self, value: float) -> None:
setting = self._device.settings[self.entity_description.key]
if not (
isinstance(setting, HonParameter) or isinstance(setting, HonParameterFixed)
):
setting.value = value
2023-05-14 22:39:34 +02:00
if "settings." in self.entity_description.key:
await self._device.commands["settings"].send()
2023-04-23 20:04:19 +02:00
await self.coordinator.async_refresh()
2023-02-19 02:58:21 +01:00
@callback
def _handle_coordinator_update(self):
2023-03-08 23:00:55 +01: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 02:58:21 +01:00
self.async_write_ha_state()
@property
def available(self) -> bool:
"""Return True if entity is available."""
if self.entity_category == EntityCategory.CONFIG:
return super().available
else:
return (
super().available
and self._device.get("remoteCtrValid", "1") == "1"
and self._device.get("attributes.lastConnEvent.category")
!= "DISCONNECTED"
)