1
0
mirror of https://github.com/home-assistant/core.git synced 2026-04-02 08:26:41 +01:00

Add ambient temperature range controls to ToGrill integration (#165235)

This commit is contained in:
Panda-NZ
2026-03-10 11:40:30 +13:00
committed by GitHub
parent bf846e0756
commit a36733c4dc
5 changed files with 1850 additions and 2 deletions

View File

@@ -32,7 +32,7 @@ from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import CONNECTION_BLUETOOTH, DeviceInfo
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import CONF_PROBE_COUNT, DOMAIN
from .const import CONF_HAS_AMBIENT, CONF_PROBE_COUNT, DOMAIN
type ToGrillConfigEntry = ConfigEntry[ToGrillCoordinator]
@@ -213,6 +213,8 @@ class ToGrillCoordinator(DataUpdateCoordinator[dict[tuple[int, int | None], Pack
await client.request(PacketA1Notify)
for probe in range(1, self.config_entry.data[CONF_PROBE_COUNT] + 1):
await client.write(PacketA8Write(probe=probe))
if self.config_entry.data.get(CONF_HAS_AMBIENT):
await client.write(PacketA8Write(probe=0))
except BleakError as exc:
raise DeviceFailed(f"Device failed {exc}") from exc
return self.data

View File

@@ -27,7 +27,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
@@ -123,12 +123,64 @@ def _get_temperature_descriptions(
)
def _get_ambient_temperatures(
coordinator: ToGrillCoordinator, alarm_type: AlarmType
) -> tuple[float | None, float | None]:
if not (packet := coordinator.get_packet(PacketA8Notify, 0)):
return None, None
if packet.alarm_type != alarm_type:
return None, None
return packet.temperature_1, packet.temperature_2
ENTITY_DESCRIPTIONS = (
*[
description
for probe_number in range(1, MAX_PROBE_COUNT + 1)
for description in _get_temperature_descriptions(probe_number)
],
ToGrillNumberEntityDescription(
key="ambient_temperature_minimum",
translation_key="ambient_temperature_minimum",
device_class=NumberDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
native_min_value=0,
native_max_value=400,
mode=NumberMode.BOX,
icon="mdi:thermometer-chevron-down",
set_packet=lambda coordinator, value: PacketA300Write(
probe=0,
minimum=None if value == 0.0 else value,
maximum=_get_ambient_temperatures(coordinator, AlarmType.TEMPERATURE_RANGE)[
1
],
),
get_value=lambda x: _get_ambient_temperatures(x, AlarmType.TEMPERATURE_RANGE)[
0
],
entity_supported=lambda x: x.get(CONF_HAS_AMBIENT, False),
),
ToGrillNumberEntityDescription(
key="ambient_temperature_maximum",
translation_key="ambient_temperature_maximum",
device_class=NumberDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
native_min_value=0,
native_max_value=400,
mode=NumberMode.BOX,
icon="mdi:thermometer-chevron-up",
set_packet=lambda coordinator, value: PacketA300Write(
probe=0,
minimum=_get_ambient_temperatures(coordinator, AlarmType.TEMPERATURE_RANGE)[
0
],
maximum=None if value == 0.0 else value,
),
get_value=lambda x: _get_ambient_temperatures(x, AlarmType.TEMPERATURE_RANGE)[
1
],
entity_supported=lambda x: x.get(CONF_HAS_AMBIENT, False),
),
ToGrillNumberEntityDescription(
key="alarm_interval",
translation_key="alarm_interval",

View File

@@ -55,6 +55,12 @@
"alarm_interval": {
"name": "Alarm interval"
},
"ambient_temperature_maximum": {
"name": "Ambient maximum temperature"
},
"ambient_temperature_minimum": {
"name": "Ambient minimum temperature"
},
"temperature_maximum": {
"name": "Maximum temperature"
},

File diff suppressed because it is too large Load Diff

View File

@@ -77,6 +77,150 @@ 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(
[
PacketA8Notify(
probe=0,
alarm_type=PacketA8Notify.AlarmType.TEMPERATURE_RANGE,
temperature_1=5.0,
temperature_2=300.0,
),
],
id="ambient_with_range",
),
pytest.param(
[
PacketA8Notify(
probe=0,
alarm_type=None,
temperature_1=5.0,
temperature_2=300.0,
),
],
id="ambient_wrong_alarm_type",
),
],
)
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 numbers with ambient sensor enabled."""
inject_bluetooth_service_info(hass, TOGRILL_SERVICE_INFO)
await setup_entry(hass, mock_entry_with_ambient, [Platform.NUMBER])
for packet in packets:
mock_client.mocked_notify(packet)
await snapshot_platform(
hass, entity_registry, snapshot, mock_entry_with_ambient.entry_id
)
@pytest.mark.parametrize(
("packets", "entity_id", "value", "write_packet"),
[
pytest.param(
[
PacketA8Notify(
probe=0,
alarm_type=PacketA8Notify.AlarmType.TEMPERATURE_RANGE,
temperature_1=5.0,
temperature_2=300.0,
),
],
"number.pro_05_ambient_minimum_temperature",
10.0,
PacketA300Write(probe=0, minimum=10.0, maximum=300.0),
id="ambient_minimum",
),
pytest.param(
[
PacketA8Notify(
probe=0,
alarm_type=PacketA8Notify.AlarmType.TEMPERATURE_RANGE,
temperature_1=5.0,
temperature_2=300.0,
),
],
"number.pro_05_ambient_minimum_temperature",
0.0,
PacketA300Write(probe=0, minimum=None, maximum=300.0),
id="ambient_minimum_clear",
),
pytest.param(
[
PacketA8Notify(
probe=0,
alarm_type=PacketA8Notify.AlarmType.TEMPERATURE_RANGE,
temperature_1=5.0,
temperature_2=300.0,
),
],
"number.pro_05_ambient_maximum_temperature",
350.0,
PacketA300Write(probe=0, minimum=5.0, maximum=350.0),
id="ambient_maximum",
),
pytest.param(
[
PacketA8Notify(
probe=0,
alarm_type=PacketA8Notify.AlarmType.TEMPERATURE_RANGE,
temperature_1=5.0,
temperature_2=300.0,
),
],
"number.pro_05_ambient_maximum_temperature",
0.0,
PacketA300Write(probe=0, minimum=5.0, maximum=None),
id="ambient_maximum_clear",
),
],
)
async def test_set_ambient_number(
hass: HomeAssistant,
mock_entry_with_ambient: MockConfigEntry,
mock_client: Mock,
packets,
entity_id,
value,
write_packet,
) -> None:
"""Test setting ambient temperature numbers."""
inject_bluetooth_service_info(hass, TOGRILL_SERVICE_INFO)
await setup_entry(hass, mock_entry_with_ambient, [Platform.NUMBER])
for packet in packets:
mock_client.mocked_notify(packet)
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
service_data={
ATTR_VALUE: value,
},
target={
ATTR_ENTITY_ID: entity_id,
},
blocking=True,
)
mock_client.write.assert_any_call(write_packet)
@pytest.mark.parametrize(
("packets", "entity_id", "value", "write_packet"),
[