From 56c2276630647cbca4caa7ab518052fe6479a4a7 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 7 Aug 2023 09:19:46 -1000 Subject: [PATCH] Restore sleepy oralb devices state at startup (#97983) --- homeassistant/components/oralb/sensor.py | 4 ++- tests/components/oralb/test_sensor.py | 35 +++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/oralb/sensor.py b/homeassistant/components/oralb/sensor.py index 76104c75164..16118361ab8 100644 --- a/homeassistant/components/oralb/sensor.py +++ b/homeassistant/components/oralb/sensor.py @@ -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( diff --git a/tests/components/oralb/test_sensor.py b/tests/components/oralb/test_sensor.py index d43997fe7ed..2abc27c8b14 100644 --- a/tests/components/oralb/test_sensor.py +++ b/tests/components/oralb/test_sensor.py @@ -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"