2023-02-19 01:58:21 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-04-08 02:44:47 +00:00
|
|
|
import logging
|
|
|
|
|
2023-04-10 05:09:54 +00:00
|
|
|
from pyhon import Hon
|
|
|
|
from pyhon.appliance import HonAppliance
|
2023-04-16 14:59:37 +00:00
|
|
|
from pyhon.parameter.fixed import HonParameterFixed
|
2023-02-19 01:58:21 +00:00
|
|
|
|
|
|
|
from homeassistant.components.select import SelectEntity, SelectEntityDescription
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-03-22 07:25:30 +00:00
|
|
|
from homeassistant.const import UnitOfTemperature, UnitOfTime, REVOLUTIONS_PER_MINUTE
|
2023-02-19 01:58:21 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.entity import EntityCategory
|
|
|
|
|
2023-03-05 20:16:09 +00:00
|
|
|
from .const import DOMAIN
|
2023-03-03 17:23:30 +00:00
|
|
|
from .hon import HonEntity, HonCoordinator
|
|
|
|
|
2023-04-08 02:44:47 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2023-02-19 01:58:21 +00:00
|
|
|
SELECTS = {
|
|
|
|
"WM": (
|
|
|
|
SelectEntityDescription(
|
2023-03-03 17:23:30 +00:00
|
|
|
key="startProgram.spinSpeed",
|
2023-02-19 01:58:21 +00:00
|
|
|
name="Spin speed",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
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
|
|
|
),
|
|
|
|
SelectEntityDescription(
|
2023-03-03 17:23:30 +00:00
|
|
|
key="startProgram.temp",
|
2023-02-19 01:58:21 +00:00
|
|
|
name="Temperature",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
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
|
|
|
),
|
|
|
|
SelectEntityDescription(
|
2023-03-03 17:23:30 +00:00
|
|
|
key="startProgram.program",
|
2023-03-10 23:30:38 +00:00
|
|
|
name="Program",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
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": (
|
|
|
|
SelectEntityDescription(
|
|
|
|
key="startProgram.program",
|
|
|
|
name="Program",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
2023-04-22 19:19:32 +00:00
|
|
|
translation_key="programs_td",
|
2023-03-21 12:00:50 +00:00
|
|
|
),
|
2023-03-22 07:25:30 +00:00
|
|
|
SelectEntityDescription(
|
|
|
|
key="startProgram.dryTimeMM",
|
2023-04-23 14:24:30 +00:00
|
|
|
name="Dry Time",
|
2023-03-22 07:25:30 +00:00
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
icon="mdi:timer",
|
2023-04-10 17:51:16 +00:00
|
|
|
unit_of_measurement=UnitOfTime.MINUTES,
|
2023-04-23 14:24:30 +00:00
|
|
|
translation_key="dry_time",
|
2023-03-22 07:25:30 +00:00
|
|
|
),
|
2023-04-22 21:09:57 +00:00
|
|
|
SelectEntityDescription(
|
|
|
|
key="startProgram.dryLevel",
|
|
|
|
name="Dry level",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
icon="mdi:hair-dryer",
|
|
|
|
translation_key="dry_levels",
|
|
|
|
),
|
2023-04-07 11:52:55 +00:00
|
|
|
),
|
2023-04-08 02:44:47 +00:00
|
|
|
"WD": (
|
|
|
|
SelectEntityDescription(
|
|
|
|
key="startProgram.program",
|
|
|
|
name="Program",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
2023-04-22 19:19:32 +00:00
|
|
|
translation_key="programs_wm",
|
2023-04-08 02:44:47 +00:00
|
|
|
),
|
|
|
|
),
|
2023-04-07 11:52:55 +00:00
|
|
|
"OV": (
|
|
|
|
SelectEntityDescription(
|
|
|
|
key="startProgram.program",
|
|
|
|
name="Program",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
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-04-15 02:27:40 +00:00
|
|
|
SelectEntityDescription(
|
|
|
|
key="startProgram.program",
|
|
|
|
name="Program",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
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": (
|
|
|
|
SelectEntityDescription(
|
|
|
|
key="startProgram.program",
|
|
|
|
name="Program",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
translation_key="programs_dw",
|
|
|
|
),
|
|
|
|
),
|
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:
|
2023-04-15 20:03:50 +00:00
|
|
|
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)
|
2023-04-15 20:03:50 +00:00
|
|
|
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 := SELECTS.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):
|
2023-03-05 18:19:52 +00:00
|
|
|
continue
|
2023-04-10 17:51:16 +00:00
|
|
|
appliances.extend(
|
|
|
|
[HonSelectEntity(hass, coordinator, entry, device, description)]
|
2023-02-19 01:58:21 +00:00
|
|
|
)
|
|
|
|
async_add_entities(appliances)
|
|
|
|
|
|
|
|
|
|
|
|
class HonSelectEntity(HonEntity, SelectEntity):
|
2023-04-10 17:51:16 +00:00
|
|
|
def __init__(
|
|
|
|
self, hass, coordinator, entry, device: HonAppliance, description
|
|
|
|
) -> None:
|
2023-02-19 01:58:21 +00:00
|
|
|
super().__init__(hass, entry, coordinator, device)
|
|
|
|
|
|
|
|
self._coordinator = coordinator
|
|
|
|
self._device = device
|
|
|
|
self.entity_description = description
|
|
|
|
self._attr_unique_id = f"{super().unique_id}{description.key}"
|
|
|
|
|
2023-03-08 22:00:55 +00:00
|
|
|
if not isinstance(self._device.settings[description.key], HonParameterFixed):
|
|
|
|
self._attr_options: list[str] = device.settings[description.key].values
|
2023-02-19 01:58:21 +00:00
|
|
|
else:
|
2023-03-08 22:00:55 +00:00
|
|
|
self._attr_options: list[str] = [device.settings[description.key].value]
|
2023-02-19 01:58:21 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def current_option(self) -> str | None:
|
2023-04-22 21:09:57 +00:00
|
|
|
value = self._device.settings.get(self.entity_description.key)
|
|
|
|
if value is None or value.value not in self._attr_options:
|
2023-02-19 01:58:21 +00:00
|
|
|
return None
|
2023-04-22 21:09:57 +00:00
|
|
|
return value.value
|
2023-02-19 01:58:21 +00:00
|
|
|
|
|
|
|
async def async_select_option(self, option: str) -> None:
|
2023-03-08 22:00:55 +00:00
|
|
|
self._device.settings[self.entity_description.key].value = option
|
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-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] = []
|
|
|
|
self._attr_native_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
|
|
|
|
self._attr_native_value = setting.value
|
2023-02-19 01:58:21 +00:00
|
|
|
self.async_write_ha_state()
|