Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The jvc_projector integration."""
2 
3 from __future__ import annotations
4 
5 from jvcprojector import JvcProjector, JvcProjectorAuthError, JvcProjectorConnectError
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import (
9  CONF_HOST,
10  CONF_PASSWORD,
11  CONF_PORT,
12  EVENT_HOMEASSISTANT_STOP,
13  Platform,
14 )
15 from homeassistant.core import Event, HomeAssistant
16 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
17 
18 from .coordinator import JvcProjectorDataUpdateCoordinator
19 
20 type JVCConfigEntry = ConfigEntry[JvcProjectorDataUpdateCoordinator]
21 
22 PLATFORMS = [Platform.BINARY_SENSOR, Platform.REMOTE, Platform.SELECT, Platform.SENSOR]
23 
24 
25 async def async_setup_entry(hass: HomeAssistant, entry: JVCConfigEntry) -> bool:
26  """Set up integration from a config entry."""
27  device = JvcProjector(
28  host=entry.data[CONF_HOST],
29  port=entry.data[CONF_PORT],
30  password=entry.data[CONF_PASSWORD],
31  )
32 
33  try:
34  await device.connect(True)
35  except JvcProjectorConnectError as err:
36  await device.disconnect()
37  raise ConfigEntryNotReady(
38  f"Unable to connect to {entry.data[CONF_HOST]}"
39  ) from err
40  except JvcProjectorAuthError as err:
41  await device.disconnect()
42  raise ConfigEntryAuthFailed("Password authentication failed") from err
43 
44  coordinator = JvcProjectorDataUpdateCoordinator(hass, device)
45  await coordinator.async_config_entry_first_refresh()
46 
47  entry.runtime_data = coordinator
48 
49  async def disconnect(event: Event) -> None:
50  await device.disconnect()
51 
52  entry.async_on_unload(
53  hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, disconnect)
54  )
55 
56  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
57 
58  return True
59 
60 
61 async def async_unload_entry(hass: HomeAssistant, entry: JVCConfigEntry) -> bool:
62  """Unload config entry."""
63  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
64  await entry.runtime_data.device.disconnect()
65  return unload_ok
bool async_unload_entry(HomeAssistant hass, JVCConfigEntry entry)
Definition: __init__.py:61
bool async_setup_entry(HomeAssistant hass, JVCConfigEntry entry)
Definition: __init__.py:25