Add function test buttons to Bosch SHC (#194275)

This commit is contained in:
Thomas
2026-11-27 12:00:47 -05:00
committed by GitHub
parent 8520c61a80
commit e1f337f23b
4 changed files with 182 additions and 0 deletions
@@ -13,6 +13,7 @@ from boschshcpy import (
SHCSmokeDetector,
)
from boschshcpy.device import SHCDevice
from boschshcpy.services_impl import DetectionTestService, WalkTestService
from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
from homeassistant.const import EntityCategory
@@ -94,6 +95,39 @@ PREVIEW_MIN_BRIGHTNESS_DESCRIPTION = SHCButtonEntityDescription[SHCMicromoduleDi
)
def _function_test_start(device: SHCMotionDetector2) -> None:
if device.supports_walk_test:
device.set_walk_state_request(WalkTestService.WalkStateRequest.WALK_STATE_START)
else:
device.set_detection_state_request(
DetectionTestService.DetectionStateRequest.DETECTION_STATE_START
)
def _function_test_stop(device: SHCMotionDetector2) -> None:
if device.supports_walk_test:
device.set_walk_state_request(WalkTestService.WalkStateRequest.WALK_STATE_STOP)
else:
device.set_detection_state_request(
DetectionTestService.DetectionStateRequest.DETECTION_STATE_STOP
)
FUNCTION_TEST_START_DESCRIPTION = SHCButtonEntityDescription[SHCMotionDetector2](
key="function_test_start",
translation_key="function_test_start",
entity_category=EntityCategory.DIAGNOSTIC,
press_fn=_function_test_start,
)
FUNCTION_TEST_STOP_DESCRIPTION = SHCButtonEntityDescription[SHCMotionDetector2](
key="function_test_stop",
translation_key="function_test_stop",
entity_category=EntityCategory.DIAGNOSTIC,
press_fn=_function_test_stop,
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: BoschConfigEntry,
@@ -127,6 +161,20 @@ async def async_setup_entry(
for device in session.device_helper.motion_detectors2
if device.supports_tamper_reset
)
for device in session.device_helper.motion_detectors2:
if not (device.supports_walk_test or device.supports_detection_test):
continue
descriptions = (FUNCTION_TEST_START_DESCRIPTION, FUNCTION_TEST_STOP_DESCRIPTION)
entities.extend(
SHCButton(
hass=hass,
device=device,
parent_id=shc_info.unique_id,
entry_id=config_entry.entry_id,
description=description,
)
for description in descriptions
)
entities.extend(
SHCButton(
hass=hass,
@@ -81,6 +81,12 @@
}
},
"button": {
"function_test_start": {
"name": "Start function test"
},
"function_test_stop": {
"name": "Stop function test"
},
"preview_max_brightness": {
"name": "Preview max brightness"
},
+4
View File
@@ -1035,6 +1035,8 @@ def motion_detector2_device(
occupied: bool = False,
was_tampered: bool = False,
temperature: float = 21.5,
supports_walk_test: bool = False,
supports_detection_test: bool = False,
) -> SHCMotionDetector2:
"""Build a minimal device double for the motion_detectors2 bucket."""
device = create_autospec(SHCMotionDetector2, instance=True, spec_set=True)
@@ -1047,6 +1049,8 @@ def motion_detector2_device(
device.device_services = [FakeLatestMotionService()]
device.deleted = False
device.status = "AVAILABLE"
device.supports_walk_test = supports_walk_test
device.supports_detection_test = supports_detection_test
device.pet_immunity_enabled = pet_immunity_enabled
device.tamper_protection_enabled = tamper_protection_enabled
device.supports_smart_sensitivity = supports_smart_sensitivity
+124
View File
@@ -3,6 +3,7 @@
from collections.abc import Generator
from unittest.mock import MagicMock, patch
from boschshcpy.services_impl import DetectionTestService, WalkTestService
import pytest
from pytest_homeassistant.common import MockConfigEntry
@@ -253,3 +254,126 @@ async def test_dimmer_preview_buttons_disabled_by_default(
entry = entity_registry.async_get(entity_id)
assert entry is not None
assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION
@pytest.mark.parametrize(
("entity_id", "request_value"),
[
(
"button.motion_detector_start_function_test",
WalkTestService.WalkStateRequest.WALK_STATE_START,
),
(
"button.motion_detector_stop_function_test",
WalkTestService.WalkStateRequest.WALK_STATE_STOP,
),
],
)
@pytest.mark.parametrize(
"device_buckets",
[{"motion_detectors2": [motion_detector2_device(supports_walk_test=True)]}],
indirect=True,
)
@pytest.mark.usefixtures("mock_session")
async def test_walk_test_buttons(
hass: HomeAssistant,
mock_session: MagicMock,
mock_config_entry: MockConfigEntry,
entity_id: str,
request_value: WalkTestService.WalkStateRequest,
) -> None:
"""The walk test buttons send a start or stop request."""
await setup_integration(hass, mock_config_entry)
device = mock_session.device_helper.motion_detectors2[0]
await hass.services.async_call(
BUTTON_DOMAIN,
ButtonService.PRESS,
{TargetKey.ENTITY_ID: entity_id},
blocking=True,
)
device.set_walk_state_request.assert_called_with(request_value)
@pytest.mark.parametrize(
("entity_id", "request_value"),
[
(
"button.motion_detector_start_function_test",
DetectionTestService.DetectionStateRequest.DETECTION_STATE_START,
),
(
"button.motion_detector_stop_function_test",
DetectionTestService.DetectionStateRequest.DETECTION_STATE_STOP,
),
],
)
@pytest.mark.parametrize(
"device_buckets",
[{"motion_detectors2": [motion_detector2_device(supports_detection_test=True)]}],
indirect=True,
)
@pytest.mark.usefixtures("mock_session")
async def test_detection_test_buttons(
hass: HomeAssistant,
mock_session: MagicMock,
mock_config_entry: MockConfigEntry,
entity_id: str,
request_value: DetectionTestService.DetectionStateRequest,
) -> None:
"""Without a walk test service, the detection test buttons are created."""
await setup_integration(hass, mock_config_entry)
device = mock_session.device_helper.motion_detectors2[0]
await hass.services.async_call(
BUTTON_DOMAIN,
ButtonService.PRESS,
{TargetKey.ENTITY_ID: entity_id},
blocking=True,
)
device.set_detection_state_request.assert_called_with(request_value)
@pytest.mark.parametrize(
"device_buckets",
[{"motion_detectors2": [motion_detector2_device()]}],
indirect=True,
)
@pytest.mark.usefixtures("mock_session")
async def test_no_function_test_without_service(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
) -> None:
"""A Motion Detector II without a test service gets no function test buttons."""
await setup_integration(hass, mock_config_entry)
assert hass.states.get("button.motion_detector_start_function_test") is None
@pytest.mark.parametrize(
"device_buckets",
[
{"motion_detectors2": [motion_detector2_device(supports_walk_test=True)]},
{"motion_detectors2": [motion_detector2_device(supports_detection_test=True)]},
],
ids=["walk", "detection"],
indirect=True,
)
@pytest.mark.usefixtures("mock_session")
async def test_function_test_unique_ids_independent_of_service(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
) -> None:
"""Unique IDs do not depend on which test service the device exposes."""
await setup_integration(hass, mock_config_entry)
for entity_id, suffix in (
("button.motion_detector_start_function_test", "function_test_start"),
("button.motion_detector_stop_function_test", "function_test_stop"),
):
entry = entity_registry.async_get(entity_id)
assert entry is not None
assert entry.unique_id.endswith(suffix)