Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The 1-Wire component."""
2 
3 import logging
4 
5 from pyownet import protocol
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.core import HomeAssistant
9 from homeassistant.exceptions import ConfigEntryNotReady
10 from homeassistant.helpers import device_registry as dr
11 
12 from .const import DOMAIN, PLATFORMS
13 from .onewirehub import CannotConnect, OneWireHub
14 
15 _LOGGER = logging.getLogger(__name__)
16 type OneWireConfigEntry = ConfigEntry[OneWireHub]
17 
18 
19 async def async_setup_entry(hass: HomeAssistant, entry: OneWireConfigEntry) -> bool:
20  """Set up a 1-Wire proxy for a config entry."""
21  onewire_hub = OneWireHub(hass)
22  try:
23  await onewire_hub.initialize(entry)
24  except (
25  CannotConnect, # Failed to connect to the server
26  protocol.OwnetError, # Connected to server, but failed to list the devices
27  ) as exc:
28  raise ConfigEntryNotReady from exc
29 
30  entry.runtime_data = onewire_hub
31 
32  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
33 
34  entry.async_on_unload(entry.add_update_listener(options_update_listener))
35 
36  return True
37 
38 
40  hass: HomeAssistant, config_entry: OneWireConfigEntry, device_entry: dr.DeviceEntry
41 ) -> bool:
42  """Remove a config entry from a device."""
43  onewire_hub = config_entry.runtime_data
44  return not device_entry.identifiers.intersection(
45  (DOMAIN, device.id) for device in onewire_hub.devices or []
46  )
47 
48 
50  hass: HomeAssistant, config_entry: OneWireConfigEntry
51 ) -> bool:
52  """Unload a config entry."""
53  return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
54 
55 
57  hass: HomeAssistant, entry: OneWireConfigEntry
58 ) -> None:
59  """Handle options update."""
60  _LOGGER.debug("Configuration options updated, reloading OneWire integration")
61  await hass.config_entries.async_reload(entry.entry_id)
bool async_remove_config_entry_device(HomeAssistant hass, OneWireConfigEntry config_entry, dr.DeviceEntry device_entry)
Definition: __init__.py:41
bool async_setup_entry(HomeAssistant hass, OneWireConfigEntry entry)
Definition: __init__.py:19
None options_update_listener(HomeAssistant hass, OneWireConfigEntry entry)
Definition: __init__.py:58
bool async_unload_entry(HomeAssistant hass, OneWireConfigEntry config_entry)
Definition: __init__.py:51