1
0
mirror of https://github.com/home-assistant/core.git synced 2026-02-14 23:28:42 +00:00

Use service helper to extract bosch alarm config entry (#162789)

This commit is contained in:
epenet
2026-02-12 06:04:50 +01:00
committed by GitHub
parent f9bd1b3d30
commit efa522cc73
3 changed files with 11 additions and 31 deletions

View File

@@ -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)

View File

@@ -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}\"."
}

View File

@@ -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"