Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Philips TV integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from haphilipsjs import PhilipsTV
8 from haphilipsjs.typing import SystemType
9 
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import (
12  CONF_API_VERSION,
13  CONF_HOST,
14  CONF_PASSWORD,
15  CONF_USERNAME,
16  Platform,
17 )
18 from homeassistant.core import HomeAssistant
19 
20 from .const import CONF_SYSTEM
21 from .coordinator import PhilipsTVDataUpdateCoordinator
22 
23 PLATFORMS = [
24  Platform.BINARY_SENSOR,
25  Platform.LIGHT,
26  Platform.MEDIA_PLAYER,
27  Platform.REMOTE,
28  Platform.SWITCH,
29 ]
30 
31 LOGGER = logging.getLogger(__name__)
32 
33 PhilipsTVConfigEntry = ConfigEntry[PhilipsTVDataUpdateCoordinator]
34 
35 
36 async def async_setup_entry(hass: HomeAssistant, entry: PhilipsTVConfigEntry) -> bool:
37  """Set up Philips TV from a config entry."""
38 
39  system: SystemType | None = entry.data.get(CONF_SYSTEM)
40  tvapi = PhilipsTV(
41  entry.data[CONF_HOST],
42  entry.data[CONF_API_VERSION],
43  username=entry.data.get(CONF_USERNAME),
44  password=entry.data.get(CONF_PASSWORD),
45  system=system,
46  )
47  coordinator = PhilipsTVDataUpdateCoordinator(hass, tvapi, entry.options)
48 
49  await coordinator.async_refresh()
50 
51  if (actual_system := tvapi.system) and actual_system != system:
52  data = {**entry.data, CONF_SYSTEM: actual_system}
53  hass.config_entries.async_update_entry(entry, data=data)
54 
55  entry.runtime_data = coordinator
56 
57  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
58 
59  entry.async_on_unload(entry.add_update_listener(async_update_entry))
60 
61  return True
62 
63 
64 async def async_update_entry(hass: HomeAssistant, entry: PhilipsTVConfigEntry) -> None:
65  """Update options."""
66  await hass.config_entries.async_reload(entry.entry_id)
67 
68 
69 async def async_unload_entry(hass: HomeAssistant, entry: PhilipsTVConfigEntry) -> bool:
70  """Unload a config entry."""
71  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_unload_entry(HomeAssistant hass, PhilipsTVConfigEntry entry)
Definition: __init__.py:69
None async_update_entry(HomeAssistant hass, PhilipsTVConfigEntry entry)
Definition: __init__.py:64
bool async_setup_entry(HomeAssistant hass, PhilipsTVConfigEntry entry)
Definition: __init__.py:36