Use ServiceValidationError in Renault (#131265)

This commit is contained in:
epenet 2024-11-22 14:59:11 +01:00 committed by GitHub
parent 7a42c42384
commit ae592a0c35
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 6 deletions

View File

@ -11,6 +11,7 @@ import voluptuous as vol
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers import config_validation as cv, device_registry as dr
from .const import DOMAIN
@ -169,18 +170,27 @@ def setup_services(hass: HomeAssistant) -> None:
device_id = service_call_data[ATTR_VEHICLE]
device_entry = device_registry.async_get(device_id)
if device_entry is None:
raise ValueError(f"Unable to find device with id: {device_id}")
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="invalid_device_id",
translation_placeholders={"device_id": device_id},
)
loaded_entries: list[RenaultConfigEntry] = [
entry
for entry in hass.config_entries.async_entries(DOMAIN)
if entry.state == ConfigEntryState.LOADED
and entry.entry_id in device_entry.config_entries
]
for entry in loaded_entries:
for vin, vehicle in entry.runtime_data.vehicles.items():
if (DOMAIN, vin) in device_entry.identifiers:
return vehicle
raise ValueError(f"Unable to find vehicle with VIN: {device_entry.identifiers}")
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="no_config_entry_for_device",
translation_placeholders={"device_id": device_entry.name or device_id},
)
hass.services.async_register(
DOMAIN,

View File

@ -213,5 +213,13 @@
}
}
}
},
"exceptions": {
"invalid_device_id": {
"message": "No device with id {device_id} was found"
},
"no_config_entry_for_device": {
"message": "No loaded config entry was found for device with id {device_id}"
}
}
}

View File

@ -30,7 +30,7 @@ from homeassistant.const import (
ATTR_NAME,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from homeassistant.helpers import device_registry as dr
from .const import MOCK_VEHICLES
@ -341,12 +341,14 @@ async def test_service_invalid_device_id(
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
data = {ATTR_VEHICLE: "VF1AAAAA555777999"}
data = {ATTR_VEHICLE: "some_random_id"}
with pytest.raises(ValueError):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
DOMAIN, SERVICE_AC_CANCEL, service_data=data, blocking=True
)
assert err.value.translation_key == "invalid_device_id"
assert err.value.translation_placeholders == {"device_id": "some_random_id"}
async def test_service_invalid_device_id2(
@ -372,7 +374,9 @@ async def test_service_invalid_device_id2(
data = {ATTR_VEHICLE: device_id}
with pytest.raises(ValueError):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
DOMAIN, SERVICE_AC_CANCEL, service_data=data, blocking=True
)
assert err.value.translation_key == "no_config_entry_for_device"
assert err.value.translation_placeholders == {"device_id": "REG-NUMBER"}