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