mirror of
https://github.com/home-assistant/core.git
synced 2025-05-02 05:07:52 +00:00
Restore xiaomi_ble state at start when device is in range or sleepy (#97979)
This commit is contained in:
parent
b34ce3c792
commit
fb12c237ab
@ -122,7 +122,9 @@ async def async_setup_entry(
|
|||||||
XiaomiBluetoothSensorEntity, async_add_entities
|
XiaomiBluetoothSensorEntity, async_add_entities
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
entry.async_on_unload(coordinator.async_register_processor(processor))
|
entry.async_on_unload(
|
||||||
|
coordinator.async_register_processor(processor, BinarySensorEntityDescription)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class XiaomiBluetoothSensorEntity(
|
class XiaomiBluetoothSensorEntity(
|
||||||
|
@ -195,7 +195,9 @@ async def async_setup_entry(
|
|||||||
XiaomiBluetoothSensorEntity, async_add_entities
|
XiaomiBluetoothSensorEntity, async_add_entities
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
entry.async_on_unload(coordinator.async_register_processor(processor))
|
entry.async_on_unload(
|
||||||
|
coordinator.async_register_processor(processor, SensorEntityDescription)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class XiaomiBluetoothSensorEntity(
|
class XiaomiBluetoothSensorEntity(
|
||||||
|
@ -367,3 +367,64 @@ async def test_sleepy_device(hass: HomeAssistant) -> None:
|
|||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert entry.data[CONF_SLEEPY_DEVICE] is True
|
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:66:E5:67",
|
||||||
|
)
|
||||||
|
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_bleak(
|
||||||
|
hass,
|
||||||
|
make_advertisement(
|
||||||
|
"A4:C1:38:66:E5:67",
|
||||||
|
b"@0\xd6\x03$\x19\x10\x01\x00",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert len(hass.states.async_all()) == 1
|
||||||
|
|
||||||
|
opening_sensor = hass.states.get("binary_sensor.door_window_sensor_e567_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.door_window_sensor_e567_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.door_window_sensor_e567_opening")
|
||||||
|
|
||||||
|
# Sleepy devices should keep their state over time and restore it
|
||||||
|
assert opening_sensor.state == STATE_ON
|
||||||
|
|
||||||
|
assert entry.data[CONF_SLEEPY_DEVICE] is True
|
||||||
|
@ -7,7 +7,7 @@ from homeassistant.components.bluetooth import (
|
|||||||
FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS,
|
FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS,
|
||||||
)
|
)
|
||||||
from homeassistant.components.sensor import ATTR_STATE_CLASS
|
from homeassistant.components.sensor import ATTR_STATE_CLASS
|
||||||
from homeassistant.components.xiaomi_ble.const import DOMAIN
|
from homeassistant.components.xiaomi_ble.const import CONF_SLEEPY_DEVICE, DOMAIN
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_FRIENDLY_NAME,
|
ATTR_FRIENDLY_NAME,
|
||||||
ATTR_UNIT_OF_MEASUREMENT,
|
ATTR_UNIT_OF_MEASUREMENT,
|
||||||
@ -713,7 +713,7 @@ async def test_unavailable(hass: HomeAssistant) -> None:
|
|||||||
|
|
||||||
|
|
||||||
async def test_sleepy_device(hass: HomeAssistant) -> None:
|
async def test_sleepy_device(hass: HomeAssistant) -> None:
|
||||||
"""Test normal device goes to unavailable after 60 minutes."""
|
"""Test sleepy devices stay available."""
|
||||||
start_monotonic = time.monotonic()
|
start_monotonic = time.monotonic()
|
||||||
|
|
||||||
entry = MockConfigEntry(
|
entry = MockConfigEntry(
|
||||||
@ -759,3 +759,64 @@ async def test_sleepy_device(hass: HomeAssistant) -> None:
|
|||||||
|
|
||||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
|
||||||
|
async def test_sleepy_device_restore_state(hass: HomeAssistant) -> None:
|
||||||
|
"""Test sleepy devices stay available."""
|
||||||
|
start_monotonic = time.monotonic()
|
||||||
|
|
||||||
|
entry = MockConfigEntry(
|
||||||
|
domain=DOMAIN,
|
||||||
|
unique_id="50:FB:19:1B:B5:DC",
|
||||||
|
)
|
||||||
|
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_bleak(hass, MISCALE_V1_SERVICE_INFO)
|
||||||
|
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert len(hass.states.async_all()) == 2
|
||||||
|
|
||||||
|
mass_non_stabilized_sensor = hass.states.get(
|
||||||
|
"sensor.mi_smart_scale_b5dc_mass_non_stabilized"
|
||||||
|
)
|
||||||
|
assert mass_non_stabilized_sensor.state == "86.55"
|
||||||
|
|
||||||
|
# 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()
|
||||||
|
|
||||||
|
mass_non_stabilized_sensor = hass.states.get(
|
||||||
|
"sensor.mi_smart_scale_b5dc_mass_non_stabilized"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Sleepy devices should keep their state over time
|
||||||
|
assert mass_non_stabilized_sensor.state == "86.55"
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
mass_non_stabilized_sensor = hass.states.get(
|
||||||
|
"sensor.mi_smart_scale_b5dc_mass_non_stabilized"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Sleepy devices should keep their state over time and restore it
|
||||||
|
assert mass_non_stabilized_sensor.state == "86.55"
|
||||||
|
|
||||||
|
assert entry.data[CONF_SLEEPY_DEVICE] is True
|
||||||
|
Loading…
x
Reference in New Issue
Block a user