diff --git a/homeassistant/components/bmw_connected_drive/__init__.py b/homeassistant/components/bmw_connected_drive/__init__.py index 038a8696818..599892d6a03 100644 --- a/homeassistant/components/bmw_connected_drive/__init__.py +++ b/homeassistant/components/bmw_connected_drive/__init__.py @@ -11,6 +11,7 @@ from homeassistant.components.notify import DOMAIN as NOTIFY_DOMAIN from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry from homeassistant.const import ( ATTR_ATTRIBUTION, + CONF_DEVICE_ID, CONF_NAME, CONF_PASSWORD, CONF_REGION, @@ -18,7 +19,7 @@ from homeassistant.const import ( ) from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import ConfigEntryNotReady -from homeassistant.helpers import discovery +from homeassistant.helpers import device_registry, discovery import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import DeviceInfo, Entity from homeassistant.helpers.event import track_utc_time_change @@ -51,7 +52,12 @@ ACCOUNT_SCHEMA = vol.Schema( CONFIG_SCHEMA = vol.Schema({DOMAIN: {cv.string: ACCOUNT_SCHEMA}}, extra=vol.ALLOW_EXTRA) -SERVICE_SCHEMA = vol.Schema({vol.Required(ATTR_VIN): cv.string}) +SERVICE_SCHEMA = vol.Schema( + vol.Any( + {vol.Required(ATTR_VIN): cv.string}, + {vol.Required(CONF_DEVICE_ID): cv.string}, + ) +) DEFAULT_OPTIONS = { CONF_READ_ONLY: False, @@ -207,8 +213,15 @@ def setup_account(entry: ConfigEntry, hass, name: str) -> BMWConnectedDriveAccou def execute_service(call): """Execute a service for a vehicle.""" - vin = call.data[ATTR_VIN] + vin = call.data.get(ATTR_VIN) + device_id = call.data.get(CONF_DEVICE_ID) + vehicle = None + + if not vin and device_id: + device = device_registry.async_get(hass).async_get(device_id) + vin = next(iter(device.identifiers))[1] + # Double check for read_only accounts as another account could create the services for entry_data in [ e diff --git a/homeassistant/components/bmw_connected_drive/services.yaml b/homeassistant/components/bmw_connected_drive/services.yaml index 563e14e5577..964fb8ab39b 100644 --- a/homeassistant/components/bmw_connected_drive/services.yaml +++ b/homeassistant/components/bmw_connected_drive/services.yaml @@ -6,14 +6,20 @@ light_flash: name: Flash lights description: > - Flash the lights of the vehicle. The vehicle is identified via the vin - (see below). + Flash the lights of the vehicle. The vehicle is identified either via its + device entry or the VIN. If a VIN is specified, the device entry will be ignored. fields: + device_id: + name: Car + description: The BMW Connected Drive device + selector: + device: + integration: bmw_connected_drive vin: name: VIN - description: > - The vehicle identification number (VIN) of the vehicle, 17 characters - required: true + description: The vehicle identification number (VIN) of the vehicle, 17 characters + advanced: true + required: false example: WBANXXXXXX1234567 selector: text: @@ -21,14 +27,20 @@ light_flash: sound_horn: name: Sound horn description: > - Sound the horn of the vehicle. The vehicle is identified via the vin - (see below). + Sound the horn of the vehicle. The vehicle is identified either via its + device entry or the VIN. If a VIN is specified, the device entry will be ignored. fields: + device_id: + name: Car + description: The BMW Connected Drive device + selector: + device: + integration: bmw_connected_drive vin: name: VIN - description: > - The vehicle identification number (VIN) of the vehicle, 17 characters - required: true + description: The vehicle identification number (VIN) of the vehicle, 17 characters + advanced: true + required: false example: WBANXXXXXX1234567 selector: text: @@ -38,14 +50,20 @@ activate_air_conditioning: description: > Start the air conditioning of the vehicle. What exactly is started here depends on the type of vehicle. It might range from just ventilation over - auxiliary heating to real air conditioning. The vehicle is identified via - the vin (see below). + auxiliary heating to real air conditioning. The vehicle is identified either via its + device entry or the VIN. If a VIN is specified, the device entry will be ignored. fields: + device_id: + name: Car + description: The BMW Connected Drive device + selector: + device: + integration: bmw_connected_drive vin: name: VIN - description: > - The vehicle identification number (VIN) of the vehicle, 17 characters - required: true + description: The vehicle identification number (VIN) of the vehicle, 17 characters + advanced: true + required: false example: WBANXXXXXX1234567 selector: text: @@ -53,14 +71,20 @@ activate_air_conditioning: find_vehicle: name: Find vehicle description: > - Request vehicle to update the gps location. The vehicle is identified via the vin - (see below). + Request vehicle to update the GPS location. The vehicle is identified either via its + device entry or the VIN. If a VIN is specified, the device entry will be ignored. fields: + device_id: + name: Car + description: The BMW Connected Drive device + selector: + device: + integration: bmw_connected_drive vin: name: VIN - description: > - The vehicle identification number (VIN) of the vehicle, 17 characters - required: true + description: The vehicle identification number (VIN) of the vehicle, 17 characters + advanced: true + required: false example: WBANXXXXXX1234567 selector: text: