mirror of https://github.com/Andre0512/hon.git
Refactor select entity
This commit is contained in:
parent
d83179a9fa
commit
a181359faa
|
@ -9,7 +9,6 @@ from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import UnitOfTemperature, UnitOfTime, REVOLUTIONS_PER_MINUTE
|
from homeassistant.const import UnitOfTemperature, UnitOfTime, REVOLUTIONS_PER_MINUTE
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.helpers.entity import EntityCategory
|
from homeassistant.helpers.entity import EntityCategory
|
||||||
from pyhon.appliance import HonAppliance
|
|
||||||
|
|
||||||
from . import const
|
from . import const
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
@ -156,11 +155,8 @@ async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> Non
|
||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class HonSelectEntity(HonEntity, SelectEntity):
|
class HonConfigSelectEntity(HonEntity, SelectEntity):
|
||||||
entity_description: HonSelectEntityDescription
|
entity_description: HonConfigSelectEntityDescription
|
||||||
|
|
||||||
def __init__(self, hass, entry, device: HonAppliance, description) -> None:
|
|
||||||
super().__init__(hass, entry, device, description)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def current_option(self) -> str | None:
|
def current_option(self) -> str | None:
|
||||||
|
@ -173,41 +169,50 @@ class HonSelectEntity(HonEntity, SelectEntity):
|
||||||
return None
|
return None
|
||||||
return value
|
return value
|
||||||
|
|
||||||
async def async_select_option(self, option: str) -> None:
|
@property
|
||||||
setting = self._device.settings[self.entity_description.key]
|
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]):
|
||||||
if (options := self.entity_description.option_list) is not None:
|
if (options := self.entity_description.option_list) is not None:
|
||||||
setting.value = next(
|
return next(
|
||||||
(k for k, v in options.items() if k in setting.values and v == option),
|
(k for k, v in options.items() if k in values and v == option),
|
||||||
option,
|
option,
|
||||||
)
|
)
|
||||||
else:
|
return option
|
||||||
setting.value = 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)
|
||||||
command = self.entity_description.key.split(".")[0]
|
command = self.entity_description.key.split(".")[0]
|
||||||
await self._device.commands[command].send()
|
await self._device.commands[command].send()
|
||||||
await self.coordinator.async_refresh()
|
await self.coordinator.async_refresh()
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _handle_coordinator_update(self, update=True) -> None:
|
def _handle_coordinator_update(self, update=True) -> None:
|
||||||
setting = self._device.settings.get(self.entity_description.key)
|
self._attr_available = self.available
|
||||||
if setting is None:
|
self._attr_options = self.options
|
||||||
self._attr_available = False
|
self._attr_current_option = self.current_option
|
||||||
self._attr_options: List[str] = []
|
|
||||||
value = None
|
|
||||||
else:
|
|
||||||
self._attr_available = True
|
|
||||||
self._attr_options: List[str] = setting.values
|
|
||||||
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
|
|
||||||
if update:
|
if update:
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self) -> bool:
|
||||||
|
return self._device.settings.get(self.entity_description.key) is not None
|
||||||
|
|
||||||
|
|
||||||
|
class HonSelectEntity(HonConfigSelectEntity):
|
||||||
|
entity_description: HonSelectEntityDescription
|
||||||
|
|
||||||
|
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)
|
||||||
|
await self.coordinator.async_refresh()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self) -> bool:
|
def available(self) -> bool:
|
||||||
"""Return True if entity is available."""
|
"""Return True if entity is available."""
|
||||||
|
@ -216,23 +221,3 @@ class HonSelectEntity(HonEntity, SelectEntity):
|
||||||
and self._device.get("remoteCtrValid", "1") == "1"
|
and self._device.get("remoteCtrValid", "1") == "1"
|
||||||
and self._device.get("attributes.lastConnEvent.category") != "DISCONNECTED"
|
and self._device.get("attributes.lastConnEvent.category") != "DISCONNECTED"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class HonConfigSelectEntity(HonSelectEntity):
|
|
||||||
entity_description: HonConfigSelectEntityDescription
|
|
||||||
|
|
||||||
async def async_select_option(self, option: str) -> None:
|
|
||||||
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
|
|
||||||
await self.coordinator.async_refresh()
|
|
||||||
|
|
||||||
@property
|
|
||||||
def available(self) -> bool:
|
|
||||||
"""Return True if entity is available."""
|
|
||||||
return super(SelectEntity, self).available
|
|
||||||
|
|
Loading…
Reference in New Issue