mirror of
https://github.com/home-assistant/core.git
synced 2026-12-03 04:08:19 -04:00
Make Chef iQ sensors unavailable when the probe stops broadcasting (#189188)
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5.1
parent
ee47c1da32
commit
4542d2e62b
@@ -5,18 +5,17 @@ import logging
|
||||
from chefiq_ble import ChefIqBluetoothDeviceData
|
||||
|
||||
from homeassistant.components.bluetooth import BluetoothScanningMode
|
||||
from homeassistant.components.bluetooth.passive_update_processor import (
|
||||
PassiveBluetoothProcessorCoordinator,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .coordinator import ChefIqPassiveBluetoothProcessorCoordinator
|
||||
|
||||
PLATFORMS: list[Platform] = [Platform.SENSOR]
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
type ChefIqConfigEntry = ConfigEntry[PassiveBluetoothProcessorCoordinator]
|
||||
type ChefIqConfigEntry = ConfigEntry[ChefIqPassiveBluetoothProcessorCoordinator]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ChefIqConfigEntry) -> None:
|
||||
@@ -25,7 +24,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ChefIqConfigEntry) -> No
|
||||
assert address is not None
|
||||
data = ChefIqBluetoothDeviceData()
|
||||
|
||||
coordinator = entry.runtime_data = PassiveBluetoothProcessorCoordinator(
|
||||
coordinator = entry.runtime_data = ChefIqPassiveBluetoothProcessorCoordinator(
|
||||
hass,
|
||||
_LOGGER,
|
||||
address=address,
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
"""Coordinator for the Chef iQ integration."""
|
||||
|
||||
from typing import override
|
||||
|
||||
from chefiq_ble import SensorUpdate
|
||||
|
||||
from homeassistant.components.bluetooth import (
|
||||
BluetoothChange,
|
||||
BluetoothServiceInfoBleak,
|
||||
)
|
||||
from homeassistant.components.bluetooth.passive_update_processor import (
|
||||
PassiveBluetoothProcessorCoordinator,
|
||||
)
|
||||
from homeassistant.core import callback
|
||||
|
||||
|
||||
class ChefIqPassiveBluetoothProcessorCoordinator(
|
||||
PassiveBluetoothProcessorCoordinator[SensorUpdate]
|
||||
):
|
||||
"""Passive coordinator that logs once when the probe stops and resumes broadcasting.
|
||||
|
||||
A probe stops advertising when it is docked, so its sensors become
|
||||
unavailable after the stale-advertisement window and come back with the
|
||||
next advertisement. Each transition is logged once.
|
||||
"""
|
||||
|
||||
_unavailable_logged = False
|
||||
|
||||
@callback
|
||||
@override
|
||||
def _async_handle_unavailable(
|
||||
self, service_info: BluetoothServiceInfoBleak
|
||||
) -> None:
|
||||
"""Handle the probe going unavailable."""
|
||||
super()._async_handle_unavailable(service_info)
|
||||
if not self._unavailable_logged:
|
||||
self._unavailable_logged = True
|
||||
self.logger.info(
|
||||
"Chef iQ probe %s stopped broadcasting; its sensors are unavailable",
|
||||
self.address,
|
||||
)
|
||||
|
||||
@callback
|
||||
@override
|
||||
def _async_handle_bluetooth_event(
|
||||
self,
|
||||
service_info: BluetoothServiceInfoBleak,
|
||||
change: BluetoothChange,
|
||||
) -> None:
|
||||
"""Handle a Bluetooth event."""
|
||||
super()._async_handle_bluetooth_event(service_info, change)
|
||||
if self._unavailable_logged and self.available:
|
||||
self._unavailable_logged = False
|
||||
self.logger.info("Chef iQ probe %s is broadcasting again", self.address)
|
||||
@@ -54,13 +54,15 @@ rules:
|
||||
entity-unavailable:
|
||||
status: done
|
||||
comment: |
|
||||
Availability is provided by the passive bluetooth coordinator. These are
|
||||
sleepy devices, so once seen the entities use assumed_state rather than
|
||||
becoming unavailable.
|
||||
Entities take the passive Bluetooth coordinator's availability and become
|
||||
unavailable once the probe has stopped broadcasting for the stale
|
||||
advertisement window, for example while it is docked.
|
||||
integration-owner: done
|
||||
log-when-unavailable:
|
||||
status: done
|
||||
comment: Handled by the passive bluetooth coordinator.
|
||||
comment: |
|
||||
The coordinator logs once when the probe stops broadcasting and once when
|
||||
it is seen again.
|
||||
parallel-updates: done
|
||||
reauthentication-flow:
|
||||
status: exempt
|
||||
|
||||
@@ -149,22 +149,3 @@ class ChefIqBluetoothSensorEntity(
|
||||
def native_value(self) -> float | int | None:
|
||||
"""Return the native value."""
|
||||
return self.processor.entity_data.get(self.entity_key)
|
||||
|
||||
@property
|
||||
@override
|
||||
def available(self) -> bool:
|
||||
"""Return True if entity is available.
|
||||
|
||||
The sensor is only created when the device is seen.
|
||||
|
||||
Since these are sleepy devices which stop broadcasting
|
||||
when not in use, we can't rely on the last update time
|
||||
so once we have seen the device we always return True.
|
||||
"""
|
||||
return True
|
||||
|
||||
@property
|
||||
@override
|
||||
def assumed_state(self) -> bool:
|
||||
"""Return True if the device is no longer broadcasting."""
|
||||
return not self.processor.available
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"""Test the Chef iQ sensors."""
|
||||
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
import time
|
||||
|
||||
import pytest
|
||||
@@ -19,7 +20,7 @@ from syrupy.assertion import SnapshotAssertion
|
||||
from homeassistant.components.bluetooth import (
|
||||
FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS,
|
||||
)
|
||||
from homeassistant.const import ATTR_ASSUMED_STATE
|
||||
from homeassistant.const import STATE_UNAVAILABLE
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.util import dt as dt_util
|
||||
@@ -27,6 +28,12 @@ from homeassistant.util import dt as dt_util
|
||||
from . import CHEFIQ_STATUS_SERVICE_INFO, CHEFIQ_TEMPERATURE_SERVICE_INFO
|
||||
|
||||
|
||||
def _food_state(hass: HomeAssistant) -> str:
|
||||
state = hass.states.get("sensor.cq60_079a_food_temperature")
|
||||
assert state is not None
|
||||
return state.state
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_sensors(
|
||||
hass: HomeAssistant,
|
||||
@@ -51,11 +58,13 @@ async def test_sensors(
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_sleepy_device_keeps_state(
|
||||
async def test_probe_unavailable_while_not_broadcasting(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""Test the probe keeps its state and goes to assumed_state when idle."""
|
||||
"""Test the sensors go unavailable, logged once, until the probe is seen again."""
|
||||
caplog.set_level(logging.INFO, logger="homeassistant.components.chef_iq")
|
||||
start_monotonic = time.monotonic()
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
@@ -63,9 +72,7 @@ async def test_sleepy_device_keeps_state(
|
||||
|
||||
inject_bluetooth_service_info(hass, CHEFIQ_TEMPERATURE_SERVICE_INFO)
|
||||
await hass.async_block_till_done()
|
||||
food = hass.states.get("sensor.cq60_079a_food_temperature")
|
||||
assert food.state == "29.9"
|
||||
assert ATTR_ASSUMED_STATE not in food.attributes
|
||||
assert _food_state(hass) == "29.9"
|
||||
|
||||
# Fast-forward past the stale-advertisement window with no advertisements.
|
||||
monotonic_now = start_monotonic + FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS + 1
|
||||
@@ -80,7 +87,11 @@ async def test_sleepy_device_keeps_state(
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Sleepy devices keep their last value and report assumed_state.
|
||||
food = hass.states.get("sensor.cq60_079a_food_temperature")
|
||||
assert food.state == "29.9"
|
||||
assert food.attributes[ATTR_ASSUMED_STATE] is True
|
||||
assert _food_state(hass) == STATE_UNAVAILABLE
|
||||
assert caplog.text.count("stopped broadcasting") == 1
|
||||
|
||||
# The probe is seen again: the sensors recover and that is logged once too.
|
||||
inject_bluetooth_service_info(hass, CHEFIQ_TEMPERATURE_SERVICE_INFO)
|
||||
await hass.async_block_till_done()
|
||||
assert _food_state(hass) == "29.9"
|
||||
assert caplog.text.count("broadcasting again") == 1
|
||||
|
||||
Reference in New Issue
Block a user