mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Avoid multiple options and current_option lookups in select entites (#96630)
This commit is contained in:
parent
28540b0cb2
commit
cde1903e8b
@ -144,9 +144,10 @@ class SelectEntity(Entity):
|
|||||||
@final
|
@final
|
||||||
def state(self) -> str | None:
|
def state(self) -> str | None:
|
||||||
"""Return the entity state."""
|
"""Return the entity state."""
|
||||||
if self.current_option is None or self.current_option not in self.options:
|
current_option = self.current_option
|
||||||
|
if current_option is None or current_option not in self.options:
|
||||||
return None
|
return None
|
||||||
return self.current_option
|
return current_option
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def options(self) -> list[str]:
|
def options(self) -> list[str]:
|
||||||
@ -209,21 +210,24 @@ class SelectEntity(Entity):
|
|||||||
async def _async_offset_index(self, offset: int, cycle: bool) -> None:
|
async def _async_offset_index(self, offset: int, cycle: bool) -> None:
|
||||||
"""Offset current index."""
|
"""Offset current index."""
|
||||||
current_index = 0
|
current_index = 0
|
||||||
if self.current_option is not None and self.current_option in self.options:
|
current_option = self.current_option
|
||||||
current_index = self.options.index(self.current_option)
|
options = self.options
|
||||||
|
if current_option is not None and current_option in self.options:
|
||||||
|
current_index = self.options.index(current_option)
|
||||||
|
|
||||||
new_index = current_index + offset
|
new_index = current_index + offset
|
||||||
if cycle:
|
if cycle:
|
||||||
new_index = new_index % len(self.options)
|
new_index = new_index % len(options)
|
||||||
elif new_index < 0:
|
elif new_index < 0:
|
||||||
new_index = 0
|
new_index = 0
|
||||||
elif new_index >= len(self.options):
|
elif new_index >= len(options):
|
||||||
new_index = len(self.options) - 1
|
new_index = len(options) - 1
|
||||||
|
|
||||||
await self.async_select_option(self.options[new_index])
|
await self.async_select_option(options[new_index])
|
||||||
|
|
||||||
@final
|
@final
|
||||||
async def _async_select_index(self, idx: int) -> None:
|
async def _async_select_index(self, idx: int) -> None:
|
||||||
"""Select new option by index."""
|
"""Select new option by index."""
|
||||||
new_index = idx % len(self.options)
|
options = self.options
|
||||||
await self.async_select_option(self.options[new_index])
|
new_index = idx % len(options)
|
||||||
|
await self.async_select_option(options[new_index])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user