hOn/custom_components/hon/select.py

137 lines
4.7 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-02-19 02:58:21 +01:00
from pyhon import HonConnection
from pyhon.device import HonDevice
from pyhon.parameter import HonParameterFixed
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
from homeassistant.helpers.entity import EntityCategory
2023-03-05 21:16:09 +01:00
from .const import DOMAIN
2023-03-03 18:23:30 +01:00
from .hon import HonEntity, HonCoordinator
2023-04-08 04:44:47 +02:00
_LOGGER = logging.getLogger(__name__)
2023-02-19 02:58:21 +01:00
SELECTS = {
"WM": (
SelectEntityDescription(
2023-03-03 18:23:30 +01:00
key="startProgram.spinSpeed",
2023-02-19 02:58:21 +01:00
name="Spin speed",
entity_category=EntityCategory.CONFIG,
2023-03-03 18:23:30 +01:00
icon="mdi:numeric",
unit_of_measurement=REVOLUTIONS_PER_MINUTE
2023-02-19 02:58:21 +01:00
),
SelectEntityDescription(
2023-03-03 18:23:30 +01:00
key="startProgram.temp",
2023-02-19 02:58:21 +01:00
name="Temperature",
entity_category=EntityCategory.CONFIG,
2023-03-03 18:23:30 +01:00
icon="mdi:thermometer",
unit_of_measurement=UnitOfTemperature.CELSIUS
2023-02-19 02:58:21 +01:00
),
SelectEntityDescription(
2023-03-03 18:23:30 +01:00
key="startProgram.program",
2023-03-11 00:30:38 +01:00
name="Program",
entity_category=EntityCategory.CONFIG,
translation_key="programs"
2023-02-19 02:58:21 +01:00
),
2023-03-21 13:00:50 +01:00
),
"TD": (
SelectEntityDescription(
key="startProgram.program",
name="Program",
entity_category=EntityCategory.CONFIG,
translation_key="programs"
),
SelectEntityDescription(
key="startProgram.dryTimeMM",
name="Time",
entity_category=EntityCategory.CONFIG,
icon="mdi:timer",
unit_of_measurement=UnitOfTime.MINUTES
),
2023-04-07 13:52:55 +02:00
),
2023-04-08 04:44:47 +02:00
"WD": (
SelectEntityDescription(
key="startProgram.program",
name="Program",
entity_category=EntityCategory.CONFIG,
translation_key="programs"
),
),
2023-04-07 13:52:55 +02:00
"OV": (
SelectEntityDescription(
key="startProgram.program",
name="Program",
entity_category=EntityCategory.CONFIG,
),
SelectEntityDescription(
key="startProgram.preheatStatus",
name="Preheat",
entity_category=EntityCategory.CONFIG
),
2023-04-08 04:44:47 +02:00
),
2023-02-19 02:58:21 +01:00
}
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
hon: HonConnection = hass.data[DOMAIN][entry.unique_id]
coordinators = hass.data[DOMAIN]["coordinators"]
appliances = []
for device in hon.devices:
if device.mac_address in coordinators:
coordinator = hass.data[DOMAIN]["coordinators"][device.mac_address]
else:
coordinator = HonCoordinator(hass, device)
hass.data[DOMAIN]["coordinators"][device.mac_address] = coordinator
await coordinator.async_config_entry_first_refresh()
2023-03-08 23:00:55 +01:00
if descriptions := SELECTS.get(device.appliance_type):
2023-02-19 02:58:21 +01:00
for description in descriptions:
2023-04-08 06:15:33 +02:00
if not device.settings.get(description.key):
2023-03-05 19:19:52 +01:00
continue
2023-02-19 02:58:21 +01:00
appliances.extend([
HonSelectEntity(hass, coordinator, entry, device, description)]
)
async_add_entities(appliances)
class HonSelectEntity(HonEntity, SelectEntity):
def __init__(self, hass, coordinator, entry, device: HonDevice, description) -> None:
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 23:00:55 +01:00
if not isinstance(self._device.settings[description.key], HonParameterFixed):
self._attr_options: list[str] = device.settings[description.key].values
2023-02-19 02:58:21 +01:00
else:
2023-03-08 23:00:55 +01:00
self._attr_options: list[str] = [device.settings[description.key].value]
2023-02-19 02:58:21 +01:00
@property
def current_option(self) -> str | None:
2023-03-08 23:00:55 +01:00
value = self._device.settings[self.entity_description.key].value
2023-02-19 02:58:21 +01:00
if value is None or value not in self._attr_options:
return None
return value
async def async_select_option(self, option: str) -> None:
2023-03-08 23:00:55 +01:00
self._device.settings[self.entity_description.key].value = option
2023-02-19 02:58:21 +01:00
await self.coordinator.async_request_refresh()
@callback
def _handle_coordinator_update(self):
2023-03-08 23:00:55 +01:00
setting = self._device.settings[self.entity_description.key]
if not isinstance(self._device.settings[self.entity_description.key], HonParameterFixed):
self._attr_options: list[str] = setting.values
2023-02-19 02:58:21 +01:00
else:
2023-03-08 23:00:55 +01:00
self._attr_options = [setting.value]
self._attr_native_value = setting.value
2023-02-19 02:58:21 +01:00
self.async_write_ha_state()