1
0
mirror of https://github.com/home-assistant/core.git synced 2026-02-15 07:36:16 +00:00

Add ambient temperature sensor to ToGrill (#159798)

This commit is contained in:
Panda-NZ
2025-12-30 21:44:23 +13:00
committed by GitHub
parent 75ea42a834
commit 2d1a672de5
8 changed files with 1249 additions and 3 deletions

View File

@@ -19,7 +19,7 @@ from homeassistant.const import CONF_ADDRESS, CONF_MODEL
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import AbortFlow
from .const import CONF_PROBE_COUNT, DOMAIN
from .const import CONF_HAS_AMBIENT, CONF_PROBE_COUNT, DOMAIN
from .coordinator import LOGGER
_TIMEOUT = 10
@@ -48,6 +48,7 @@ async def read_config_data(
CONF_MODEL: info.name,
CONF_ADDRESS: info.address,
CONF_PROBE_COUNT: packet_a0.probe_count,
CONF_HAS_AMBIENT: packet_a0.ambient,
}

View File

@@ -5,4 +5,5 @@ DOMAIN = "togrill"
MAX_PROBE_COUNT = 6
CONF_PROBE_COUNT = "probe_count"
CONF_HAS_AMBIENT = "has_ambient"
CONF_VERSION = "version"

View File

@@ -20,7 +20,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from . import ToGrillConfigEntry
from .const import CONF_PROBE_COUNT, MAX_PROBE_COUNT
from .const import CONF_HAS_AMBIENT, CONF_PROBE_COUNT, MAX_PROBE_COUNT
from .coordinator import ToGrillCoordinator
from .entity import ToGrillEntity
@@ -63,6 +63,27 @@ def _get_temperature_description(probe_number: int):
)
def _get_ambient_temperature(packet: Packet) -> StateType:
"""Extract ambient temperature from packet.
The ambient temperature is the last value in the temperatures list
when the device has an ambient sensor.
"""
assert isinstance(packet, PacketA1Notify)
if not packet.temperatures:
return None
# Ambient is always the last temperature value
temperature = packet.temperatures[-1]
if temperature is None:
return None
return temperature
def _ambient_supported(config: Mapping[str, Any]) -> bool:
"""Check if ambient sensor is supported."""
return config.get(CONF_HAS_AMBIENT, False)
ENTITY_DESCRIPTIONS = (
ToGrillSensorEntityDescription(
key="battery",
@@ -78,6 +99,17 @@ ENTITY_DESCRIPTIONS = (
_get_temperature_description(probe_number)
for probe_number in range(1, MAX_PROBE_COUNT + 1)
],
ToGrillSensorEntityDescription(
key="ambient_temperature",
translation_key="ambient_temperature",
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=1,
packet_type=PacketA1Notify.type,
packet_extract=_get_ambient_temperature,
entity_supported=_ambient_supported,
),
)

View File

@@ -98,6 +98,11 @@
"well_done": "Well done"
}
}
},
"sensor": {
"ambient_temperature": {
"name": "Ambient temperature"
}
}
},
"exceptions": {

View File

@@ -7,7 +7,11 @@ import pytest
from togrill_bluetooth.client import Client
from togrill_bluetooth.packets import Packet, PacketA0Notify, PacketNotify
from homeassistant.components.togrill.const import CONF_PROBE_COUNT, DOMAIN
from homeassistant.components.togrill.const import (
CONF_HAS_AMBIENT,
CONF_PROBE_COUNT,
DOMAIN,
)
from homeassistant.const import CONF_ADDRESS, CONF_MODEL
from . import TOGRILL_SERVICE_INFO
@@ -29,6 +33,21 @@ def mock_entry() -> MockConfigEntry:
)
@pytest.fixture
def mock_entry_with_ambient() -> MockConfigEntry:
"""Create hass config fixture with ambient sensor enabled."""
return MockConfigEntry(
domain=DOMAIN,
data={
CONF_ADDRESS: TOGRILL_SERVICE_INFO.address,
CONF_MODEL: "Pro-05",
CONF_PROBE_COUNT: 2,
CONF_HAS_AMBIENT: True,
},
unique_id=TOGRILL_SERVICE_INFO.address,
)
@pytest.fixture(scope="module")
def mock_unload_entry() -> Generator[AsyncMock]:
"""Override async_unload_entry."""

File diff suppressed because it is too large Load Diff

View File

@@ -42,6 +42,7 @@ async def test_user_selection(
"address": TOGRILL_SERVICE_INFO.address,
"model": "Pro-05",
"probe_count": 0,
"has_ambient": False,
}
assert result["title"] == "Pro-05"
assert result["result"].unique_id == TOGRILL_SERVICE_INFO.address
@@ -176,6 +177,7 @@ async def test_bluetooth(
"address": TOGRILL_SERVICE_INFO.address,
"model": "Pro-05",
"probe_count": 0,
"has_ambient": False,
}
assert result["title"] == "Pro-05"
assert result["result"].unique_id == TOGRILL_SERVICE_INFO.address

View File

@@ -70,6 +70,50 @@ async def test_setup(
await snapshot_platform(hass, entity_registry, snapshot, mock_entry.entry_id)
@pytest.mark.parametrize(
"packets",
[
pytest.param([], id="no_data"),
pytest.param(
[PacketA1Notify([10, 20, 25])],
id="ambient_temp_data",
),
pytest.param(
[PacketA1Notify([10, None, 25])],
id="ambient_temp_with_missing_probe",
),
pytest.param(
[PacketA1Notify([])],
id="ambient_empty_temperatures",
),
pytest.param(
[PacketA1Notify([10, 20, None])],
id="ambient_temp_none",
),
],
)
async def test_setup_with_ambient(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
mock_entry_with_ambient: MockConfigEntry,
mock_client: Mock,
packets,
) -> None:
"""Test the sensors with ambient temperature sensor enabled."""
inject_bluetooth_service_info(hass, TOGRILL_SERVICE_INFO)
await setup_entry(hass, mock_entry_with_ambient, [Platform.SENSOR])
for packet in packets:
mock_client.mocked_notify(packet)
await snapshot_platform(
hass, entity_registry, snapshot, mock_entry_with_ambient.entry_id
)
async def test_device_disconnected(
hass: HomeAssistant,
mock_entry: MockConfigEntry,
@@ -116,3 +160,25 @@ async def test_device_discovered(
state = hass.states.get(entity_id)
assert state
assert state.state == "0"
async def test_ambient_sensor_not_created_without_config(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_entry: MockConfigEntry,
mock_client: Mock,
) -> None:
"""Test ambient temperature sensor is not created when not configured."""
inject_bluetooth_service_info(hass, TOGRILL_SERVICE_INFO)
await setup_entry(hass, mock_entry, [Platform.SENSOR])
entity_id = "sensor.pro_05_ambient_temperature"
# Entity should not exist when CONF_HAS_AMBIENT is not set
state = hass.states.get(entity_id)
assert state is None
# Verify it's not in the entity registry
entry = entity_registry.async_get(entity_id)
assert entry is None