mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 15:17:35 +00:00
Address late review of Mazda services (#51178)
* Add services for Mazda integration * Address review comments * Follow-up review comments * Update dict access for send_poi service calls
This commit is contained in:
parent
99ee2bd0a3
commit
88dce0ec8f
@ -69,16 +69,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
"""Handle a service call."""
|
"""Handle a service call."""
|
||||||
# Get device entry from device registry
|
# Get device entry from device registry
|
||||||
dev_reg = device_registry.async_get(hass)
|
dev_reg = device_registry.async_get(hass)
|
||||||
device_id = service_call.data.get("device_id")
|
device_id = service_call.data["device_id"]
|
||||||
device_entry = dev_reg.async_get(device_id)
|
device_entry = dev_reg.async_get(device_id)
|
||||||
|
|
||||||
# Get vehicle VIN from device identifiers
|
# Get vehicle VIN from device identifiers
|
||||||
mazda_identifiers = [
|
mazda_identifiers = (
|
||||||
identifier
|
identifier
|
||||||
for identifier in device_entry.identifiers
|
for identifier in device_entry.identifiers
|
||||||
if identifier[0] == DOMAIN
|
if identifier[0] == DOMAIN
|
||||||
]
|
)
|
||||||
vin_identifier = next(iter(mazda_identifiers))
|
vin_identifier = next(mazda_identifiers)
|
||||||
vin = vin_identifier[1]
|
vin = vin_identifier[1]
|
||||||
|
|
||||||
# Get vehicle ID and API client from hass.data
|
# Get vehicle ID and API client from hass.data
|
||||||
@ -89,6 +89,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
if vehicle["vin"] == vin:
|
if vehicle["vin"] == vin:
|
||||||
vehicle_id = vehicle["id"]
|
vehicle_id = vehicle["id"]
|
||||||
api_client = entry_data[DATA_CLIENT]
|
api_client = entry_data[DATA_CLIENT]
|
||||||
|
break
|
||||||
|
|
||||||
if vehicle_id == 0 or api_client is None:
|
if vehicle_id == 0 or api_client is None:
|
||||||
raise HomeAssistantError("Vehicle ID not found")
|
raise HomeAssistantError("Vehicle ID not found")
|
||||||
@ -96,14 +97,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
api_method = getattr(api_client, service_call.service)
|
api_method = getattr(api_client, service_call.service)
|
||||||
try:
|
try:
|
||||||
if service_call.service == "send_poi":
|
if service_call.service == "send_poi":
|
||||||
latitude = service_call.data.get("latitude")
|
latitude = service_call.data["latitude"]
|
||||||
longitude = service_call.data.get("longitude")
|
longitude = service_call.data["longitude"]
|
||||||
poi_name = service_call.data.get("poi_name")
|
poi_name = service_call.data["poi_name"]
|
||||||
await api_method(vehicle_id, latitude, longitude, poi_name)
|
await api_method(vehicle_id, latitude, longitude, poi_name)
|
||||||
else:
|
else:
|
||||||
await api_method(vehicle_id)
|
await api_method(vehicle_id)
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
_LOGGER.exception("Error occurred during Mazda service call: %s", ex)
|
|
||||||
raise HomeAssistantError(ex) from ex
|
raise HomeAssistantError(ex) from ex
|
||||||
|
|
||||||
def validate_mazda_device_id(device_id):
|
def validate_mazda_device_id(device_id):
|
||||||
@ -119,7 +119,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
for identifier in device_entry.identifiers
|
for identifier in device_entry.identifiers
|
||||||
if identifier[0] == DOMAIN
|
if identifier[0] == DOMAIN
|
||||||
]
|
]
|
||||||
if len(mazda_identifiers) < 1:
|
if not mazda_identifiers:
|
||||||
raise vol.Invalid("Device ID is not a Mazda vehicle")
|
raise vol.Invalid("Device ID is not a Mazda vehicle")
|
||||||
|
|
||||||
return device_id
|
return device_id
|
||||||
|
@ -7,7 +7,7 @@ from pymazda import MazdaAuthenticationException, MazdaException
|
|||||||
import pytest
|
import pytest
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.mazda.const import DOMAIN, SERVICES
|
from homeassistant.components.mazda.const import DOMAIN
|
||||||
from homeassistant.config_entries import ConfigEntryState
|
from homeassistant.config_entries import ConfigEntryState
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_EMAIL,
|
CONF_EMAIL,
|
||||||
@ -186,7 +186,23 @@ async def test_device_no_nickname(hass):
|
|||||||
assert reg_device.name == "2021 MAZDA3 2.5 S SE AWD"
|
assert reg_device.name == "2021 MAZDA3 2.5 S SE AWD"
|
||||||
|
|
||||||
|
|
||||||
async def test_services(hass):
|
@pytest.mark.parametrize(
|
||||||
|
"service, service_data, expected_args",
|
||||||
|
[
|
||||||
|
("start_charging", {}, [12345]),
|
||||||
|
("start_engine", {}, [12345]),
|
||||||
|
("stop_charging", {}, [12345]),
|
||||||
|
("stop_engine", {}, [12345]),
|
||||||
|
("turn_off_hazard_lights", {}, [12345]),
|
||||||
|
("turn_on_hazard_lights", {}, [12345]),
|
||||||
|
(
|
||||||
|
"send_poi",
|
||||||
|
{"latitude": 1.2345, "longitude": 2.3456, "poi_name": "Work"},
|
||||||
|
[12345, 1.2345, 2.3456, "Work"],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_services(hass, service, service_data, expected_args):
|
||||||
"""Test service calls."""
|
"""Test service calls."""
|
||||||
client_mock = await init_integration(hass)
|
client_mock = await init_integration(hass)
|
||||||
|
|
||||||
@ -196,21 +212,13 @@ async def test_services(hass):
|
|||||||
)
|
)
|
||||||
device_id = reg_device.id
|
device_id = reg_device.id
|
||||||
|
|
||||||
for service in SERVICES:
|
service_data["device_id"] = device_id
|
||||||
service_data = {"device_id": device_id}
|
|
||||||
if service == "send_poi":
|
|
||||||
service_data["latitude"] = 1.2345
|
|
||||||
service_data["longitude"] = 2.3456
|
|
||||||
service_data["poi_name"] = "Work"
|
|
||||||
|
|
||||||
await hass.services.async_call(DOMAIN, service, service_data, blocking=True)
|
await hass.services.async_call(DOMAIN, service, service_data, blocking=True)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
api_method = getattr(client_mock, service)
|
api_method = getattr(client_mock, service)
|
||||||
if service == "send_poi":
|
api_method.assert_called_once_with(*expected_args)
|
||||||
api_method.assert_called_once_with(12345, 1.2345, 2.3456, "Work")
|
|
||||||
else:
|
|
||||||
api_method.assert_called_once_with(12345)
|
|
||||||
|
|
||||||
|
|
||||||
async def test_service_invalid_device_id(hass):
|
async def test_service_invalid_device_id(hass):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user