Skip to content

Commit

Permalink
Fix set select entity #70
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre0512 committed Jun 15, 2023
1 parent c1e6f95 commit ae985cb
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions custom_components/hon/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,24 @@ def __init__(self, hass, entry, device: HonAppliance, description) -> None:

@property
def current_option(self) -> str | None:
value = self._device.settings.get(self.entity_description.key)
if value is None or value.value not in self._attr_options:
if not (setting := self._device.settings.get(self.entity_description.key)):
return None
return value.value
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

async def async_select_option(self, option: str) -> None:
self._device.settings[self.entity_description.key].value = option
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
command = self.entity_description.key.split(".")[0]
await self._device.commands[command].send()
await self.coordinator.async_refresh()
Expand All @@ -185,7 +196,7 @@ def _handle_coordinator_update(self, update=True) -> None:
else:
self._attr_available = True
self._attr_options: List[str] = setting.values
value = setting.value
value = str(setting.value)
if self.entity_description.option_list is not None:
self._attr_options = [
self.entity_description.option_list.get(k, k)
Expand All @@ -211,7 +222,14 @@ class HonConfigSelectEntity(HonSelectEntity):
entity_description: HonConfigSelectEntityDescription

async def async_select_option(self, option: str) -> None:
self._device.settings[self.entity_description.key].value = option
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
Expand Down

0 comments on commit ae985cb

Please sign in to comment.