hOn/custom_components/hon/select.py

239 lines
8 KiB
Python
Raw Normal View History

2023-02-19 01:58:21 +00:00
from __future__ import annotations
2023-04-08 02:44:47 +00:00
import logging
2023-05-27 22:30:08 +00:00
from dataclasses import dataclass
from typing import Dict, List
2023-02-19 01:58:21 +00: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 01:58:21 +00:00
from homeassistant.core import callback
2023-06-08 18:01:55 +00:00
from homeassistant.helpers.entity import EntityCategory
2023-05-24 22:52:54 +00:00
from pyhon.appliance import HonAppliance
from . import const
2023-03-05 20:16:09 +00:00
from .const import DOMAIN
2023-05-24 23:30:33 +00:00
from .hon import HonEntity, unique_entities
2023-03-03 17:23:30 +00:00
2023-04-08 02:44:47 +00:00
_LOGGER = logging.getLogger(__name__)
2023-05-27 22:30:08 +00:00
@dataclass
class HonSelectEntityDescription(SelectEntityDescription):
option_list: Dict[str, str] = None
2023-05-27 22:30:08 +00:00
@dataclass
class HonConfigSelectEntityDescription(SelectEntityDescription):
entity_category: EntityCategory = EntityCategory.CONFIG
option_list: Dict[str, str] = None
2023-05-27 22:30:08 +00:00
2023-02-19 01:58:21 +00:00
SELECTS = {
"WM": (
2023-05-27 22:30:08 +00:00
HonConfigSelectEntityDescription(
2023-03-03 17:23:30 +00:00
key="startProgram.spinSpeed",
2023-02-19 01:58:21 +00:00
name="Spin speed",
2023-03-03 17:23:30 +00:00
icon="mdi:numeric",
2023-04-10 17:51:16 +00:00
unit_of_measurement=REVOLUTIONS_PER_MINUTE,
2023-04-23 00:01:14 +00:00
translation_key="spin_speed",
2023-02-19 01:58:21 +00:00
),
2023-05-27 22:30:08 +00:00
HonConfigSelectEntityDescription(
2023-03-03 17:23:30 +00:00
key="startProgram.temp",
2023-02-19 01:58:21 +00:00
name="Temperature",
2023-03-03 17:23:30 +00:00
icon="mdi:thermometer",
2023-04-10 17:51:16 +00:00
unit_of_measurement=UnitOfTemperature.CELSIUS,
2023-04-23 00:01:14 +00:00
translation_key="temperature",
2023-02-19 01:58:21 +00:00
),
2023-05-27 22:30:08 +00:00
HonConfigSelectEntityDescription(
2023-03-03 17:23:30 +00:00
key="startProgram.program",
2023-03-10 23:30:38 +00:00
name="Program",
2023-04-22 19:19:32 +00:00
translation_key="programs_wm",
2023-02-19 01:58:21 +00:00
),
2023-03-21 12:00:50 +00:00
),
"TD": (
2023-05-27 22:30:08 +00:00
HonConfigSelectEntityDescription(
2023-03-21 12:00:50 +00:00
key="startProgram.program",
name="Program",
2023-04-22 19:19:32 +00:00
translation_key="programs_td",
2023-03-21 12:00:50 +00:00
),
2023-05-27 22:30:08 +00:00
HonConfigSelectEntityDescription(
key="startProgram.dryTimeMM",
name="Dry Time",
icon="mdi:timer",
2023-04-10 17:51:16 +00:00
unit_of_measurement=UnitOfTime.MINUTES,
translation_key="dry_time",
),
2023-05-27 22:30:08 +00:00
HonConfigSelectEntityDescription(
2023-04-22 21:09:57 +00: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 21:09:57 +00:00
),
2023-04-07 11:52:55 +00:00
),
"OV": (
2023-05-27 22:30:08 +00:00
HonConfigSelectEntityDescription(
2023-04-07 11:52:55 +00:00
key="startProgram.program",
name="Program",
2023-04-22 19:19:32 +00:00
translation_key="programs_ov",
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-05-27 22:30:08 +00:00
HonConfigSelectEntityDescription(
2023-04-15 02:27:40 +00:00
key="startProgram.program",
name="Program",
2023-04-22 19:19:32 +00:00
translation_key="programs_ih",
2023-04-15 02:27:40 +00:00
),
),
2023-04-16 19:46:17 +00:00
"DW": (
2023-05-27 22:30:08 +00:00
HonConfigSelectEntityDescription(
2023-04-16 19:46:17 +00:00
key="startProgram.program",
name="Program",
translation_key="programs_dw",
),
2023-05-27 22:30:08 +00:00
HonConfigSelectEntityDescription(
2023-05-14 01:17:58 +00:00
key="startProgram.temp",
name="Temperature",
icon="mdi:thermometer",
unit_of_measurement=UnitOfTemperature.CELSIUS,
translation_key="temperature",
),
2023-05-27 22:30:08 +00:00
HonConfigSelectEntityDescription(
2023-05-14 01:17:58 +00:00
key="startProgram.remainingTime",
name="Remaining Time",
icon="mdi:timer",
unit_of_measurement=UnitOfTime.MINUTES,
translation_key="remaining_time",
),
2023-04-16 19:46:17 +00:00
),
2023-04-26 21:57:44 +00:00
"AC": (
2023-05-27 22:30:08 +00:00
HonSelectEntityDescription(
2023-04-26 21:57:44 +00:00
key="startProgram.program",
name="Program",
translation_key="programs_ac",
),
2023-05-27 22:30:08 +00:00
HonSelectEntityDescription(
2023-05-08 00:05:04 +00:00
key="settings.humanSensingStatus",
2023-04-26 21:57:44 +00:00
name="Eco Pilot",
2023-05-08 00:05:04 +00:00
icon="mdi:run",
2023-04-26 21:57:44 +00:00
translation_key="eco_pilot",
option_list=const.AC_HUMAN_SENSE,
2023-04-26 21:57:44 +00:00
),
),
2023-05-10 16:13:05 +00:00
"REF": (
2023-05-27 22:30:08 +00:00
HonConfigSelectEntityDescription(
2023-05-10 16:13:05 +00:00
key="startProgram.program",
name="Program",
translation_key="programs_ref",
),
2023-05-27 22:30:08 +00:00
HonConfigSelectEntityDescription(
2023-05-21 18:51:20 +00:00
key="startProgram.zone",
name="Zone",
icon="mdi:radiobox-marked",
translation_key="ref_zones",
),
2023-05-10 16:13:05 +00:00
),
2023-02-19 01:58:21 +00:00
}
2023-05-07 14:39:45 +00:00
SELECTS["WD"] = unique_entities(SELECTS["WM"], SELECTS["TD"])
2023-02-19 01:58:21 +00:00
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
2023-05-24 23:30:33 +00:00
entities = []
for device in hass.data[DOMAIN][entry.unique_id].appliances:
for description in SELECTS.get(device.appliance_type, []):
if description.key not in device.available_settings:
continue
2023-05-27 22:30:08 +00: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-24 23:30:33 +00:00
await entity.coordinator.async_config_entry_first_refresh()
entities.append(entity)
async_add_entities(entities)
2023-02-19 01:58:21 +00:00
class HonSelectEntity(HonEntity, SelectEntity):
2023-05-27 22:30:08 +00:00
entity_description: HonSelectEntityDescription
2023-02-19 01:58:21 +00:00
2023-05-27 22:30:08 +00:00
def __init__(self, hass, entry, device: HonAppliance, description) -> None:
super().__init__(hass, entry, device, description)
2023-02-19 01:58:21 +00:00
@property
def current_option(self) -> str | None:
2023-06-15 21:52:56 +00:00
if not (setting := self._device.settings.get(self.entity_description.key)):
2023-02-19 01:58:21 +00:00
return None
2023-06-15 21:52:56 +00:00
value = setting.value
if self.entity_description.option_list:
value = self.entity_description.option_list.get(str(value), value)
if value not in self._attr_options:
return None
return value
2023-02-19 01:58:21 +00:00
async def async_select_option(self, option: str) -> None:
2023-06-15 21:52:56 +00:00
setting = self._device.settings[self.entity_description.key]
if (options := self.entity_description.option_list) is not None:
setting.value = next(
(k for k, v in options.items() if k in setting.values and v == option),
option,
)
else:
setting.value = option
2023-05-27 22:30:08 +00:00
command = self.entity_description.key.split(".")[0]
await self._device.commands[command].send()
2023-04-23 18:04:19 +00:00
await self.coordinator.async_refresh()
2023-02-19 01:58:21 +00:00
@callback
2023-06-08 18:01:55 +00:00
def _handle_coordinator_update(self, update=True) -> None:
2023-04-22 21:09:57 +00:00
setting = self._device.settings.get(self.entity_description.key)
if setting is None:
self._attr_available = False
self._attr_options: List[str] = []
value = None
2023-02-19 01:58:21 +00:00
else:
2023-04-22 21:09:57 +00:00
self._attr_available = True
self._attr_options: List[str] = setting.values
2023-06-15 21:52:56 +00:00
value = str(setting.value)
if self.entity_description.option_list is not None:
self._attr_options = [
self.entity_description.option_list.get(k, k)
for k in self._attr_options
]
if value is not None:
value = self.entity_description.option_list.get(value, value)
self._attr_native_value = value
2023-06-08 18:01:55 +00:00
if update:
self.async_write_ha_state()
@property
def available(self) -> bool:
"""Return True if entity is available."""
2023-05-27 22:30:08 +00:00
return (
super().available
and self._device.get("remoteCtrValid", "1") == "1"
and self._device.get("attributes.lastConnEvent.category") != "DISCONNECTED"
)
class HonConfigSelectEntity(HonSelectEntity):
entity_description: HonConfigSelectEntityDescription
async def async_select_option(self, option: str) -> None:
2023-06-15 21:52:56 +00:00
setting = self._device.settings[self.entity_description.key]
if (options := self.entity_description.option_list) is not None:
setting.value = next(
(k for k, v in options.items() if k in setting.values and v == option),
option,
)
else:
setting.value = option
2023-05-27 22:30:08 +00:00
await self.coordinator.async_refresh()
@property
def available(self) -> bool:
"""Return True if entity is available."""
return super(SelectEntity, self).available