diff --git a/homeassistant/components/bosch_alarm/services.py b/homeassistant/components/bosch_alarm/services.py index f3292f97ee8..1907d4ad45e 100644 --- a/homeassistant/components/bosch_alarm/services.py +++ b/homeassistant/components/bosch_alarm/services.py @@ -8,11 +8,10 @@ from typing import Any import voluptuous as vol -from homeassistant.config_entries import ConfigEntryState from homeassistant.const import ATTR_CONFIG_ENTRY_ID from homeassistant.core import HomeAssistant, ServiceCall, callback -from homeassistant.exceptions import HomeAssistantError, ServiceValidationError -from homeassistant.helpers import config_validation as cv +from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers import config_validation as cv, service from homeassistant.util import dt as dt_util from .const import ATTR_DATETIME, DOMAIN, SERVICE_SET_DATE_TIME @@ -41,21 +40,10 @@ SET_DATE_TIME_SCHEMA = vol.Schema( async def async_set_panel_date(call: ServiceCall) -> None: """Set the date and time on a bosch alarm panel.""" - config_entry: BoschAlarmConfigEntry | None value: dt.datetime = call.data.get(ATTR_DATETIME, dt_util.now()) - entry_id = call.data[ATTR_CONFIG_ENTRY_ID] - if not (config_entry := call.hass.config_entries.async_get_entry(entry_id)): - raise ServiceValidationError( - translation_domain=DOMAIN, - translation_key="integration_not_found", - translation_placeholders={"target": entry_id}, - ) - if config_entry.state is not ConfigEntryState.LOADED: - raise HomeAssistantError( - translation_domain=DOMAIN, - translation_key="not_loaded", - translation_placeholders={"target": config_entry.title}, - ) + config_entry: BoschAlarmConfigEntry = service.async_get_config_entry( + call.hass, DOMAIN, call.data[ATTR_CONFIG_ENTRY_ID] + ) panel = config_entry.runtime_data try: await panel.set_panel_date(value) diff --git a/homeassistant/components/bosch_alarm/strings.json b/homeassistant/components/bosch_alarm/strings.json index 2101056ca46..3f97840ed7a 100644 --- a/homeassistant/components/bosch_alarm/strings.json +++ b/homeassistant/components/bosch_alarm/strings.json @@ -155,12 +155,6 @@ "incorrect_door_state": { "message": "Door cannot be manipulated while it is momentarily unlocked." }, - "integration_not_found": { - "message": "Integration \"{target}\" not found in registry." - }, - "not_loaded": { - "message": "{target} is not loaded." - }, "unknown_error": { "message": "An unknown error occurred while setting the date and time on \"{target}\"." } diff --git a/tests/components/bosch_alarm/test_services.py b/tests/components/bosch_alarm/test_services.py index 059b01c1e3b..fb035b5cd78 100644 --- a/tests/components/bosch_alarm/test_services.py +++ b/tests/components/bosch_alarm/test_services.py @@ -59,10 +59,7 @@ async def test_set_date_time_service_fails_bad_entity( ) -> None: """Test that the service calls fail if the service call is done for an incorrect entity.""" await setup_integration(hass, mock_config_entry) - with pytest.raises( - ServiceValidationError, - match='Integration "bad-config_id" not found in registry', - ): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, SERVICE_SET_DATE_TIME, @@ -72,6 +69,8 @@ async def test_set_date_time_service_fails_bad_entity( }, blocking=True, ) + assert err.value.translation_key == "service_config_entry_not_found" + assert err.value.translation_placeholders["entry_id"] == "bad-config_id" async def test_set_date_time_service_fails_bad_params( @@ -177,10 +176,7 @@ async def test_set_date_time_service_fails_unloaded( """Test that the service calls fail if the config entry is unloaded.""" await async_setup_component(hass, DOMAIN, {}) mock_config_entry.add_to_hass(hass) - with pytest.raises( - HomeAssistantError, - match=f"{mock_config_entry.title} is not loaded", - ): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, SERVICE_SET_DATE_TIME, @@ -190,3 +186,5 @@ async def test_set_date_time_service_fails_unloaded( }, blocking=True, ) + assert err.value.translation_key == "service_config_entry_not_loaded" + assert err.value.translation_placeholders["entry_title"] == "Mock Title"