1 """Bridge between emulated_roku and Home Assistant."""
5 from emulated_roku
import EmulatedRokuCommandHandler, EmulatedRokuServer
10 LOGGER = logging.getLogger(__package__)
12 EVENT_ROKU_COMMAND =
"roku_command"
14 ATTR_COMMAND_TYPE =
"type"
15 ATTR_SOURCE_NAME =
"source_name"
17 ATTR_APP_ID =
"app_id"
19 ROKU_COMMAND_KEYDOWN =
"keydown"
20 ROKU_COMMAND_KEYUP =
"keyup"
21 ROKU_COMMAND_KEYPRESS =
"keypress"
22 ROKU_COMMAND_LAUNCH =
"launch"
26 """Manages an emulated_roku server."""
38 """Initialize the properties."""
56 """Start the emulated_roku server."""
58 class EventCommandHandler(EmulatedRokuCommandHandler):
59 """emulated_roku command handler to turn commands into events."""
64 def on_keydown(self, roku_usn, key):
65 """Handle keydown event."""
66 self.
hasshass.bus.async_fire(
69 ATTR_SOURCE_NAME: roku_usn,
70 ATTR_COMMAND_TYPE: ROKU_COMMAND_KEYDOWN,
76 def on_keyup(self, roku_usn, key):
77 """Handle keyup event."""
78 self.
hasshass.bus.async_fire(
81 ATTR_SOURCE_NAME: roku_usn,
82 ATTR_COMMAND_TYPE: ROKU_COMMAND_KEYUP,
88 def on_keypress(self, roku_usn, key):
89 """Handle keypress event."""
90 self.
hasshass.bus.async_fire(
93 ATTR_SOURCE_NAME: roku_usn,
94 ATTR_COMMAND_TYPE: ROKU_COMMAND_KEYPRESS,
100 def launch(self, roku_usn, app_id):
101 """Handle launch event."""
102 self.
hasshass.bus.async_fire(
105 ATTR_SOURCE_NAME: roku_usn,
106 ATTR_COMMAND_TYPE: ROKU_COMMAND_LAUNCH,
113 "Initializing emulated_roku %s on %s:%s",
119 handler = EventCommandHandler(self.
hasshass)
132 async
def emulated_roku_stop(event):
133 """Wrap the call to emulated_roku.close."""
134 LOGGER.debug(
"Stopping emulated_roku %s", self.
roku_usnroku_usn)
138 async
def emulated_roku_start(event):
139 """Wrap the call to emulated_roku.start."""
141 LOGGER.debug(
"Starting emulated_roku %s", self.
roku_usnroku_usn)
146 "Failed to start Emulated Roku %s on %s:%s",
152 await emulated_roku_stop(
None)
155 EVENT_HOMEASSISTANT_STOP, emulated_roku_stop
159 if self.
hasshass.state
is CoreState.running:
160 await emulated_roku_start(
None)
163 EVENT_HOMEASSISTANT_START, emulated_roku_start
169 """Unload the emulated_roku server."""
170 LOGGER.debug(
"Unloading emulated_roku %s", self.
roku_usnroku_usn)
def __init__(self, hass, name, host_ip, listen_port, advertise_ip, advertise_port, upnp_bind_multicast)