diff --git a/homeassistant/components/bosch_shc/button.py b/homeassistant/components/bosch_shc/button.py index c6f073a03d33..0807113efca4 100644 --- a/homeassistant/components/bosch_shc/button.py +++ b/homeassistant/components/bosch_shc/button.py @@ -4,7 +4,13 @@ from collections.abc import Callable from dataclasses import dataclass from typing import TYPE_CHECKING, override -from boschshcpy import SHCMotionDetector2, SHCOutdoorSiren, SHCSmokeDetector +from boschshcpy import ( + SHCMotionDetector2, + SHCOutdoorSiren, + SHCSmartPlug, + SHCSmartPlugCompact, + SHCSmokeDetector, +) from boschshcpy.device import SHCDevice from homeassistant.components.button import ButtonEntity, ButtonEntityDescription @@ -46,6 +52,15 @@ SIREN_TEST_ALARM_DESCRIPTION = SHCButtonEntityDescription[SHCOutdoorSiren]( press_fn=lambda device: device.trigger_test_alarm(), ) +RESET_ENERGY_DESCRIPTION = SHCButtonEntityDescription[ + SHCSmartPlug | SHCSmartPlugCompact +]( + key="reset_energy_summation", + translation_key="reset_energy_summation", + entity_category=EntityCategory.CONFIG, + press_fn=lambda device: device.reset_energy_summation(), +) + async def async_setup_entry( hass: HomeAssistant, @@ -90,6 +105,19 @@ async def async_setup_entry( ) for device in session.device_helper.outdoor_sirens ) + entities.extend( + SHCButton( + hass=hass, + device=plug, + parent_id=shc_info.unique_id, + entry_id=config_entry.entry_id, + description=RESET_ENERGY_DESCRIPTION, + ) + for plug in ( + *session.device_helper.smart_plugs, + *session.device_helper.smart_plugs_compact, + ) + ) async_add_entities(entities) diff --git a/homeassistant/components/bosch_shc/strings.json b/homeassistant/components/bosch_shc/strings.json index 6c359275631b..a236758ce6c9 100644 --- a/homeassistant/components/bosch_shc/strings.json +++ b/homeassistant/components/bosch_shc/strings.json @@ -69,6 +69,9 @@ } }, "button": { + "reset_energy_summation": { + "name": "Reset consumption value" + }, "reset_tamper": { "name": "Reset tamper" }, diff --git a/tests/components/bosch_shc/test_button.py b/tests/components/bosch_shc/test_button.py index 3787f5aede30..3b600c6ebf74 100644 --- a/tests/components/bosch_shc/test_button.py +++ b/tests/components/bosch_shc/test_button.py @@ -14,6 +14,8 @@ from .conftest import ( motion_detector2_device, outdoor_siren_device, setup_integration, + smart_plug_compact_device, + smart_plug_device, smoke_detector_device, ) @@ -116,3 +118,42 @@ async def test_siren_test_alarm_button_press( ) device.trigger_test_alarm.assert_called_once_with() + + +@pytest.mark.parametrize( + "device_buckets", + [ + { + "smart_plugs": [smart_plug_device()], + "smart_plugs_compact": [smart_plug_compact_device()], + } + ], + indirect=True, +) +@pytest.mark.parametrize( + ("entity_id", "bucket"), + [ + ("button.smart_plug_reset_consumption_value", "smart_plugs"), + ("button.smart_plug_compact_reset_consumption_value", "smart_plugs_compact"), + ], +) +@pytest.mark.usefixtures("mock_session") +async def test_reset_energy_summation_button_press( + hass: HomeAssistant, + mock_session: MagicMock, + mock_config_entry: MockConfigEntry, + entity_id: str, + bucket: str, +) -> None: + """Pressing the button resets the accumulated energy counter.""" + await setup_integration(hass, mock_config_entry) + device = getattr(mock_session.device_helper, bucket)[0] + + await hass.services.async_call( + BUTTON_DOMAIN, + ButtonService.PRESS, + {TargetKey.ENTITY_ID: entity_id}, + blocking=True, + ) + + device.reset_energy_summation.assert_called_once_with()