Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The GitHub integration."""
2 
3 from __future__ import annotations
4 
5 from aiogithubapi import GitHubAPI
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import CONF_ACCESS_TOKEN, Platform
9 from homeassistant.core import HomeAssistant, callback
10 from homeassistant.helpers import device_registry as dr
12  SERVER_SOFTWARE,
13  async_get_clientsession,
14 )
15 
16 from .const import CONF_REPOSITORIES, DOMAIN, LOGGER
17 from .coordinator import GitHubDataUpdateCoordinator
18 
19 PLATFORMS: list[Platform] = [Platform.SENSOR]
20 
21 
22 type GithubConfigEntry = ConfigEntry[dict[str, GitHubDataUpdateCoordinator]]
23 
24 
25 async def async_setup_entry(hass: HomeAssistant, entry: GithubConfigEntry) -> bool:
26  """Set up GitHub from a config entry."""
27  client = GitHubAPI(
28  token=entry.data[CONF_ACCESS_TOKEN],
29  session=async_get_clientsession(hass),
30  client_name=SERVER_SOFTWARE,
31  )
32 
33  repositories: list[str] = entry.options[CONF_REPOSITORIES]
34 
35  entry.runtime_data = {}
36  for repository in repositories:
37  coordinator = GitHubDataUpdateCoordinator(
38  hass=hass,
39  client=client,
40  repository=repository,
41  )
42 
43  await coordinator.async_config_entry_first_refresh()
44 
45  if not entry.pref_disable_polling:
46  await coordinator.subscribe()
47 
48  entry.runtime_data[repository] = coordinator
49 
50  async_cleanup_device_registry(hass=hass, entry=entry)
51 
52  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
53  entry.async_on_unload(entry.add_update_listener(async_reload_entry))
54  return True
55 
56 
57 @callback
59  hass: HomeAssistant,
60  entry: ConfigEntry,
61 ) -> None:
62  """Remove entries form device registry if we no longer track the repository."""
63  device_registry = dr.async_get(hass)
64  devices = dr.async_entries_for_config_entry(
65  registry=device_registry,
66  config_entry_id=entry.entry_id,
67  )
68  for device in devices:
69  for item in device.identifiers:
70  if item[0] == DOMAIN and item[1] not in entry.options[CONF_REPOSITORIES]:
71  LOGGER.debug(
72  (
73  "Unlinking device %s for untracked repository %s from config"
74  " entry %s"
75  ),
76  device.id,
77  item[1],
78  entry.entry_id,
79  )
80  device_registry.async_update_device(
81  device.id, remove_config_entry_id=entry.entry_id
82  )
83  break
84 
85 
86 async def async_unload_entry(hass: HomeAssistant, entry: GithubConfigEntry) -> bool:
87  """Unload a config entry."""
88  repositories = entry.runtime_data
89  for coordinator in repositories.values():
90  coordinator.unsubscribe()
91 
92  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
93 
94 
95 async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
96  """Handle an options update."""
97  await hass.config_entries.async_reload(entry.entry_id)
bool async_unload_entry(HomeAssistant hass, GithubConfigEntry entry)
Definition: __init__.py:86
None async_reload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:95
bool async_setup_entry(HomeAssistant hass, GithubConfigEntry entry)
Definition: __init__.py:25
None async_cleanup_device_registry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:61
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)