mirror of
https://github.com/Electric-Special/ha-core.git
synced 2026-03-21 05:06:13 +01:00
Use service helper to extract risco config entry (#162806)
This commit is contained in:
@@ -4,11 +4,10 @@ from datetime import datetime
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import ATTR_CONFIG_ENTRY_ID, ATTR_TIME, CONF_TYPE
|
||||
from homeassistant.core import HomeAssistant, ServiceCall
|
||||
from homeassistant.exceptions import ServiceValidationError
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers import config_validation as cv, service
|
||||
|
||||
from .const import DOMAIN, SERVICE_SET_TIME, TYPE_LOCAL
|
||||
from .models import LocalData
|
||||
@@ -18,23 +17,11 @@ async def async_setup_services(hass: HomeAssistant) -> None:
|
||||
"""Create the Risco Services/Actions."""
|
||||
|
||||
async def _set_time(service_call: ServiceCall) -> None:
|
||||
config_entry_id = service_call.data[ATTR_CONFIG_ENTRY_ID]
|
||||
entry = service.async_get_config_entry(
|
||||
service_call.hass, DOMAIN, service_call.data[ATTR_CONFIG_ENTRY_ID]
|
||||
)
|
||||
time = service_call.data.get(ATTR_TIME)
|
||||
|
||||
# Validate config entry exists
|
||||
if not (entry := hass.config_entries.async_get_entry(config_entry_id)):
|
||||
raise ServiceValidationError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="config_entry_not_found",
|
||||
)
|
||||
|
||||
# Validate config entry is loaded
|
||||
if entry.state is not ConfigEntryState.LOADED:
|
||||
raise ServiceValidationError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="config_entry_not_loaded",
|
||||
)
|
||||
|
||||
# Validate config entry is local (not cloud)
|
||||
if entry.data.get(CONF_TYPE) != TYPE_LOCAL:
|
||||
raise ServiceValidationError(
|
||||
@@ -46,7 +33,7 @@ async def async_setup_services(hass: HomeAssistant) -> None:
|
||||
if time is None:
|
||||
time_to_send = datetime.now()
|
||||
|
||||
local_data: LocalData = hass.data[DOMAIN][config_entry_id]
|
||||
local_data: LocalData = hass.data[DOMAIN][entry.entry_id]
|
||||
|
||||
await local_data.system.set_time(time_to_send)
|
||||
|
||||
|
||||
@@ -72,12 +72,6 @@
|
||||
}
|
||||
},
|
||||
"exceptions": {
|
||||
"config_entry_not_found": {
|
||||
"message": "Config entry not found. Please check that the config entry ID is correct."
|
||||
},
|
||||
"config_entry_not_loaded": {
|
||||
"message": "Config entry is not loaded. Please ensure the Risco integration is set up correctly."
|
||||
},
|
||||
"not_local_entry": {
|
||||
"message": "This service only works with local Risco connections."
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ async def test_set_time_service_with_invalid_entry(
|
||||
ATTR_CONFIG_ENTRY_ID: "invalid_entry_id",
|
||||
}
|
||||
|
||||
with pytest.raises(ServiceValidationError, match="Config entry not found"):
|
||||
with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"):
|
||||
await hass.services.async_call(
|
||||
DOMAIN, SERVICE_SET_TIME, service_data=data, blocking=True
|
||||
)
|
||||
@@ -80,7 +80,7 @@ async def test_set_time_service_with_not_loaded_entry(
|
||||
ATTR_CONFIG_ENTRY_ID: local_config_entry.entry_id,
|
||||
}
|
||||
|
||||
with pytest.raises(ServiceValidationError, match="is not loaded"):
|
||||
with pytest.raises(ServiceValidationError, match="service_config_entry_not_loaded"):
|
||||
await hass.services.async_call(
|
||||
DOMAIN, SERVICE_SET_TIME, service_data=data, blocking=True
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user