2020-09-24 14:39:24 +00:00
|
|
|
"""The 1-Wire component."""
|
2024-03-08 18:15:59 +00:00
|
|
|
|
2021-04-01 13:06:47 +00:00
|
|
|
import logging
|
2020-10-24 01:57:16 +00:00
|
|
|
|
2021-12-16 13:06:38 +00:00
|
|
|
from pyownet import protocol
|
|
|
|
|
2020-10-26 15:33:13 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-04-22 18:23:19 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2020-10-26 15:33:13 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2022-05-31 08:40:08 +00:00
|
|
|
from homeassistant.helpers import device_registry as dr
|
2020-10-26 15:33:13 +00:00
|
|
|
|
2021-03-02 20:43:59 +00:00
|
|
|
from .const import DOMAIN, PLATFORMS
|
2020-10-26 15:33:13 +00:00
|
|
|
from .onewirehub import CannotConnect, OneWireHub
|
2020-10-24 01:57:16 +00:00
|
|
|
|
2021-04-01 13:06:47 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2024-05-17 13:42:58 +00:00
|
|
|
type OneWireConfigEntry = ConfigEntry[OneWireHub]
|
2021-04-01 13:06:47 +00:00
|
|
|
|
2020-10-24 01:57:16 +00:00
|
|
|
|
2024-04-30 13:55:20 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: OneWireConfigEntry) -> bool:
|
2020-10-24 01:57:16 +00:00
|
|
|
"""Set up a 1-Wire proxy for a config entry."""
|
2022-05-31 10:26:15 +00:00
|
|
|
onewire_hub = OneWireHub(hass)
|
2020-10-26 15:33:13 +00:00
|
|
|
try:
|
2022-05-31 10:26:15 +00:00
|
|
|
await onewire_hub.initialize(entry)
|
2021-12-16 13:06:38 +00:00
|
|
|
except (
|
|
|
|
CannotConnect, # Failed to connect to the server
|
|
|
|
protocol.OwnetError, # Connected to server, but failed to list the devices
|
|
|
|
) as exc:
|
2024-03-17 23:40:38 +00:00
|
|
|
raise ConfigEntryNotReady from exc
|
2020-10-26 15:33:13 +00:00
|
|
|
|
2024-04-30 13:55:20 +00:00
|
|
|
entry.runtime_data = onewire_hub
|
2020-10-26 15:33:13 +00:00
|
|
|
|
2022-07-09 15:27:42 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2021-04-01 13:06:47 +00:00
|
|
|
|
2022-03-03 19:41:59 +00:00
|
|
|
entry.async_on_unload(entry.add_update_listener(options_update_listener))
|
|
|
|
|
2020-10-24 01:57:16 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-05-31 08:40:08 +00:00
|
|
|
async def async_remove_config_entry_device(
|
2024-04-30 13:55:20 +00:00
|
|
|
hass: HomeAssistant, config_entry: OneWireConfigEntry, device_entry: dr.DeviceEntry
|
2022-05-31 08:40:08 +00:00
|
|
|
) -> bool:
|
|
|
|
"""Remove a config entry from a device."""
|
2024-04-30 13:55:20 +00:00
|
|
|
onewire_hub = config_entry.runtime_data
|
2022-05-31 08:40:08 +00:00
|
|
|
return not device_entry.identifiers.intersection(
|
2022-05-31 10:26:15 +00:00
|
|
|
(DOMAIN, device.id) for device in onewire_hub.devices or []
|
2022-05-31 08:40:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-04-30 13:55:20 +00:00
|
|
|
async def async_unload_entry(
|
|
|
|
hass: HomeAssistant, config_entry: OneWireConfigEntry
|
|
|
|
) -> bool:
|
2020-10-24 01:57:16 +00:00
|
|
|
"""Unload a config entry."""
|
2024-04-30 13:55:20 +00:00
|
|
|
return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
|
2022-03-03 19:41:59 +00:00
|
|
|
|
|
|
|
|
2024-04-30 13:55:20 +00:00
|
|
|
async def options_update_listener(
|
|
|
|
hass: HomeAssistant, entry: OneWireConfigEntry
|
|
|
|
) -> None:
|
2022-03-03 19:41:59 +00:00
|
|
|
"""Handle options update."""
|
2022-03-15 07:26:54 +00:00
|
|
|
_LOGGER.debug("Configuration options updated, reloading OneWire integration")
|
2022-03-03 19:41:59 +00:00
|
|
|
await hass.config_entries.async_reload(entry.entry_id)
|