Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Android TV Remote integration."""
2 
3 from __future__ import annotations
4 
5 from asyncio import timeout
6 import logging
7 
8 from androidtvremote2 import (
9  AndroidTVRemote,
10  CannotConnect,
11  ConnectionClosed,
12  InvalidAuth,
13 )
14 
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.const import CONF_HOST, CONF_NAME, EVENT_HOMEASSISTANT_STOP, Platform
17 from homeassistant.core import Event, HomeAssistant, callback
18 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
19 
20 from .helpers import create_api, get_enable_ime
21 
22 _LOGGER = logging.getLogger(__name__)
23 
24 PLATFORMS: list[Platform] = [Platform.MEDIA_PLAYER, Platform.REMOTE]
25 
26 AndroidTVRemoteConfigEntry = ConfigEntry[AndroidTVRemote]
27 
28 
30  hass: HomeAssistant, entry: AndroidTVRemoteConfigEntry
31 ) -> bool:
32  """Set up Android TV Remote from a config entry."""
33  _LOGGER.debug("async_setup_entry: %s", entry.data)
34  api = create_api(hass, entry.data[CONF_HOST], get_enable_ime(entry))
35 
36  @callback
37  def is_available_updated(is_available: bool) -> None:
38  if is_available:
39  _LOGGER.info(
40  "Reconnected to %s at %s", entry.data[CONF_NAME], entry.data[CONF_HOST]
41  )
42  else:
43  _LOGGER.warning(
44  "Disconnected from %s at %s",
45  entry.data[CONF_NAME],
46  entry.data[CONF_HOST],
47  )
48 
49  api.add_is_available_updated_callback(is_available_updated)
50 
51  try:
52  async with timeout(5.0):
53  await api.async_connect()
54  except InvalidAuth as exc:
55  # The Android TV is hard reset or the certificate and key files were deleted.
56  raise ConfigEntryAuthFailed from exc
57  except (CannotConnect, ConnectionClosed, TimeoutError) as exc:
58  # The Android TV is network unreachable. Raise exception and let Home Assistant retry
59  # later. If device gets a new IP address the zeroconf flow will update the config.
60  raise ConfigEntryNotReady from exc
61 
62  def reauth_needed() -> None:
63  """Start a reauth flow if Android TV is hard reset while reconnecting."""
64  entry.async_start_reauth(hass)
65 
66  # Start a task (canceled in disconnect) to keep reconnecting if device becomes
67  # network unreachable. If device gets a new IP address the zeroconf flow will
68  # update the config entry data and reload the config entry.
69  api.keep_reconnecting(reauth_needed)
70 
71  entry.runtime_data = api
72 
73  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
74 
75  @callback
76  def on_hass_stop(event: Event) -> None:
77  """Stop push updates when hass stops."""
78  api.disconnect()
79 
80  entry.async_on_unload(
81  hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, on_hass_stop)
82  )
83  entry.async_on_unload(entry.add_update_listener(async_update_options))
84  entry.async_on_unload(api.disconnect)
85 
86  return True
87 
88 
89 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
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)
93 
94 
95 async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None:
96  """Handle options update."""
97  _LOGGER.debug(
98  "async_update_options: data: %s options: %s", entry.data, entry.options
99  )
100  await hass.config_entries.async_reload(entry.entry_id)
AndroidTVRemote create_api(HomeAssistant hass, str host, bool enable_ime)
Definition: helpers.py:14
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:89
bool async_setup_entry(HomeAssistant hass, AndroidTVRemoteConfigEntry entry)
Definition: __init__.py:31
None async_update_options(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:95