Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The FiveM integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from fivem import FiveMServerOfflineError
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONF_HOST, CONF_PORT, Platform
11 from homeassistant.core import HomeAssistant
12 from homeassistant.exceptions import ConfigEntryNotReady
13 
14 from .const import DOMAIN
15 from .coordinator import FiveMDataUpdateCoordinator
16 
17 PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]
18 
19 _LOGGER = logging.getLogger(__name__)
20 
21 
22 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
23  """Set up FiveM from a config entry."""
24  _LOGGER.debug(
25  "Create FiveM server instance for '%s:%s'",
26  entry.data[CONF_HOST],
27  entry.data[CONF_PORT],
28  )
29 
30  coordinator = FiveMDataUpdateCoordinator(hass, entry.data, entry.entry_id)
31 
32  try:
33  await coordinator.initialize()
34  except FiveMServerOfflineError as err:
35  raise ConfigEntryNotReady from err
36 
37  await coordinator.async_config_entry_first_refresh()
38 
39  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
40 
41  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
42 
43  return True
44 
45 
46 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
47  """Unload a config entry."""
48  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
49  hass.data[DOMAIN].pop(entry.entry_id)
50 
51  return unload_ok
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:22
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:46