Restore bthome state at start when device is in range or sleepy (#97949)
This commit is contained in:
parent
d56484e2d6
commit
a234ab51fe
4 changed files with 132 additions and 2 deletions
|
@ -1195,3 +1195,67 @@ async def test_sleepy_device(hass: HomeAssistant) -> None:
|
|||
await hass.async_block_till_done()
|
||||
|
||||
assert entry.data[CONF_SLEEPY_DEVICE] is True
|
||||
|
||||
|
||||
async def test_sleepy_device_restore_state(hass: HomeAssistant) -> None:
|
||||
"""Test sleepy device does not go to unavailable after 60 minutes and restores state."""
|
||||
start_monotonic = time.monotonic()
|
||||
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id="A4:C1:38:8D:18:B2",
|
||||
data={},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
inject_bluetooth_service_info(
|
||||
hass,
|
||||
make_bthome_v2_adv(
|
||||
"A4:C1:38:8D:18:B2",
|
||||
b"\x44\x04\x13\x8a\x01",
|
||||
),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.states.async_all()) == 1
|
||||
|
||||
pressure_sensor = hass.states.get("sensor.test_device_18b2_pressure")
|
||||
|
||||
assert pressure_sensor.state == "1008.83"
|
||||
|
||||
# Fastforward time without BLE advertisements
|
||||
monotonic_now = start_monotonic + FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS + 1
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.bluetooth.manager.MONOTONIC_TIME",
|
||||
return_value=monotonic_now,
|
||||
), patch_all_discovered_devices([]):
|
||||
async_fire_time_changed(
|
||||
hass,
|
||||
dt_util.utcnow()
|
||||
+ timedelta(seconds=FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS + 1),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
pressure_sensor = hass.states.get("sensor.test_device_18b2_pressure")
|
||||
|
||||
# Sleepy devices should keep their state over time
|
||||
assert pressure_sensor.state == "1008.83"
|
||||
|
||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
pressure_sensor = hass.states.get("sensor.test_device_18b2_pressure")
|
||||
|
||||
# Sleepy devices should keep their state over time and restore it
|
||||
assert pressure_sensor.state == "1008.83"
|
||||
|
||||
assert entry.data[CONF_SLEEPY_DEVICE] is True
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue