Add options to SelectEntityDescription (#78882)

This commit is contained in:
epenet 2022-10-10 14:20:04 +02:00 committed by GitHub
parent f8f4b059a1
commit ca9bfc8b86
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 14 additions and 22 deletions

View file

@ -23,7 +23,6 @@ class PlenticoreRequiredKeysMixin:
"""A class that describes required properties for plenticore select entities."""
module_id: str
options: list[str]
@dataclass
@ -65,6 +64,7 @@ async def async_setup_entry(
entities = []
for description in SELECT_SETTINGS_DATA:
assert description.options is not None
if description.module_id not in available_settings_data:
continue
needed_data_ids = {
@ -109,7 +109,6 @@ class PlenticoreDataSelect(CoordinatorEntity, SelectEntity):
self.platform_name = platform_name
self.module_id = description.module_id
self.data_id = description.key
self._attr_options = description.options
self._device_info = device_info
self._attr_unique_id = f"{entry_id}_{description.module_id}"

View file

@ -22,7 +22,6 @@ from .entity import LaMetricEntity
class LaMetricEntityDescriptionMixin:
"""Mixin values for LaMetric entities."""
options: list[str]
current_fn: Callable[[Device], str]
select_fn: Callable[[LaMetricDevice, str], Awaitable[Any]]
@ -77,7 +76,6 @@ class LaMetricSelectEntity(LaMetricEntity, SelectEntity):
"""Initiate LaMetric Select."""
super().__init__(coordinator)
self.entity_description = description
self._attr_options = description.options
self._attr_unique_id = f"{coordinator.data.serial_number}-{description.key}"
@property

View file

@ -21,7 +21,6 @@ from .entity import OverkizDescriptiveEntity, OverkizDeviceClass
class OverkizSelectDescriptionMixin:
"""Define an entity description mixin for select entities."""
options: list[str | OverkizCommandParam]
select_option: Callable[[str, Callable[..., Awaitable[None]]], Awaitable[None]]
@ -149,11 +148,6 @@ class OverkizSelect(OverkizDescriptiveEntity, SelectEntity):
return None
@property
def options(self) -> list[str]:
"""Return a set of selectable options."""
return self.entity_description.options
async def async_select_option(self, option: str) -> None:
"""Change the selected option."""
await self.entity_description.select_option(

View file

@ -24,7 +24,6 @@ class RenaultSelectRequiredKeysMixin:
data_key: str
icon_lambda: Callable[[RenaultSelectEntity], str]
options: list[str]
@dataclass
@ -74,11 +73,6 @@ class RenaultSelectEntity(
"""Icon handling."""
return self.entity_description.icon_lambda(self)
@property
def options(self) -> list[str]:
"""Return a set of selectable options."""
return self.entity_description.options
async def async_select_option(self, option: str) -> None:
"""Change the selected option."""
await self.vehicle.vehicle.set_charge_mode(option)

View file

@ -72,6 +72,8 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
class SelectEntityDescription(EntityDescription):
"""A class that describes select entities."""
options: list[str] | None = None
class SelectEntity(Entity):
"""Representation of a Select entity."""
@ -99,7 +101,14 @@ class SelectEntity(Entity):
@property
def options(self) -> list[str]:
"""Return a set of selectable options."""
return self._attr_options
if hasattr(self, "_attr_options"):
return self._attr_options
if (
hasattr(self, "entity_description")
and self.entity_description.options is not None
):
return self.entity_description.options
raise AttributeError()
@property
def current_option(self) -> str | None:

View file

@ -74,7 +74,6 @@ class XiaomiMiioSelectDescription(SelectEntityDescription):
options_map: dict = field(default_factory=dict)
set_method: str = ""
set_method_error_message: str = ""
options: tuple = ()
class AttributeEnumMapping(NamedTuple):
@ -150,7 +149,7 @@ SELECTOR_TYPES = (
set_method_error_message="Setting the display orientation failed.",
icon="mdi:tablet",
device_class="xiaomi_miio__display_orientation",
options=("forward", "left", "right"),
options=["forward", "left", "right"],
entity_category=EntityCategory.CONFIG,
),
XiaomiMiioSelectDescription(
@ -161,7 +160,7 @@ SELECTOR_TYPES = (
set_method_error_message="Setting the led brightness failed.",
icon="mdi:brightness-6",
device_class="xiaomi_miio__led_brightness",
options=("bright", "dim", "off"),
options=["bright", "dim", "off"],
entity_category=EntityCategory.CONFIG,
),
XiaomiMiioSelectDescription(
@ -172,7 +171,7 @@ SELECTOR_TYPES = (
set_method_error_message="Setting the ptc level failed.",
icon="mdi:fire-circle",
device_class="xiaomi_miio__ptc_level",
options=("low", "medium", "high"),
options=["low", "medium", "high"],
entity_category=EntityCategory.CONFIG,
),
)
@ -220,7 +219,6 @@ class XiaomiSelector(XiaomiCoordinatedMiioEntity, SelectEntity):
def __init__(self, device, entry, unique_id, coordinator, description):
"""Initialize the generic Xiaomi attribute selector."""
super().__init__(device, entry, unique_id, coordinator)
self._attr_options = list(description.options)
self.entity_description = description