Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Govee Light local integration."""
2 
3 from __future__ import annotations
4 
5 import asyncio
6 from contextlib import suppress
7 from errno import EADDRINUSE
8 import logging
9 
10 from govee_local_api.controller import LISTENING_PORT
11 
12 from homeassistant.const import Platform
13 from homeassistant.core import HomeAssistant
14 from homeassistant.exceptions import ConfigEntryNotReady
15 
16 from .const import DISCOVERY_TIMEOUT
17 from .coordinator import GoveeLocalApiCoordinator, GoveeLocalConfigEntry
18 
19 PLATFORMS: list[Platform] = [Platform.LIGHT]
20 
21 _LOGGER = logging.getLogger(__name__)
22 
23 
24 async def async_setup_entry(hass: HomeAssistant, entry: GoveeLocalConfigEntry) -> bool:
25  """Set up Govee light local from a config entry."""
26  coordinator = GoveeLocalApiCoordinator(hass=hass)
27 
28  async def await_cleanup():
29  cleanup_complete: asyncio.Event = coordinator.cleanup()
30  with suppress(TimeoutError):
31  await asyncio.wait_for(cleanup_complete.wait(), 1)
32 
33  entry.async_on_unload(await_cleanup)
34 
35  try:
36  await coordinator.start()
37  except OSError as ex:
38  if ex.errno != EADDRINUSE:
39  _LOGGER.error("Start failed, errno: %d", ex.errno)
40  return False
41  _LOGGER.error("Port %s already in use", LISTENING_PORT)
42  raise ConfigEntryNotReady from ex
43 
44  await coordinator.async_config_entry_first_refresh()
45 
46  try:
47  async with asyncio.timeout(delay=DISCOVERY_TIMEOUT):
48  while not coordinator.devices:
49  await asyncio.sleep(delay=1)
50  except TimeoutError as ex:
51  raise ConfigEntryNotReady from ex
52 
53  entry.runtime_data = coordinator
54  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
55  return True
56 
57 
58 async def async_unload_entry(hass: HomeAssistant, entry: GoveeLocalConfigEntry) -> bool:
59  """Unload a config entry."""
60  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_setup_entry(HomeAssistant hass, GoveeLocalConfigEntry entry)
Definition: __init__.py:24
bool async_unload_entry(HomeAssistant hass, GoveeLocalConfigEntry entry)
Definition: __init__.py:58