From 4542d2e62bbc3fd6cc30e3d960e2b262f775aba0 Mon Sep 17 00:00:00 2001 From: Jonathan Horowitz Date: Wed, 28 Oct 2026 04:58:15 -0700 Subject: [PATCH] Make Chef iQ sensors unavailable when the probe stops broadcasting (#189188) Co-authored-by: Claude Fable 5.1 --- homeassistant/components/chef_iq/__init__.py | 9 ++-- .../components/chef_iq/coordinator.py | 54 +++++++++++++++++++ .../components/chef_iq/quality_scale.yaml | 10 ++-- homeassistant/components/chef_iq/sensor.py | 19 ------- tests/components/chef_iq/test_sensor.py | 31 +++++++---- 5 files changed, 85 insertions(+), 38 deletions(-) create mode 100644 homeassistant/components/chef_iq/coordinator.py diff --git a/homeassistant/components/chef_iq/__init__.py b/homeassistant/components/chef_iq/__init__.py index 24a1a239ffc6..7337fc2bb6d7 100644 --- a/homeassistant/components/chef_iq/__init__.py +++ b/homeassistant/components/chef_iq/__init__.py @@ -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, diff --git a/homeassistant/components/chef_iq/coordinator.py b/homeassistant/components/chef_iq/coordinator.py new file mode 100644 index 000000000000..2404fd5b21a8 --- /dev/null +++ b/homeassistant/components/chef_iq/coordinator.py @@ -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) diff --git a/homeassistant/components/chef_iq/quality_scale.yaml b/homeassistant/components/chef_iq/quality_scale.yaml index bf74f8e8a20a..2055d6e3fb87 100644 --- a/homeassistant/components/chef_iq/quality_scale.yaml +++ b/homeassistant/components/chef_iq/quality_scale.yaml @@ -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 diff --git a/homeassistant/components/chef_iq/sensor.py b/homeassistant/components/chef_iq/sensor.py index 24be7c57641f..eddfa8003f97 100644 --- a/homeassistant/components/chef_iq/sensor.py +++ b/homeassistant/components/chef_iq/sensor.py @@ -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 diff --git a/tests/components/chef_iq/test_sensor.py b/tests/components/chef_iq/test_sensor.py index 64784533e72e..b6617db39edf 100644 --- a/tests/components/chef_iq/test_sensor.py +++ b/tests/components/chef_iq/test_sensor.py @@ -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