mirror of
https://github.com/home-assistant/core.git
synced 2026-08-28 02:24:46 -05:00
Use async_get_device_and_config_entry service helper in teslemetry (#180151)
This commit is contained in:
@@ -17,7 +17,11 @@ from homeassistant.const import (
|
||||
)
|
||||
from homeassistant.core import HomeAssistant, ServiceCall, callback
|
||||
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
|
||||
from homeassistant.helpers import config_validation as cv, device_registry as dr
|
||||
from homeassistant.helpers import (
|
||||
config_validation as cv,
|
||||
device_registry as dr,
|
||||
service,
|
||||
)
|
||||
|
||||
from .const import DOMAIN
|
||||
from .helpers import handle_command, handle_vehicle_command
|
||||
@@ -64,40 +68,15 @@ SERVICE_ADD_PRECONDITION_SCHEDULE = "add_precondition_schedule"
|
||||
SERVICE_REMOVE_PRECONDITION_SCHEDULE = "remove_precondition_schedule"
|
||||
|
||||
|
||||
def async_get_device_for_service_call(
|
||||
def async_get_device_and_config_for_service_call(
|
||||
hass: HomeAssistant, call: ServiceCall
|
||||
) -> dr.DeviceEntry:
|
||||
"""Get the device entry related to a service call."""
|
||||
device_id = call.data[CONF_DEVICE_ID]
|
||||
device_registry = dr.async_get(hass)
|
||||
if (
|
||||
device_entry := device_registry.async_get(
|
||||
device_id, include_child_devices=False
|
||||
)
|
||||
) is None:
|
||||
raise ServiceValidationError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="invalid_device",
|
||||
translation_placeholders={"device_id": device_id},
|
||||
)
|
||||
|
||||
return device_entry
|
||||
|
||||
|
||||
def async_get_config_for_device(
|
||||
hass: HomeAssistant, device_entry: dr.DeviceEntry
|
||||
) -> TeslemetryConfigEntry:
|
||||
"""Get the config entry related to a device entry."""
|
||||
_, config_entry = dr.async_get_device_and_config_entry_for_domain(
|
||||
hass, device_entry.id, domain=DOMAIN
|
||||
)
|
||||
if config_entry:
|
||||
return config_entry
|
||||
raise ServiceValidationError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="no_config_entry_for_device",
|
||||
translation_placeholders={"device_id": device_entry.id},
|
||||
) -> tuple[dr.DeviceEntry, TeslemetryConfigEntry]:
|
||||
"""Get the device entry and config entry related to a service call."""
|
||||
config_entry: TeslemetryConfigEntry
|
||||
device_entry, config_entry = service.async_get_device_and_config_entry(
|
||||
hass, DOMAIN, call.data[CONF_DEVICE_ID]
|
||||
)
|
||||
return device_entry, config_entry
|
||||
|
||||
|
||||
def async_get_vehicle_for_entry(
|
||||
@@ -136,8 +115,7 @@ def async_setup_services(hass: HomeAssistant) -> None:
|
||||
|
||||
async def navigate_gps_request(call: ServiceCall) -> None:
|
||||
"""Send lat,lon,order with a vehicle."""
|
||||
device = async_get_device_for_service_call(hass, call)
|
||||
config = async_get_config_for_device(hass, device)
|
||||
device, config = async_get_device_and_config_for_service_call(hass, call)
|
||||
vehicle = async_get_vehicle_for_entry(hass, device, config)
|
||||
|
||||
await handle_vehicle_command(
|
||||
@@ -166,8 +144,7 @@ def async_setup_services(hass: HomeAssistant) -> None:
|
||||
|
||||
async def set_scheduled_charging(call: ServiceCall) -> None:
|
||||
"""Configure fleet telemetry."""
|
||||
device = async_get_device_for_service_call(hass, call)
|
||||
config = async_get_config_for_device(hass, device)
|
||||
device, config = async_get_device_and_config_for_service_call(hass, call)
|
||||
vehicle = async_get_vehicle_for_entry(hass, device, config)
|
||||
|
||||
time: int
|
||||
@@ -201,8 +178,7 @@ def async_setup_services(hass: HomeAssistant) -> None:
|
||||
|
||||
async def set_scheduled_departure(call: ServiceCall) -> None:
|
||||
"""Configure fleet telemetry."""
|
||||
device = async_get_device_for_service_call(hass, call)
|
||||
config = async_get_config_for_device(hass, device)
|
||||
device, config = async_get_device_and_config_for_service_call(hass, call)
|
||||
vehicle = async_get_vehicle_for_entry(hass, device, config)
|
||||
|
||||
enable = call.data.get("enable", True)
|
||||
@@ -274,8 +250,7 @@ def async_setup_services(hass: HomeAssistant) -> None:
|
||||
|
||||
async def valet_mode(call: ServiceCall) -> None:
|
||||
"""Configure fleet telemetry."""
|
||||
device = async_get_device_for_service_call(hass, call)
|
||||
config = async_get_config_for_device(hass, device)
|
||||
device, config = async_get_device_and_config_for_service_call(hass, call)
|
||||
vehicle = async_get_vehicle_for_entry(hass, device, config)
|
||||
|
||||
await handle_vehicle_command(
|
||||
@@ -297,8 +272,7 @@ def async_setup_services(hass: HomeAssistant) -> None:
|
||||
|
||||
async def speed_limit(call: ServiceCall) -> None:
|
||||
"""Configure fleet telemetry."""
|
||||
device = async_get_device_for_service_call(hass, call)
|
||||
config = async_get_config_for_device(hass, device)
|
||||
device, config = async_get_device_and_config_for_service_call(hass, call)
|
||||
vehicle = async_get_vehicle_for_entry(hass, device, config)
|
||||
|
||||
enable = call.data["enable"]
|
||||
@@ -326,8 +300,7 @@ def async_setup_services(hass: HomeAssistant) -> None:
|
||||
|
||||
async def time_of_use(call: ServiceCall) -> None:
|
||||
"""Configure time of use settings."""
|
||||
device = async_get_device_for_service_call(hass, call)
|
||||
config = async_get_config_for_device(hass, device)
|
||||
device, config = async_get_device_and_config_for_service_call(hass, call)
|
||||
site = async_get_energy_site_for_entry(hass, device, config)
|
||||
|
||||
tou_settings = call.data[ATTR_TOU_SETTINGS]
|
||||
@@ -360,8 +333,7 @@ def async_setup_services(hass: HomeAssistant) -> None:
|
||||
|
||||
async def add_charge_schedule(call: ServiceCall) -> None:
|
||||
"""Configure charging schedule for a vehicle."""
|
||||
device = async_get_device_for_service_call(hass, call)
|
||||
config = async_get_config_for_device(hass, device)
|
||||
device, config = async_get_device_and_config_for_service_call(hass, call)
|
||||
vehicle = async_get_vehicle_for_entry(hass, device, config)
|
||||
|
||||
# Extract parameters from the service call
|
||||
@@ -434,8 +406,7 @@ def async_setup_services(hass: HomeAssistant) -> None:
|
||||
|
||||
async def remove_charge_schedule(call: ServiceCall) -> None:
|
||||
"""Remove a charging schedule for a vehicle."""
|
||||
device = async_get_device_for_service_call(hass, call)
|
||||
config = async_get_config_for_device(hass, device)
|
||||
device, config = async_get_device_and_config_for_service_call(hass, call)
|
||||
vehicle = async_get_vehicle_for_entry(hass, device, config)
|
||||
|
||||
# Extract parameters from the service call
|
||||
@@ -461,8 +432,7 @@ def async_setup_services(hass: HomeAssistant) -> None:
|
||||
|
||||
async def add_precondition_schedule(call: ServiceCall) -> None:
|
||||
"""Add or modify a precondition schedule for a vehicle."""
|
||||
device = async_get_device_for_service_call(hass, call)
|
||||
config = async_get_config_for_device(hass, device)
|
||||
device, config = async_get_device_and_config_for_service_call(hass, call)
|
||||
vehicle = async_get_vehicle_for_entry(hass, device, config)
|
||||
|
||||
# Extract parameters from the service call
|
||||
@@ -527,8 +497,7 @@ def async_setup_services(hass: HomeAssistant) -> None:
|
||||
|
||||
async def remove_precondition_schedule(call: ServiceCall) -> None:
|
||||
"""Remove a preconditioning schedule for a vehicle."""
|
||||
device = async_get_device_for_service_call(hass, call)
|
||||
config = async_get_config_for_device(hass, device)
|
||||
device, config = async_get_device_and_config_for_service_call(hass, call)
|
||||
vehicle = async_get_vehicle_for_entry(hass, device, config)
|
||||
|
||||
# Extract parameters from the service call
|
||||
|
||||
@@ -1173,18 +1173,12 @@
|
||||
"invalid_cop_temp": {
|
||||
"message": "Cabin overheat protection does not support that temperature"
|
||||
},
|
||||
"invalid_device": {
|
||||
"message": "Invalid device ID: {device_id}"
|
||||
},
|
||||
"missing_scope": {
|
||||
"message": "Missing required scope: {scope}"
|
||||
},
|
||||
"no_cable": {
|
||||
"message": "Insert cable to lock, charge cable will lock automatically when connected"
|
||||
},
|
||||
"no_config_entry_for_device": {
|
||||
"message": "No config entry for device ID: {device_id}"
|
||||
},
|
||||
"no_energy_site_data_for_device": {
|
||||
"message": "No energy site data for device ID: {device_id}"
|
||||
},
|
||||
|
||||
@@ -381,7 +381,7 @@ async def test_service_validation_errors(
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
assert exc_info.value.translation_key == "invalid_device"
|
||||
assert exc_info.value.translation_key == "service_device_not_found"
|
||||
|
||||
# Test set_scheduled_charging validation error (enable=True but no time)
|
||||
with pytest.raises(ServiceValidationError) as exc_info:
|
||||
@@ -472,4 +472,4 @@ async def test_service_validation_errors(
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
assert exc_info.value.translation_key == "no_config_entry_for_device"
|
||||
assert exc_info.value.translation_key == "service_device_wrong_domain"
|
||||
|
||||
Reference in New Issue
Block a user