1 """The Android TV Remote integration."""
3 from __future__
import annotations
5 from asyncio
import timeout
8 from androidtvremote2
import (
20 from .helpers
import create_api, get_enable_ime
22 _LOGGER = logging.getLogger(__name__)
24 PLATFORMS: list[Platform] = [Platform.MEDIA_PLAYER, Platform.REMOTE]
26 AndroidTVRemoteConfigEntry = ConfigEntry[AndroidTVRemote]
30 hass: HomeAssistant, entry: AndroidTVRemoteConfigEntry
32 """Set up Android TV Remote from a config entry."""
33 _LOGGER.debug(
"async_setup_entry: %s", entry.data)
37 def is_available_updated(is_available: bool) ->
None:
40 "Reconnected to %s at %s", entry.data[CONF_NAME], entry.data[CONF_HOST]
44 "Disconnected from %s at %s",
45 entry.data[CONF_NAME],
46 entry.data[CONF_HOST],
49 api.add_is_available_updated_callback(is_available_updated)
52 async
with timeout(5.0):
53 await api.async_connect()
54 except InvalidAuth
as exc:
56 raise ConfigEntryAuthFailed
from exc
57 except (CannotConnect, ConnectionClosed, TimeoutError)
as exc:
60 raise ConfigEntryNotReady
from exc
62 def reauth_needed() -> None:
63 """Start a reauth flow if Android TV is hard reset while reconnecting."""
64 entry.async_start_reauth(hass)
69 api.keep_reconnecting(reauth_needed)
71 entry.runtime_data = api
73 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
76 def on_hass_stop(event: Event) ->
None:
77 """Stop push updates when hass stops."""
80 entry.async_on_unload(
81 hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, on_hass_stop)
83 entry.async_on_unload(entry.add_update_listener(async_update_options))
84 entry.async_on_unload(api.disconnect)
90 """Unload a config entry."""
91 _LOGGER.debug(
"async_unload_entry: %s", entry.data)
92 return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
96 """Handle options update."""
98 "async_update_options: data: %s options: %s", entry.data, entry.options
100 await hass.config_entries.async_reload(entry.entry_id)
AndroidTVRemote create_api(HomeAssistant hass, str host, bool enable_ime)
bool get_enable_ime(ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, AndroidTVRemoteConfigEntry entry)
None async_update_options(HomeAssistant hass, ConfigEntry entry)