1 """Support for Roku selects."""
3 from __future__
import annotations
5 from collections.abc
import Awaitable, Callable
6 from dataclasses
import dataclass
8 from rokuecp
import Roku
9 from rokuecp.models
import Device
as RokuDevice
16 from .const
import DOMAIN
17 from .coordinator
import RokuDataUpdateCoordinator
18 from .entity
import RokuEntity
19 from .helpers
import format_channel_name, roku_exception_handler
23 if device.app
is None or device.app.name
is None:
26 if device.app.name ==
"Roku":
29 return device.app.name
33 return [
"Home", *sorted(app.name
for app
in device.apps
if app.name
is not None)]
37 if device.channel
is None:
51 await roku.remote(
"home")
54 (app
for app
in device.apps
if value == app.name),
58 if appl
is not None and appl.app_id
is not None:
59 await roku.launch(appl.app_id)
62 async
def _tune_channel(device: RokuDevice, roku: Roku, value: str) ->
None:
66 for channel
in device.channels
68 channel.name
is not None
71 or value == channel.number
76 if _channel
is not None:
77 await roku.tune(_channel.number)
80 @dataclass(frozen=True, kw_only=True)
82 """Describes Roku select entity."""
84 options_fn: Callable[[RokuDevice], list[str]]
85 value_fn: Callable[[RokuDevice], str |
None]
86 set_fn: Callable[[RokuDevice, Roku, str], Awaitable[
None]]
89 ENTITIES: tuple[RokuSelectEntityDescription, ...] = (
92 translation_key=
"application",
93 set_fn=_launch_application,
94 value_fn=_get_application_name,
95 options_fn=_get_applications,
96 entity_registry_enabled_default=
False,
102 translation_key=
"channel",
103 set_fn=_tune_channel,
104 value_fn=_get_channel_name,
105 options_fn=_get_channels,
112 async_add_entities: AddEntitiesCallback,
114 """Set up Roku select based on a config entry."""
115 coordinator: RokuDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
116 device: RokuDevice = coordinator.data
118 entities: list[RokuSelectEntity] = [
120 coordinator=coordinator,
121 description=description,
123 for description
in ENTITIES
126 if len(device.channels) > 0:
129 coordinator=coordinator,
130 description=CHANNEL_ENTITY,
138 """Defines a Roku select entity."""
140 entity_description: RokuSelectEntityDescription
144 """Return the current value."""
149 """Return a set of selectable options."""
152 @roku_exception_handler()
154 """Set the option."""
156 self.coordinator.data,
157 self.coordinator.roku,
160 await self.coordinator.async_request_refresh()
str|None current_option(self)
None async_select_option(self, str option)
str format_channel_name(str channel_number, str|None channel_name=None)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
list[str] _get_applications(RokuDevice device)
None _launch_application(RokuDevice device, Roku roku, str value)
list[str] _get_channels(RokuDevice device)
str|None _get_application_name(RokuDevice device)
None _tune_channel(RokuDevice device, Roku roku, str value)
str|None _get_channel_name(RokuDevice device)