Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for the Philips Hue system."""
2 
3 from aiohue.util import normalize_bridge_id
4 
5 from homeassistant.components import persistent_notification
6 from homeassistant.config_entries import SOURCE_IGNORE, ConfigEntry
7 from homeassistant.core import HomeAssistant
8 from homeassistant.helpers import device_registry as dr
9 
10 from .bridge import HueBridge
11 from .const import DOMAIN, SERVICE_HUE_ACTIVATE_SCENE
12 from .migration import check_migration
13 from .services import async_register_services
14 
15 
16 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
17  """Set up a bridge from a config entry."""
18  # check (and run) migrations if needed
19  await check_migration(hass, entry)
20 
21  # setup the bridge instance
22  bridge = HueBridge(hass, entry)
23  if not await bridge.async_initialize_bridge():
24  return False
25 
26  # register Hue domain services
28 
29  api = bridge.api
30 
31  # For backwards compat
32  unique_id = normalize_bridge_id(api.config.bridge_id)
33  if entry.unique_id is None:
34  hass.config_entries.async_update_entry(entry, unique_id=unique_id)
35 
36  # For recovering from bug where we incorrectly assumed homekit ID = bridge ID
37  # Remove this logic after Home Assistant 2022.4
38  elif entry.unique_id != unique_id:
39  # Find entries with this unique ID
40  other_entry = next(
41  (
42  entry
43  for entry in hass.config_entries.async_entries(DOMAIN)
44  if entry.unique_id == unique_id
45  ),
46  None,
47  )
48  if other_entry is None:
49  # If no other entry, update unique ID of this entry ID.
50  hass.config_entries.async_update_entry(entry, unique_id=unique_id)
51 
52  elif other_entry.source == SOURCE_IGNORE:
53  # There is another entry but it is ignored, delete that one and update this one
54  hass.async_create_task(
55  hass.config_entries.async_remove(other_entry.entry_id)
56  )
57  hass.config_entries.async_update_entry(entry, unique_id=unique_id)
58  else:
59  # There is another entry that already has the right unique ID. Delete this entry
60  hass.async_create_task(hass.config_entries.async_remove(entry.entry_id))
61  return False
62 
63  # add bridge device to device registry
64  device_registry = dr.async_get(hass)
65  if bridge.api_version == 1:
66  device_registry.async_get_or_create(
67  config_entry_id=entry.entry_id,
68  connections={(dr.CONNECTION_NETWORK_MAC, api.config.mac_address)},
69  identifiers={(DOMAIN, api.config.bridge_id)},
70  manufacturer="Signify",
71  name=api.config.name,
72  model=api.config.model_id,
73  sw_version=api.config.software_version,
74  )
75  # create persistent notification if we found a bridge version with security vulnerability
76  if (
77  api.config.model_id == "BSB002"
78  and api.config.software_version < "1935144040"
79  ):
80  persistent_notification.async_create(
81  hass,
82  (
83  "Your Hue hub has a known security vulnerability ([CVE-2020-6007] "
84  "(https://cve.circl.lu/cve/CVE-2020-6007)). "
85  "Go to the Hue app and check for software updates."
86  ),
87  "Signify Hue",
88  "hue_hub_firmware",
89  )
90  else:
91  device_registry.async_get_or_create(
92  config_entry_id=entry.entry_id,
93  connections={(dr.CONNECTION_NETWORK_MAC, api.config.mac_address)},
94  identifiers={
95  (DOMAIN, api.config.bridge_id),
96  (DOMAIN, api.config.bridge_device.id),
97  },
98  manufacturer=api.config.bridge_device.product_data.manufacturer_name,
99  name=api.config.name,
100  model=api.config.model_id,
101  sw_version=api.config.software_version,
102  )
103 
104  return True
105 
106 
107 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
108  """Unload a config entry."""
109  unload_success = await hass.data[DOMAIN][entry.entry_id].async_reset()
110  if len(hass.data[DOMAIN]) == 0:
111  hass.data.pop(DOMAIN)
112  hass.services.async_remove(DOMAIN, SERVICE_HUE_ACTIVATE_SCENE)
113  return unload_success
None async_register_services(HomeAssistant hass)
Definition: services.py:80
None check_migration(core.HomeAssistant hass, ConfigEntry entry)
Definition: migration.py:26
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:107
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:16