Shutdown bond bpup and skip polling after the stop event (#49326)
parent
080c89c761
commit
6e911ba19f
|
@ -6,8 +6,8 @@ from aiohttp import ClientError, ClientTimeout
|
|||
from bond_api import Bond, BPUPSubscriptions, start_bpup
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST, EVENT_HOMEASSISTANT_STOP
|
||||
from homeassistant.core import Event, HomeAssistant, callback
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
|
@ -18,6 +18,7 @@ from .utils import BondHub
|
|||
|
||||
PLATFORMS = ["cover", "fan", "light", "switch"]
|
||||
_API_TIMEOUT = SLOW_UPDATE_WARNING - 1
|
||||
_STOP_CANCEL = "stop_cancel"
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
|
@ -41,11 +42,19 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
bpup_subs = BPUPSubscriptions()
|
||||
stop_bpup = await start_bpup(host, bpup_subs)
|
||||
|
||||
@callback
|
||||
def _async_stop_event(event: Event) -> None:
|
||||
stop_bpup()
|
||||
|
||||
stop_event_cancel = hass.bus.async_listen(
|
||||
EVENT_HOMEASSISTANT_STOP, _async_stop_event
|
||||
)
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
hass.data[DOMAIN][entry.entry_id] = {
|
||||
HUB: hub,
|
||||
BPUP_SUBS: bpup_subs,
|
||||
BPUP_STOP: stop_bpup,
|
||||
_STOP_CANCEL: stop_event_cancel,
|
||||
}
|
||||
|
||||
if not entry.unique_id:
|
||||
|
@ -86,6 +95,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
)
|
||||
|
||||
data = hass.data[DOMAIN][entry.entry_id]
|
||||
data[_STOP_CANCEL]()
|
||||
if BPUP_STOP in data:
|
||||
data[BPUP_STOP]()
|
||||
|
||||
|
|
|
@ -104,7 +104,12 @@ class BondEntity(Entity):
|
|||
|
||||
async def _async_update_if_bpup_not_alive(self, *_: Any) -> None:
|
||||
"""Fetch via the API if BPUP is not alive."""
|
||||
if self._bpup_subs.alive and self._initialized and self._available:
|
||||
if (
|
||||
self.hass.is_stopping
|
||||
or self._bpup_subs.alive
|
||||
and self._initialized
|
||||
and self._available
|
||||
):
|
||||
return
|
||||
|
||||
assert self._update_lock is not None
|
||||
|
|
|
@ -8,7 +8,8 @@ from bond_api import BPUPSubscriptions, DeviceType
|
|||
from homeassistant import core
|
||||
from homeassistant.components import fan
|
||||
from homeassistant.components.fan import DOMAIN as FAN_DOMAIN
|
||||
from homeassistant.const import STATE_ON, STATE_UNAVAILABLE
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_STOP, STATE_ON, STATE_UNAVAILABLE
|
||||
from homeassistant.core import CoreState
|
||||
from homeassistant.util import utcnow
|
||||
|
||||
from .common import patch_bond_device_state, setup_platform
|
||||
|
@ -167,3 +168,26 @@ async def test_polling_fails_and_recovers(hass: core.HomeAssistant):
|
|||
state = hass.states.get("fan.name_1")
|
||||
assert state.state == STATE_ON
|
||||
assert state.attributes[fan.ATTR_PERCENTAGE] == 33
|
||||
|
||||
|
||||
async def test_polling_stops_at_the_stop_event(hass: core.HomeAssistant):
|
||||
"""Test that polling stops at the stop event."""
|
||||
await setup_platform(
|
||||
hass, FAN_DOMAIN, ceiling_fan("name-1"), bond_device_id="test-device-id"
|
||||
)
|
||||
|
||||
with patch_bond_device_state(side_effect=asyncio.TimeoutError):
|
||||
async_fire_time_changed(hass, utcnow() + timedelta(seconds=230))
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get("fan.name_1").state == STATE_UNAVAILABLE
|
||||
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
|
||||
hass.state = CoreState.stopping
|
||||
await hass.async_block_till_done()
|
||||
|
||||
with patch_bond_device_state(return_value={"power": 1, "speed": 1}):
|
||||
async_fire_time_changed(hass, utcnow() + timedelta(seconds=230))
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get("fan.name_1").state == STATE_UNAVAILABLE
|
||||
|
|
Loading…
Reference in New Issue