Plugwise add value_fn for select (#93375)

* Plugwise prepare value_fn for select

* Plugwise prepare value_fn for select

* Try proposed options

* Fix initial defs

---------

Co-authored-by: Bouwe Westerdijk <11290930+bouwew@users.noreply.github.com>
This commit is contained in:
Tom 2023-05-24 07:56:17 +02:00 committed by GitHub
parent d8493a41aa
commit a43dcaf812
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5,7 +5,7 @@ from collections.abc import Awaitable, Callable
from dataclasses import dataclass from dataclasses import dataclass
from typing import Any from typing import Any
from plugwise import Smile from plugwise import DeviceData, Smile
from homeassistant.components.select import SelectEntity, SelectEntityDescription from homeassistant.components.select import SelectEntity, SelectEntityDescription
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
@ -23,8 +23,8 @@ class PlugwiseSelectDescriptionMixin:
"""Mixin values for Plugwise Select entities.""" """Mixin values for Plugwise Select entities."""
command: Callable[[Smile, str, str], Awaitable[Any]] command: Callable[[Smile, str, str], Awaitable[Any]]
current_option_key: str value_fn: Callable[[DeviceData], str]
options_key: str options_fn: Callable[[DeviceData], list[str]]
@dataclass @dataclass
@ -40,8 +40,8 @@ SELECT_TYPES = (
translation_key="select_schedule", translation_key="select_schedule",
icon="mdi:calendar-clock", icon="mdi:calendar-clock",
command=lambda api, loc, opt: api.set_schedule_state(loc, opt, STATE_ON), command=lambda api, loc, opt: api.set_schedule_state(loc, opt, STATE_ON),
current_option_key="selected_schedule", value_fn=lambda data: data["selected_schedule"],
options_key="available_schedules", options_fn=lambda data: data.get("available_schedules"),
), ),
PlugwiseSelectEntityDescription( PlugwiseSelectEntityDescription(
key="select_regulation_mode", key="select_regulation_mode",
@ -49,8 +49,8 @@ SELECT_TYPES = (
icon="mdi:hvac", icon="mdi:hvac",
entity_category=EntityCategory.CONFIG, entity_category=EntityCategory.CONFIG,
command=lambda api, loc, opt: api.set_regulation_mode(opt), command=lambda api, loc, opt: api.set_regulation_mode(opt),
current_option_key="regulation_mode", value_fn=lambda data: data["regulation_mode"],
options_key="regulation_modes", options_fn=lambda data: data.get("regulation_modes"),
), ),
PlugwiseSelectEntityDescription( PlugwiseSelectEntityDescription(
key="select_dhw_mode", key="select_dhw_mode",
@ -58,8 +58,8 @@ SELECT_TYPES = (
icon="mdi:shower", icon="mdi:shower",
entity_category=EntityCategory.CONFIG, entity_category=EntityCategory.CONFIG,
command=lambda api, loc, opt: api.set_dhw_mode(opt), command=lambda api, loc, opt: api.set_dhw_mode(opt),
current_option_key="dhw_mode", value_fn=lambda data: data["dhw_mode"],
options_key="dhw_modes", options_fn=lambda data: data.get("dhw_modes"),
), ),
) )
@ -77,10 +77,7 @@ async def async_setup_entry(
entities: list[PlugwiseSelectEntity] = [] entities: list[PlugwiseSelectEntity] = []
for device_id, device in coordinator.data.devices.items(): for device_id, device in coordinator.data.devices.items():
for description in SELECT_TYPES: for description in SELECT_TYPES:
if ( if (options := description.options_fn(device)) and len(options) > 1:
description.options_key in device
and len(device[description.options_key]) > 1
):
entities.append( entities.append(
PlugwiseSelectEntity(coordinator, device_id, description) PlugwiseSelectEntity(coordinator, device_id, description)
) )
@ -107,12 +104,12 @@ class PlugwiseSelectEntity(PlugwiseEntity, SelectEntity):
@property @property
def current_option(self) -> str: def current_option(self) -> str:
"""Return the selected entity option to represent the entity state.""" """Return the selected entity option to represent the entity state."""
return self.device[self.entity_description.current_option_key] return self.entity_description.value_fn(self.device)
@property @property
def options(self) -> list[str]: def options(self) -> list[str]:
"""Return the selectable entity options.""" """Return the selectable entity options."""
return self.device[self.entity_description.options_key] return self.entity_description.options_fn(self.device)
async def async_select_option(self, option: str) -> None: async def async_select_option(self, option: str) -> None:
"""Change to the selected entity option.""" """Change to the selected entity option."""