From bef5fde832df91fa6ccb02f61d13c99944b42be0 Mon Sep 17 00:00:00 2001 From: rodriguestiago0 Date: Tue, 28 Feb 2023 09:28:44 +0000 Subject: [PATCH] Add stop charge button to renault integration (#88003) * Added service to start/stop charge * Remove comment * Fixed service * removed service for start/stop charge * Remove version Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Format Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Revert change * Fix lint * Add tests --------- Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> --- homeassistant/components/renault/button.py | 7 +++++ .../components/renault/renault_vehicle.py | 5 ++++ tests/components/renault/const.py | 18 ++++++++++++ .../fixtures/action.set_charge_stop.json | 7 +++++ tests/components/renault/test_button.py | 28 +++++++++++++++++++ 5 files changed, 65 insertions(+) create mode 100644 tests/components/renault/fixtures/action.set_charge_stop.json diff --git a/homeassistant/components/renault/button.py b/homeassistant/components/renault/button.py index b34e14d365a..67dfe8fc971 100644 --- a/homeassistant/components/renault/button.py +++ b/homeassistant/components/renault/button.py @@ -71,4 +71,11 @@ BUTTON_TYPES: tuple[RenaultButtonEntityDescription, ...] = ( name="Start charge", requires_electricity=True, ), + RenaultButtonEntityDescription( + async_press=lambda x: x.vehicle.set_charge_stop(), + key="stop_charge", + icon="mdi:ev-station", + name="Stop charge", + requires_electricity=True, + ), ) diff --git a/homeassistant/components/renault/renault_vehicle.py b/homeassistant/components/renault/renault_vehicle.py index 69835552ba4..9580ea2b7d0 100644 --- a/homeassistant/components/renault/renault_vehicle.py +++ b/homeassistant/components/renault/renault_vehicle.py @@ -151,6 +151,11 @@ class RenaultVehicleProxy: """Start vehicle charge.""" return await self._vehicle.set_charge_start() + @with_error_wrapping + async def set_charge_stop(self) -> models.KamereonVehicleChargingStartActionData: + """Stop vehicle charge.""" + return await self._vehicle.set_charge_stop() + @with_error_wrapping async def set_ac_stop(self) -> models.KamereonVehicleHvacStartActionData: """Stop vehicle ac.""" diff --git a/tests/components/renault/const.py b/tests/components/renault/const.py index ee4f1683aad..e17e6b64948 100644 --- a/tests/components/renault/const.py +++ b/tests/components/renault/const.py @@ -114,6 +114,12 @@ MOCK_VEHICLES = { ATTR_STATE: STATE_UNKNOWN, ATTR_UNIQUE_ID: "vf1aaaaa555777999_start_charge", }, + { + ATTR_ENTITY_ID: "button.reg_number_stop_charge", + ATTR_ICON: "mdi:ev-station", + ATTR_STATE: STATE_UNKNOWN, + ATTR_UNIQUE_ID: "vf1aaaaa555777999_stop_charge", + }, ], Platform.DEVICE_TRACKER: [], Platform.SELECT: [ @@ -336,6 +342,12 @@ MOCK_VEHICLES = { ATTR_STATE: STATE_UNKNOWN, ATTR_UNIQUE_ID: "vf1aaaaa555777999_start_charge", }, + { + ATTR_ENTITY_ID: "button.reg_number_stop_charge", + ATTR_ICON: "mdi:ev-station", + ATTR_STATE: STATE_UNKNOWN, + ATTR_UNIQUE_ID: "vf1aaaaa555777999_stop_charge", + }, ], Platform.DEVICE_TRACKER: [ { @@ -565,6 +577,12 @@ MOCK_VEHICLES = { ATTR_STATE: STATE_UNKNOWN, ATTR_UNIQUE_ID: "vf1aaaaa555777123_start_charge", }, + { + ATTR_ENTITY_ID: "button.reg_number_stop_charge", + ATTR_ICON: "mdi:ev-station", + ATTR_STATE: STATE_UNKNOWN, + ATTR_UNIQUE_ID: "vf1aaaaa555777123_stop_charge", + }, ], Platform.DEVICE_TRACKER: [ { diff --git a/tests/components/renault/fixtures/action.set_charge_stop.json b/tests/components/renault/fixtures/action.set_charge_stop.json new file mode 100644 index 00000000000..017059782b6 --- /dev/null +++ b/tests/components/renault/fixtures/action.set_charge_stop.json @@ -0,0 +1,7 @@ +{ + "data": { + "type": "ChargingStart", + "id": "guid", + "attributes": { "action": "stop" } + } +} diff --git a/tests/components/renault/test_button.py b/tests/components/renault/test_button.py index 8ec18e3b101..695be73089e 100644 --- a/tests/components/renault/test_button.py +++ b/tests/components/renault/test_button.py @@ -160,6 +160,34 @@ async def test_button_start_charge( 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_number_stop_charge", + } + + with patch( + "renault_api.renault_vehicle.RenaultVehicle.set_charge_stop", + return_value=( + schemas.KamereonVehicleChargingStartActionDataSchema.loads( + load_fixture("renault/action.set_charge_stop.json") + ) + ), + ) 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(