hOn/custom_components/hon/select.py

224 lines
7.4 KiB
Python
Raw Normal View History

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