Restore sleepy oralb devices state at startup (#97983)

pull/97991/head
J. Nick Koston 2023-08-07 09:19:46 -10:00 committed by GitHub
parent b7f936fcdd
commit 56c2276630
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 2 deletions

View File

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

View File

@ -9,7 +9,10 @@ from homeassistant.components.bluetooth import (
async_address_present,
)
from homeassistant.components.oralb.const import DOMAIN
from homeassistant.const import ATTR_ASSUMED_STATE, ATTR_FRIENDLY_NAME
from homeassistant.const import (
ATTR_ASSUMED_STATE,
ATTR_FRIENDLY_NAME,
)
from homeassistant.core import HomeAssistant
from homeassistant.util import dt as dt_util
@ -31,6 +34,7 @@ async def test_sensors(
hass: HomeAssistant, entity_registry_enabled_by_default: None
) -> None:
"""Test setting up creates the sensors."""
start_monotonic = time.monotonic()
entry = MockConfigEntry(
domain=DOMAIN,
unique_id=ORALB_SERVICE_INFO.address,
@ -59,6 +63,30 @@ async def test_sensors(
assert await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done()
# 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()
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
# All of these devices are sleepy so we should still be available
toothbrush_sensor = hass.states.get(
"sensor.smart_series_7000_48be_toothbrush_state"
)
toothbrush_sensor_attrs = toothbrush_sensor.attributes
assert toothbrush_sensor.state == "running"
async def test_sensors_io_series_4(
hass: HomeAssistant, entity_registry_enabled_by_default: None
@ -103,6 +131,11 @@ async def test_sensors_io_series_4(
async_address_present(hass, ORALB_IO_SERIES_4_SERVICE_INFO.address) is False
)
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()
toothbrush_sensor = hass.states.get("sensor.io_series_4_48be_mode")
# Sleepy devices should keep their state over time
assert toothbrush_sensor.state == "gum care"