Restore bthome state at start when device is in range or sleepy (#97949)

This commit is contained in:
J. Nick Koston 2023-08-07 06:41:53 -10:00 committed by GitHub
parent d56484e2d6
commit a234ab51fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 132 additions and 2 deletions

View File

@ -186,7 +186,9 @@ async def async_setup_entry(
BTHomeBluetoothBinarySensorEntity, async_add_entities
)
)
entry.async_on_unload(coordinator.async_register_processor(processor))
entry.async_on_unload(
coordinator.async_register_processor(processor, BinarySensorEntityDescription)
)
class BTHomeBluetoothBinarySensorEntity(

View File

@ -383,7 +383,9 @@ async def async_setup_entry(
BTHomeBluetoothSensorEntity, async_add_entities
)
)
entry.async_on_unload(coordinator.async_register_processor(processor))
entry.async_on_unload(
coordinator.async_register_processor(processor, SensorEntityDescription)
)
class BTHomeBluetoothSensorEntity(

View File

@ -308,3 +308,65 @@ async def test_sleepy_device(hass: HomeAssistant) -> None:
assert await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done()
async def test_sleepy_device_restores_state(hass: HomeAssistant) -> None:
"""Test sleepy device does not go to unavailable after 60 minutes."""
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\x11\x01",
),
)
await hass.async_block_till_done()
assert len(hass.states.async_all()) == 1
opening_sensor = hass.states.get("binary_sensor.test_device_18b2_opening")
assert opening_sensor.state == STATE_ON
# 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()
opening_sensor = hass.states.get("binary_sensor.test_device_18b2_opening")
# Sleepy devices should keep their state over time
assert opening_sensor.state == STATE_ON
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()
opening_sensor = hass.states.get("binary_sensor.test_device_18b2_opening")
# Sleepy devices should keep their state on restore
assert opening_sensor.state == STATE_ON

View File

@ -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