Skip to content

Commit

Permalink
Remove incompatible button entities for Mazda electric vehicles (#95486)
Browse files Browse the repository at this point in the history
* Remove incompatible button entities for Mazda electric vehicles

* Update tests
  • Loading branch information
bdr99 authored and balloob committed Jun 29, 2023
1 parent 71b192c commit 1f72a5b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 42 deletions.
4 changes: 4 additions & 0 deletions homeassistant/components/mazda/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
75 changes: 33 additions & 42 deletions tests/components/mazda/test_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand Down

0 comments on commit 1f72a5b

Please sign in to comment.