diff --git a/homeassistant/components/renault/services.py b/homeassistant/components/renault/services.py index df65d16b0b8..dfad97ae4ea 100644 --- a/homeassistant/components/renault/services.py +++ b/homeassistant/components/renault/services.py @@ -2,7 +2,6 @@ from __future__ import annotations -from collections.abc import Mapping from datetime import datetime import logging from typing import TYPE_CHECKING, Any @@ -105,91 +104,96 @@ SERVICES = [ ] -def setup_services(hass: HomeAssistant) -> None: - """Register the Renault services.""" +async def ac_cancel(service_call: ServiceCall) -> None: + """Cancel A/C.""" + proxy = get_vehicle_proxy(service_call) - async def ac_cancel(service_call: ServiceCall) -> None: - """Cancel A/C.""" - proxy = get_vehicle_proxy(service_call.data) + LOGGER.debug("A/C cancel attempt") + result = await proxy.set_ac_stop() + LOGGER.debug("A/C cancel result: %s", result) - LOGGER.debug("A/C cancel attempt") - result = await proxy.set_ac_stop() - LOGGER.debug("A/C cancel result: %s", result) - async def ac_start(service_call: ServiceCall) -> None: - """Start A/C.""" - temperature: float = service_call.data[ATTR_TEMPERATURE] - when: datetime | None = service_call.data.get(ATTR_WHEN) - proxy = get_vehicle_proxy(service_call.data) +async def ac_start(service_call: ServiceCall) -> None: + """Start A/C.""" + temperature: float = service_call.data[ATTR_TEMPERATURE] + when: datetime | None = service_call.data.get(ATTR_WHEN) + proxy = get_vehicle_proxy(service_call) - LOGGER.debug("A/C start attempt: %s / %s", temperature, when) - result = await proxy.set_ac_start(temperature, when) - LOGGER.debug("A/C start result: %s", result.raw_data) + LOGGER.debug("A/C start attempt: %s / %s", temperature, when) + result = await proxy.set_ac_start(temperature, when) + LOGGER.debug("A/C start result: %s", result.raw_data) - async def charge_set_schedules(service_call: ServiceCall) -> None: - """Set charge schedules.""" - schedules: list[dict[str, Any]] = service_call.data[ATTR_SCHEDULES] - proxy = get_vehicle_proxy(service_call.data) - charge_schedules = await proxy.get_charging_settings() - for schedule in schedules: - charge_schedules.update(schedule) - if TYPE_CHECKING: - assert charge_schedules.schedules is not None - LOGGER.debug("Charge set schedules attempt: %s", schedules) - result = await proxy.set_charge_schedules(charge_schedules.schedules) +async def charge_set_schedules(service_call: ServiceCall) -> None: + """Set charge schedules.""" + schedules: list[dict[str, Any]] = service_call.data[ATTR_SCHEDULES] + proxy = get_vehicle_proxy(service_call) + charge_schedules = await proxy.get_charging_settings() + for schedule in schedules: + charge_schedules.update(schedule) - LOGGER.debug("Charge set schedules result: %s", result) - LOGGER.debug( - "It may take some time before these changes are reflected in your vehicle" - ) + if TYPE_CHECKING: + assert charge_schedules.schedules is not None + LOGGER.debug("Charge set schedules attempt: %s", schedules) + result = await proxy.set_charge_schedules(charge_schedules.schedules) - async def ac_set_schedules(service_call: ServiceCall) -> None: - """Set A/C schedules.""" - schedules: list[dict[str, Any]] = service_call.data[ATTR_SCHEDULES] - proxy = get_vehicle_proxy(service_call.data) - hvac_schedules = await proxy.get_hvac_settings() + LOGGER.debug("Charge set schedules result: %s", result) + LOGGER.debug( + "It may take some time before these changes are reflected in your vehicle" + ) - for schedule in schedules: - hvac_schedules.update(schedule) - if TYPE_CHECKING: - assert hvac_schedules.schedules is not None - LOGGER.debug("HVAC set schedules attempt: %s", schedules) - result = await proxy.set_hvac_schedules(hvac_schedules.schedules) +async def ac_set_schedules(service_call: ServiceCall) -> None: + """Set A/C schedules.""" + schedules: list[dict[str, Any]] = service_call.data[ATTR_SCHEDULES] + proxy = get_vehicle_proxy(service_call) + hvac_schedules = await proxy.get_hvac_settings() - LOGGER.debug("HVAC set schedules result: %s", result) - LOGGER.debug( - "It may take some time before these changes are reflected in your vehicle" - ) + for schedule in schedules: + hvac_schedules.update(schedule) - def get_vehicle_proxy(service_call_data: Mapping) -> RenaultVehicleProxy: - """Get vehicle from service_call data.""" - device_registry = dr.async_get(hass) - device_id = service_call_data[ATTR_VEHICLE] - device_entry = device_registry.async_get(device_id) - if device_entry is None: - raise ServiceValidationError( - translation_domain=DOMAIN, - translation_key="invalid_device_id", - translation_placeholders={"device_id": device_id}, - ) + if TYPE_CHECKING: + assert hvac_schedules.schedules is not None + LOGGER.debug("HVAC set schedules attempt: %s", schedules) + result = await proxy.set_hvac_schedules(hvac_schedules.schedules) - loaded_entries: list[RenaultConfigEntry] = [ - entry - for entry in hass.config_entries.async_loaded_entries(DOMAIN) - if 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 + LOGGER.debug("HVAC set schedules result: %s", result) + LOGGER.debug( + "It may take some time before these changes are reflected in your vehicle" + ) + + +def get_vehicle_proxy(service_call: ServiceCall) -> RenaultVehicleProxy: + """Get vehicle from service_call data.""" + device_registry = dr.async_get(service_call.hass) + device_id = service_call.data[ATTR_VEHICLE] + device_entry = device_registry.async_get(device_id) + if device_entry is None: raise ServiceValidationError( translation_domain=DOMAIN, - translation_key="no_config_entry_for_device", - translation_placeholders={"device_id": device_entry.name or device_id}, + translation_key="invalid_device_id", + translation_placeholders={"device_id": device_id}, ) + loaded_entries: list[RenaultConfigEntry] = [ + entry + for entry in service_call.hass.config_entries.async_loaded_entries(DOMAIN) + if 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 ServiceValidationError( + translation_domain=DOMAIN, + translation_key="no_config_entry_for_device", + translation_placeholders={"device_id": device_entry.name or device_id}, + ) + + +def setup_services(hass: HomeAssistant) -> None: + """Register the Renault services.""" + hass.services.async_register( DOMAIN, SERVICE_AC_CANCEL,