hOn/custom_components/hon/select.py

338 lines
11 KiB
Python
Raw Normal View History

2023-02-19 02:58:21 +01:00
from __future__ import annotations
2023-04-08 04:44:47 +02:00
import logging
2023-05-28 00:30:08 +02:00
from dataclasses import dataclass
2023-02-19 02:58:21 +01:00
from homeassistant.components.select import SelectEntity, SelectEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfTemperature, UnitOfTime, REVOLUTIONS_PER_MINUTE
2023-02-19 02:58:21 +01:00
from homeassistant.core import callback
2023-06-08 20:01:55 +02:00
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import HomeAssistantType
2023-05-25 00:52:54 +02:00
from . import const
2023-03-05 21:16:09 +01:00
from .const import DOMAIN
2024-03-29 01:22:44 +01:00
from .entity import HonEntity
from .util import unique_entities, get_readable
2023-03-03 18:23:30 +01:00
2023-04-08 04:44:47 +02:00
_LOGGER = logging.getLogger(__name__)
2023-05-28 00:30:08 +02:00
2024-01-11 00:41:49 +01:00
@dataclass(frozen=True)
2023-05-28 00:30:08 +02:00
class HonSelectEntityDescription(SelectEntityDescription):
option_list: dict[int, str] | None = None
2023-05-28 00:30:08 +02:00
2024-01-11 00:41:49 +01:00
@dataclass(frozen=True)
2023-05-28 00:30:08 +02:00
class HonConfigSelectEntityDescription(SelectEntityDescription):
entity_category: EntityCategory = EntityCategory.CONFIG
option_list: dict[int, str] | None = None
2023-05-28 00:30:08 +02:00
SELECTS: dict[str, tuple[SelectEntityDescription, ...]] = {
2023-02-19 02:58:21 +01:00
"WM": (
2023-05-28 00:30:08 +02:00
HonConfigSelectEntityDescription(
2023-03-03 18:23:30 +01:00
key="startProgram.spinSpeed",
2023-02-19 02:58:21 +01:00
name="Spin speed",
2023-03-03 18:23:30 +01:00
icon="mdi:numeric",
2023-04-10 19:51:16 +02:00
unit_of_measurement=REVOLUTIONS_PER_MINUTE,
2023-04-23 02:01:14 +02:00
translation_key="spin_speed",
2023-02-19 02:58:21 +01:00
),
2023-05-28 00:30:08 +02:00
HonConfigSelectEntityDescription(
2023-03-03 18:23:30 +01:00
key="startProgram.temp",
2023-02-19 02:58:21 +01:00
name="Temperature",
2023-03-03 18:23:30 +01:00
icon="mdi:thermometer",
2023-04-10 19:51:16 +02:00
unit_of_measurement=UnitOfTemperature.CELSIUS,
2023-04-23 02:01:14 +02:00
translation_key="temperature",
2023-02-19 02:58:21 +01:00
),
2023-05-28 00:30:08 +02:00
HonConfigSelectEntityDescription(
2023-03-03 18:23:30 +01:00
key="startProgram.program",
2023-03-11 00:30:38 +01:00
name="Program",
2023-04-22 21:19:32 +02:00
translation_key="programs_wm",
2023-02-19 02:58:21 +01:00
),
2023-07-10 00:20:35 +02:00
HonConfigSelectEntityDescription(
key="startProgram.steamLevel",
name="Steam level",
icon="mdi:weather-dust",
translation_key="steam_level",
option_list=const.STEAM_LEVEL,
),
2023-07-10 00:48:57 +02:00
HonConfigSelectEntityDescription(
key="startProgram.dirtyLevel",
name="Dirty level",
icon="mdi:liquid-spot",
translation_key="dirt_level",
option_list=const.DIRTY_LEVEL,
),
2023-10-02 03:33:45 +02:00
HonConfigSelectEntityDescription(
key="startProgram.extendedStainType",
name="Stain Type",
icon="mdi:liquid-spot",
translation_key="stain_type",
),
2023-03-21 13:00:50 +01:00
),
"TD": (
2023-05-28 00:30:08 +02:00
HonConfigSelectEntityDescription(
2023-03-21 13:00:50 +01:00
key="startProgram.program",
name="Program",
2023-04-22 21:19:32 +02:00
translation_key="programs_td",
2023-03-21 13:00:50 +01:00
),
2023-05-28 00:30:08 +02:00
HonConfigSelectEntityDescription(
key="startProgram.dryTimeMM",
name="Dry Time",
icon="mdi:timer",
2023-04-10 19:51:16 +02:00
unit_of_measurement=UnitOfTime.MINUTES,
translation_key="dry_time",
),
2023-05-28 00:30:08 +02:00
HonConfigSelectEntityDescription(
2023-04-22 23:09:57 +02:00
key="startProgram.dryLevel",
name="Dry level",
icon="mdi:hair-dryer",
translation_key="dry_levels",
option_list=const.TUMBLE_DRYER_DRY_LEVEL,
2023-04-22 23:09:57 +02:00
),
2023-04-07 13:52:55 +02:00
),
"OV": (
2023-05-28 00:30:08 +02:00
HonConfigSelectEntityDescription(
2023-04-07 13:52:55 +02:00
key="startProgram.program",
name="Program",
2023-04-22 21:19:32 +02:00
translation_key="programs_ov",
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-05-28 00:30:08 +02:00
HonConfigSelectEntityDescription(
2023-04-15 04:27:40 +02:00
key="startProgram.program",
name="Program",
2023-04-22 21:19:32 +02:00
translation_key="programs_ih",
2023-04-15 04:27:40 +02:00
),
),
2023-04-16 21:46:17 +02:00
"DW": (
2023-05-28 00:30:08 +02:00
HonConfigSelectEntityDescription(
2023-04-16 21:46:17 +02:00
key="startProgram.program",
name="Program",
translation_key="programs_dw",
),
2023-05-28 00:30:08 +02:00
HonConfigSelectEntityDescription(
2023-05-14 03:17:58 +02:00
key="startProgram.temp",
name="Temperature",
icon="mdi:thermometer",
unit_of_measurement=UnitOfTemperature.CELSIUS,
translation_key="temperature",
),
2023-05-28 00:30:08 +02:00
HonConfigSelectEntityDescription(
2023-05-14 03:17:58 +02:00
key="startProgram.remainingTime",
name="Remaining Time",
icon="mdi:timer",
unit_of_measurement=UnitOfTime.MINUTES,
translation_key="remaining_time",
),
2023-04-16 21:46:17 +02:00
),
2023-04-26 23:57:44 +02:00
"AC": (
2023-05-28 00:30:08 +02:00
HonSelectEntityDescription(
2023-04-26 23:57:44 +02:00
key="startProgram.program",
name="Program",
translation_key="programs_ac",
),
2023-05-28 00:30:08 +02:00
HonSelectEntityDescription(
2023-05-08 02:05:04 +02:00
key="settings.humanSensingStatus",
2023-04-26 23:57:44 +02:00
name="Eco Pilot",
2023-05-08 02:05:04 +02:00
icon="mdi:run",
2023-04-26 23:57:44 +02:00
translation_key="eco_pilot",
option_list=const.AC_HUMAN_SENSE,
2023-04-26 23:57:44 +02:00
),
HonSelectEntityDescription(
key="settings.windDirectionHorizontal",
name="Fan Direction Horizontal",
icon="mdi:fan",
translation_key="fan_horizontal",
option_list=const.AC_POSITION_HORIZONTAL,
),
HonSelectEntityDescription(
key="settings.windDirectionVertical",
name="Fan Direction Vertical",
icon="mdi:fan",
translation_key="fan_vertical",
option_list=const.AC_POSITION_VERTICAL,
),
2023-04-26 23:57:44 +02:00
),
2023-05-10 18:13:05 +02:00
"REF": (
2023-05-28 00:30:08 +02:00
HonConfigSelectEntityDescription(
2023-05-10 18:13:05 +02:00
key="startProgram.program",
name="Program",
translation_key="programs_ref",
),
2023-05-28 00:30:08 +02:00
HonConfigSelectEntityDescription(
2023-05-21 20:51:20 +02:00
key="startProgram.zone",
name="Zone",
icon="mdi:radiobox-marked",
translation_key="ref_zones",
),
2023-05-10 18:13:05 +02:00
),
2023-06-21 00:59:00 +02:00
"AP": (
HonSelectEntityDescription(
key="settings.aromaStatus",
name="Diffuser Level",
option_list=const.AP_DIFFUSER_LEVEL,
2023-07-01 01:45:32 +02:00
translation_key="diffuser",
icon="mdi:air-purifier",
2023-06-21 00:59:00 +02:00
),
HonSelectEntityDescription(
key="settings.machMode",
name="Mode",
2023-07-10 00:19:43 +02:00
icon="mdi:play",
2023-06-21 00:59:00 +02:00
option_list=const.AP_MACH_MODE,
2023-07-01 01:45:32 +02:00
translation_key="mode",
2023-06-21 00:59:00 +02:00
),
),
2024-03-17 23:54:50 +01:00
"FRE": (
HonConfigSelectEntityDescription(
key="startProgram.program",
name="Program",
translation_key="programs_ref",
),
HonConfigSelectEntityDescription(
key="startProgram.zone",
name="Zone",
icon="mdi:radiobox-marked",
translation_key="ref_zones",
),
HonSelectEntityDescription(
key="settings.tempSelZ3",
name="Temperature",
icon="mdi:thermometer",
unit_of_measurement=UnitOfTemperature.CELSIUS,
translation_key="temperature",
),
),
2023-02-19 02:58:21 +01:00
}
2023-05-07 16:39:45 +02:00
SELECTS["WD"] = unique_entities(SELECTS["WM"], SELECTS["TD"])
2023-02-19 02:58:21 +01:00
async def async_setup_entry(
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
2023-05-25 01:30:33 +02:00
entities = []
entity: HonSelectEntity | HonConfigSelectEntity
2024-03-29 01:22:44 +01:00
for device in hass.data[DOMAIN][entry.unique_id]["hon"].appliances:
2023-05-25 01:30:33 +02:00
for description in SELECTS.get(device.appliance_type, []):
if description.key not in device.available_settings:
continue
2023-05-28 00:30:08 +02:00
if isinstance(description, HonSelectEntityDescription):
entity = HonSelectEntity(hass, entry, device, description)
elif isinstance(description, HonConfigSelectEntityDescription):
entity = HonConfigSelectEntity(hass, entry, device, description)
else:
continue
2023-05-25 01:30:33 +02:00
entities.append(entity)
async_add_entities(entities)
2023-02-19 02:58:21 +01:00
2023-06-21 00:23:49 +02:00
class HonConfigSelectEntity(HonEntity, SelectEntity):
entity_description: HonConfigSelectEntityDescription
2023-02-19 02:58:21 +01:00
@property
def current_option(self) -> str | None:
2023-06-15 23:52:56 +02:00
if not (setting := self._device.settings.get(self.entity_description.key)):
2023-02-19 02:58:21 +01:00
return None
2023-06-22 13:18:45 +02:00
value = get_readable(self.entity_description, setting.value)
2023-06-15 23:52:56 +02:00
if value not in self._attr_options:
return None
return str(value)
2023-02-19 02:58:21 +01:00
2023-06-21 00:23:49 +02:00
@property
def options(self) -> list[str]:
setting = self._device.settings.get(self.entity_description.key)
if setting is None:
return []
return [
str(get_readable(self.entity_description, key)) for key in setting.values
]
2023-06-21 00:23:49 +02:00
def _option_to_number(self, option: str, values: list[str]) -> str:
2023-06-15 23:52:56 +02:00
if (options := self.entity_description.option_list) is not None:
2023-07-10 00:21:45 +02:00
return str(
next(
(k for k, v in options.items() if str(k) in values and v == option),
option,
)
2023-06-15 23:52:56 +02:00
)
2023-06-21 00:23:49 +02:00
return option
2023-02-19 02:58:21 +01:00
async def async_select_option(self, option: str) -> None:
2023-06-21 00:23:49 +02:00
setting = self._device.settings[self.entity_description.key]
setting.value = self._option_to_number(option, setting.values)
2024-03-29 01:22:44 +01:00
self.async_write_ha_state()
2023-02-19 02:58:21 +01:00
@callback
def _handle_coordinator_update(self, update: bool = True) -> None:
2023-06-21 00:23:49 +02:00
self._attr_available = self.available
self._attr_options = self.options
self._attr_current_option = self.current_option
2023-06-08 20:01:55 +02:00
if update:
self.async_write_ha_state()
@property
def available(self) -> bool:
"""Return True if entity is available."""
2023-06-21 00:23:49 +02:00
return self._device.settings.get(self.entity_description.key) is not None
2023-05-28 00:30:08 +02:00
class HonSelectEntity(HonEntity, SelectEntity):
2023-06-21 00:23:49 +02:00
entity_description: HonSelectEntityDescription
2023-05-28 00:30:08 +02:00
@property
def current_option(self) -> str | None:
if not (setting := self._device.settings.get(self.entity_description.key)):
return None
value = get_readable(self.entity_description, setting.value)
if value not in self._attr_options:
return None
return str(value)
@property
def options(self) -> list[str]:
setting = self._device.settings.get(self.entity_description.key)
if setting is None:
return []
return [
str(get_readable(self.entity_description, key)) for key in setting.values
]
def _option_to_number(self, option: str, values: list[str]) -> str:
if (options := self.entity_description.option_list) is not None:
return str(
next(
(k for k, v in options.items() if str(k) in values and v == option),
option,
)
)
return option
2023-05-28 00:30:08 +02:00
async def async_select_option(self, option: str) -> None:
2023-06-15 23:52:56 +02:00
setting = self._device.settings[self.entity_description.key]
2023-06-21 00:23:49 +02:00
setting.value = self._option_to_number(option, setting.values)
2023-06-21 19:52:32 +02:00
command = self.entity_description.key.split(".")[0]
await self._device.commands[command].send()
if command != "settings":
self._device.sync_command(command, "settings")
2024-03-29 01:22:44 +01:00
self.async_write_ha_state()
2023-05-28 00:30:08 +02:00
@property
def available(self) -> bool:
"""Return True if entity is available."""
2023-06-21 00:23:49 +02:00
return (
super().available
2023-06-22 13:18:45 +02:00
and int(self._device.get("remoteCtrValid", 1)) == 1
2024-03-29 14:48:35 +01:00
and self._device.connection
2023-06-21 00:23:49 +02:00
)
@callback
def _handle_coordinator_update(self, update: bool = True) -> None:
self._attr_available = self.available
self._attr_options = self.options
self._attr_current_option = self.current_option
if update:
self.async_write_ha_state()