"""Tests for Renault sensors.""" from collections.abc import Generator from unittest.mock import patch import pytest from renault_api.kamereon import schemas from syrupy.assertion import SnapshotAssertion from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS from homeassistant.components.renault.const import DOMAIN from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_ENTITY_ID, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er from tests.common import async_load_fixture, snapshot_platform pytestmark = pytest.mark.usefixtures("patch_renault_account", "patch_get_vehicles") @pytest.fixture(autouse=True) def override_platforms() -> Generator[None]: """Override PLATFORMS.""" with patch("homeassistant.components.renault.PLATFORMS", [Platform.BUTTON]): yield @pytest.mark.usefixtures("fixtures_with_data") async def test_buttons( hass: HomeAssistant, config_entry: ConfigEntry, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion, ) -> None: """Test for Renault device trackers.""" await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id) @pytest.mark.usefixtures("fixtures_with_no_data") @pytest.mark.parametrize("vehicle_type", ["zoe_40"], indirect=True) async def test_button_empty( hass: HomeAssistant, config_entry: ConfigEntry, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion, ) -> None: """Test for Renault device trackers with empty data from Renault.""" await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id) @pytest.mark.usefixtures("fixtures_with_invalid_upstream_exception") @pytest.mark.parametrize("vehicle_type", ["zoe_40"], indirect=True) async def test_button_errors( hass: HomeAssistant, config_entry: ConfigEntry, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion, ) -> None: """Test for Renault device trackers with temporary failure.""" await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id) @pytest.mark.usefixtures("fixtures_with_access_denied_exception") @pytest.mark.parametrize("vehicle_type", ["zoe_40"], indirect=True) async def test_button_access_denied( hass: HomeAssistant, config_entry: ConfigEntry, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion, ) -> None: """Test for Renault device trackers with access denied failure.""" await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id) @pytest.mark.usefixtures("fixtures_with_not_supported_exception") @pytest.mark.parametrize("vehicle_type", ["zoe_40"], indirect=True) async def test_button_not_supported( hass: HomeAssistant, config_entry: ConfigEntry, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion, ) -> None: """Test for Renault device trackers with not supported failure.""" await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id) @pytest.mark.usefixtures("fixtures_with_data") @pytest.mark.parametrize("vehicle_type", ["zoe_40"], indirect=True) async def test_button_start_charge( hass: HomeAssistant, config_entry: ConfigEntry ) -> None: """Test that button invokes renault_api with correct data.""" await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() data = { ATTR_ENTITY_ID: "button.reg_zoe_40_start_charge", } with patch( "renault_api.renault_vehicle.RenaultVehicle.set_charge_start", return_value=( schemas.KamereonVehicleHvacStartActionDataSchema.loads( await async_load_fixture(hass, "action.set_charge_start.json", DOMAIN) ) ), ) as mock_action: await hass.services.async_call( BUTTON_DOMAIN, SERVICE_PRESS, service_data=data, blocking=True ) assert len(mock_action.mock_calls) == 1 assert mock_action.mock_calls[0][1] == () @pytest.mark.usefixtures("fixtures_with_data") @pytest.mark.parametrize("vehicle_type", ["zoe_40"], indirect=True) async def test_button_stop_charge( hass: HomeAssistant, config_entry: ConfigEntry ) -> None: """Test that button invokes renault_api with correct data.""" await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() data = { ATTR_ENTITY_ID: "button.reg_zoe_40_stop_charge", } with patch( "renault_api.renault_vehicle.RenaultVehicle.set_charge_stop", return_value=( schemas.KamereonVehicleChargingStartActionDataSchema.loads( await async_load_fixture(hass, "action.set_charge_stop.json", DOMAIN) ) ), ) as mock_action: await hass.services.async_call( BUTTON_DOMAIN, SERVICE_PRESS, service_data=data, blocking=True ) assert len(mock_action.mock_calls) == 1 assert mock_action.mock_calls[0][1] == () @pytest.mark.usefixtures("fixtures_with_data") @pytest.mark.parametrize("vehicle_type", ["zoe_40"], indirect=True) async def test_button_start_air_conditioner( hass: HomeAssistant, config_entry: ConfigEntry ) -> None: """Test that button invokes renault_api with correct data.""" await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() data = { ATTR_ENTITY_ID: "button.reg_zoe_40_start_air_conditioner", } with patch( "renault_api.renault_vehicle.RenaultVehicle.set_ac_start", return_value=( schemas.KamereonVehicleHvacStartActionDataSchema.loads( await async_load_fixture(hass, "action.set_ac_start.json", DOMAIN) ) ), ) as mock_action: await hass.services.async_call( BUTTON_DOMAIN, SERVICE_PRESS, service_data=data, blocking=True ) assert len(mock_action.mock_calls) == 1 assert mock_action.mock_calls[0][1] == (21, None)