mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Fix bond reloading on zeroconf discovery when host has not changed (#57799)
This commit is contained in:
parent
e9d601a688
commit
d09ee11c54
@ -1,6 +1,7 @@
|
|||||||
"""The Bond integration."""
|
"""The Bond integration."""
|
||||||
from asyncio import TimeoutError as AsyncIOTimeoutError
|
from asyncio import TimeoutError as AsyncIOTimeoutError
|
||||||
import logging
|
import logging
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from aiohttp import ClientError, ClientResponseError, ClientTimeout
|
from aiohttp import ClientError, ClientResponseError, ClientTimeout
|
||||||
from bond_api import Bond, BPUPSubscriptions, start_bpup
|
from bond_api import Bond, BPUPSubscriptions, start_bpup
|
||||||
@ -12,18 +13,17 @@ from homeassistant.const import (
|
|||||||
EVENT_HOMEASSISTANT_STOP,
|
EVENT_HOMEASSISTANT_STOP,
|
||||||
HTTP_UNAUTHORIZED,
|
HTTP_UNAUTHORIZED,
|
||||||
)
|
)
|
||||||
from homeassistant.core import Event, HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
from homeassistant.helpers import device_registry as dr
|
from homeassistant.helpers import device_registry as dr
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
from homeassistant.helpers.entity import SLOW_UPDATE_WARNING
|
from homeassistant.helpers.entity import SLOW_UPDATE_WARNING
|
||||||
|
|
||||||
from .const import BPUP_STOP, BPUP_SUBS, BRIDGE_MAKE, DOMAIN, HUB
|
from .const import BPUP_SUBS, BRIDGE_MAKE, DOMAIN, HUB
|
||||||
from .utils import BondHub
|
from .utils import BondHub
|
||||||
|
|
||||||
PLATFORMS = ["cover", "fan", "light", "switch"]
|
PLATFORMS = ["cover", "fan", "light", "switch"]
|
||||||
_API_TIMEOUT = SLOW_UPDATE_WARNING - 1
|
_API_TIMEOUT = SLOW_UPDATE_WARNING - 1
|
||||||
_STOP_CANCEL = "stop_cancel"
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -55,18 +55,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
stop_bpup = await start_bpup(host, bpup_subs)
|
stop_bpup = await start_bpup(host, bpup_subs)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _async_stop_event(event: Event) -> None:
|
def _async_stop_event(*_: Any) -> None:
|
||||||
stop_bpup()
|
stop_bpup()
|
||||||
|
|
||||||
stop_event_cancel = hass.bus.async_listen(
|
entry.async_on_unload(_async_stop_event)
|
||||||
EVENT_HOMEASSISTANT_STOP, _async_stop_event
|
entry.async_on_unload(
|
||||||
|
hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, _async_stop_event)
|
||||||
)
|
)
|
||||||
hass.data.setdefault(DOMAIN, {})
|
hass.data.setdefault(DOMAIN, {})
|
||||||
hass.data[DOMAIN][entry.entry_id] = {
|
hass.data[DOMAIN][entry.entry_id] = {
|
||||||
HUB: hub,
|
HUB: hub,
|
||||||
BPUP_SUBS: bpup_subs,
|
BPUP_SUBS: bpup_subs,
|
||||||
BPUP_STOP: stop_bpup,
|
|
||||||
_STOP_CANCEL: stop_event_cancel,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if not entry.unique_id:
|
if not entry.unique_id:
|
||||||
@ -95,15 +94,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
|
|
||||||
data = hass.data[DOMAIN][entry.entry_id]
|
|
||||||
data[_STOP_CANCEL]()
|
|
||||||
if BPUP_STOP in data:
|
|
||||||
data[BPUP_STOP]()
|
|
||||||
|
|
||||||
if unload_ok:
|
if unload_ok:
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
hass.data[DOMAIN].pop(entry.entry_id)
|
||||||
|
|
||||||
return unload_ok
|
return unload_ok
|
||||||
|
|
||||||
|
|
||||||
|
@ -110,12 +110,14 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
token := await async_get_token(self.hass, host)
|
token := await async_get_token(self.hass, host)
|
||||||
):
|
):
|
||||||
updates[CONF_ACCESS_TOKEN] = token
|
updates[CONF_ACCESS_TOKEN] = token
|
||||||
self.hass.config_entries.async_update_entry(
|
new_data = {**entry.data, **updates}
|
||||||
entry, data={**entry.data, **updates}
|
if new_data != dict(entry.data):
|
||||||
)
|
self.hass.config_entries.async_update_entry(
|
||||||
self.hass.async_create_task(
|
entry, data={**entry.data, **updates}
|
||||||
self.hass.config_entries.async_reload(entry.entry_id)
|
)
|
||||||
)
|
self.hass.async_create_task(
|
||||||
|
self.hass.config_entries.async_reload(entry.entry_id)
|
||||||
|
)
|
||||||
raise AbortFlow("already_configured")
|
raise AbortFlow("already_configured")
|
||||||
|
|
||||||
self._discovered = {CONF_HOST: host, CONF_NAME: bond_id}
|
self._discovered = {CONF_HOST: host, CONF_NAME: bond_id}
|
||||||
|
@ -9,7 +9,6 @@ CONF_BOND_ID: str = "bond_id"
|
|||||||
|
|
||||||
HUB = "hub"
|
HUB = "hub"
|
||||||
BPUP_SUBS = "bpup_subs"
|
BPUP_SUBS = "bpup_subs"
|
||||||
BPUP_STOP = "bpup_stop"
|
|
||||||
|
|
||||||
SERVICE_SET_FAN_SPEED_TRACKED_STATE = "set_fan_speed_tracked_state"
|
SERVICE_SET_FAN_SPEED_TRACKED_STATE = "set_fan_speed_tracked_state"
|
||||||
SERVICE_SET_POWER_TRACKED_STATE = "set_switch_power_tracked_state"
|
SERVICE_SET_POWER_TRACKED_STATE = "set_switch_power_tracked_state"
|
||||||
|
@ -352,6 +352,35 @@ async def test_zeroconf_already_configured_refresh_token(hass: core.HomeAssistan
|
|||||||
assert len(mock_setup_entry.mock_calls) == 1
|
assert len(mock_setup_entry.mock_calls) == 1
|
||||||
|
|
||||||
|
|
||||||
|
async def test_zeroconf_already_configured_no_reload_same_host(
|
||||||
|
hass: core.HomeAssistant,
|
||||||
|
):
|
||||||
|
"""Test starting a flow from zeroconf when already configured does not reload if the host is the same."""
|
||||||
|
entry = MockConfigEntry(
|
||||||
|
domain=DOMAIN,
|
||||||
|
unique_id="already-registered-bond-id",
|
||||||
|
data={CONF_HOST: "stored-host", CONF_ACCESS_TOKEN: "correct-token"},
|
||||||
|
)
|
||||||
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
with _patch_async_setup_entry() as mock_setup_entry, patch_bond_token(
|
||||||
|
return_value={"token": "correct-token"}
|
||||||
|
):
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN,
|
||||||
|
context={"source": config_entries.SOURCE_ZEROCONF},
|
||||||
|
data={
|
||||||
|
"name": "already-registered-bond-id.some-other-tail-info",
|
||||||
|
"host": "stored-host",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert result["type"] == "abort"
|
||||||
|
assert result["reason"] == "already_configured"
|
||||||
|
assert len(mock_setup_entry.mock_calls) == 0
|
||||||
|
|
||||||
|
|
||||||
async def test_zeroconf_form_unexpected_error(hass: core.HomeAssistant):
|
async def test_zeroconf_form_unexpected_error(hass: core.HomeAssistant):
|
||||||
"""Test we handle unexpected error gracefully."""
|
"""Test we handle unexpected error gracefully."""
|
||||||
await _help_test_form_unexpected_error(
|
await _help_test_form_unexpected_error(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user