1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-25 13:38:04 +00:00
Files
core/homeassistant/components/threshold/__init__.py
Joakim Plate 958a456275 Allow source sensor to be changed in threshold helper (#119157)
* Allow source sensor to be changed in threshold helper

* Make sure old device link is removed on entry change

* Add test case for changed association
2024-06-11 06:41:29 +02:00

41 lines
1.4 KiB
Python

"""The threshold component."""
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Min/Max from a config entry."""
await hass.config_entries.async_forward_entry_setups(
entry, (Platform.BINARY_SENSOR,)
)
entry.async_on_unload(entry.add_update_listener(config_entry_update_listener))
return True
async def config_entry_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Update listener, called when the config entry options are changed."""
# Remove device link for entry, the source device may have changed.
# The link will be recreated after load.
device_registry = dr.async_get(hass)
devices = device_registry.devices.get_devices_for_config_entry_id(entry.entry_id)
for device in devices:
device_registry.async_update_device(
device.id, remove_config_entry_id=entry.entry_id
)
await hass.config_entries.async_reload(entry.entry_id)
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
return await hass.config_entries.async_unload_platforms(
entry, (Platform.BINARY_SENSOR,)
)