Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for Roku."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.config_entries import ConfigEntry
6 from homeassistant.const import CONF_HOST, Platform
7 from homeassistant.core import HomeAssistant
8 
9 from .const import CONF_PLAY_MEDIA_APP_ID, DEFAULT_PLAY_MEDIA_APP_ID, DOMAIN
10 from .coordinator import RokuDataUpdateCoordinator
11 
12 PLATFORMS = [
13  Platform.BINARY_SENSOR,
14  Platform.MEDIA_PLAYER,
15  Platform.REMOTE,
16  Platform.SELECT,
17  Platform.SENSOR,
18 ]
19 
20 
21 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
22  """Set up Roku from a config entry."""
23  if (device_id := entry.unique_id) is None:
24  device_id = entry.entry_id
25 
26  coordinator = RokuDataUpdateCoordinator(
27  hass,
28  host=entry.data[CONF_HOST],
29  device_id=device_id,
30  play_media_app_id=entry.options.get(
31  CONF_PLAY_MEDIA_APP_ID, DEFAULT_PLAY_MEDIA_APP_ID
32  ),
33  )
34  await coordinator.async_config_entry_first_refresh()
35 
36  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
37 
38  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
39 
40  entry.async_on_unload(entry.add_update_listener(async_reload_entry))
41 
42  return True
43 
44 
45 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
46  """Unload a config entry."""
47  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
48  hass.data[DOMAIN].pop(entry.entry_id)
49  return unload_ok
50 
51 
52 async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
53  """Reload the config entry when it changed."""
54  await hass.config_entries.async_reload(entry.entry_id)
None async_reload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:52
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:45
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:21