From 1f72a5b1fe520c60555756ec39208e81c9c74955 Mon Sep 17 00:00:00 2001 From: Brandon Rothweiler Date: Wed, 28 Jun 2023 21:46:08 -0400 Subject: [PATCH] Remove incompatible button entities for Mazda electric vehicles (#95486) * Remove incompatible button entities for Mazda electric vehicles * Update tests --- homeassistant/components/mazda/button.py | 4 ++ tests/components/mazda/test_button.py | 75 +++++++++++------------- 2 files changed, 37 insertions(+), 42 deletions(-) diff --git a/homeassistant/components/mazda/button.py b/homeassistant/components/mazda/button.py index 99a1a4ac2ff889..1b1e51db0358f6 100644 --- a/homeassistant/components/mazda/button.py +++ b/homeassistant/components/mazda/button.py @@ -78,21 +78,25 @@ class MazdaButtonEntityDescription(ButtonEntityDescription): key="start_engine", name="Start engine", icon="mdi:engine", + is_supported=lambda data: not data["isElectric"], ), MazdaButtonEntityDescription( key="stop_engine", name="Stop engine", icon="mdi:engine-off", + is_supported=lambda data: not data["isElectric"], ), MazdaButtonEntityDescription( key="turn_on_hazard_lights", name="Turn on hazard lights", icon="mdi:hazard-lights", + is_supported=lambda data: not data["isElectric"], ), MazdaButtonEntityDescription( key="turn_off_hazard_lights", name="Turn off hazard lights", icon="mdi:hazard-lights", + is_supported=lambda data: not data["isElectric"], ), MazdaButtonEntityDescription( key="refresh_vehicle_status", diff --git a/tests/components/mazda/test_button.py b/tests/components/mazda/test_button.py index 535ddcc963b62f..ba80c10b38d81d 100644 --- a/tests/components/mazda/test_button.py +++ b/tests/components/mazda/test_button.py @@ -65,40 +65,6 @@ async def test_button_setup_electric_vehicle(hass: HomeAssistant) -> None: entity_registry = er.async_get(hass) - entry = entity_registry.async_get("button.my_mazda3_start_engine") - assert entry - assert entry.unique_id == "JM000000000000000_start_engine" - state = hass.states.get("button.my_mazda3_start_engine") - assert state - assert state.attributes.get(ATTR_FRIENDLY_NAME) == "My Mazda3 Start engine" - assert state.attributes.get(ATTR_ICON) == "mdi:engine" - - entry = entity_registry.async_get("button.my_mazda3_stop_engine") - assert entry - assert entry.unique_id == "JM000000000000000_stop_engine" - state = hass.states.get("button.my_mazda3_stop_engine") - assert state - assert state.attributes.get(ATTR_FRIENDLY_NAME) == "My Mazda3 Stop engine" - assert state.attributes.get(ATTR_ICON) == "mdi:engine-off" - - entry = entity_registry.async_get("button.my_mazda3_turn_on_hazard_lights") - assert entry - assert entry.unique_id == "JM000000000000000_turn_on_hazard_lights" - state = hass.states.get("button.my_mazda3_turn_on_hazard_lights") - assert state - assert state.attributes.get(ATTR_FRIENDLY_NAME) == "My Mazda3 Turn on hazard lights" - assert state.attributes.get(ATTR_ICON) == "mdi:hazard-lights" - - entry = entity_registry.async_get("button.my_mazda3_turn_off_hazard_lights") - assert entry - assert entry.unique_id == "JM000000000000000_turn_off_hazard_lights" - state = hass.states.get("button.my_mazda3_turn_off_hazard_lights") - assert state - assert ( - state.attributes.get(ATTR_FRIENDLY_NAME) == "My Mazda3 Turn off hazard lights" - ) - assert state.attributes.get(ATTR_ICON) == "mdi:hazard-lights" - entry = entity_registry.async_get("button.my_mazda3_refresh_status") assert entry assert entry.unique_id == "JM000000000000000_refresh_vehicle_status" @@ -109,20 +75,45 @@ async def test_button_setup_electric_vehicle(hass: HomeAssistant) -> None: @pytest.mark.parametrize( - ("entity_id_suffix", "api_method_name"), + ("electric_vehicle", "entity_id_suffix"), + [ + (True, "start_engine"), + (True, "stop_engine"), + (True, "turn_on_hazard_lights"), + (True, "turn_off_hazard_lights"), + (False, "refresh_status"), + ], +) +async def test_button_not_created( + hass: HomeAssistant, electric_vehicle, entity_id_suffix +) -> None: + """Test that button entities are not created when they should not be.""" + await init_integration(hass, electric_vehicle=electric_vehicle) + + entity_registry = er.async_get(hass) + + entity_id = f"button.my_mazda3_{entity_id_suffix}" + entry = entity_registry.async_get(entity_id) + assert entry is None + state = hass.states.get(entity_id) + assert state is None + + +@pytest.mark.parametrize( + ("electric_vehicle", "entity_id_suffix", "api_method_name"), [ - ("start_engine", "start_engine"), - ("stop_engine", "stop_engine"), - ("turn_on_hazard_lights", "turn_on_hazard_lights"), - ("turn_off_hazard_lights", "turn_off_hazard_lights"), - ("refresh_status", "refresh_vehicle_status"), + (False, "start_engine", "start_engine"), + (False, "stop_engine", "stop_engine"), + (False, "turn_on_hazard_lights", "turn_on_hazard_lights"), + (False, "turn_off_hazard_lights", "turn_off_hazard_lights"), + (True, "refresh_status", "refresh_vehicle_status"), ], ) async def test_button_press( - hass: HomeAssistant, entity_id_suffix, api_method_name + hass: HomeAssistant, electric_vehicle, entity_id_suffix, api_method_name ) -> None: """Test pressing the button entities.""" - client_mock = await init_integration(hass, electric_vehicle=True) + client_mock = await init_integration(hass, electric_vehicle=electric_vehicle) await hass.services.async_call( BUTTON_DOMAIN,