From 0ca38c0928daec2ceb6084367da340f0e9b052c6 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 24 Jan 2022 12:03:52 -1000 Subject: [PATCH] Avoid creating bond stop action button when there are no other buttons (#64869) --- homeassistant/components/bond/button.py | 48 ++++++++++++++++--------- tests/components/bond/test_button.py | 21 +++++++++++ 2 files changed, 52 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/bond/button.py b/homeassistant/components/bond/button.py index 72600cc2eea0..700c6b5f4079 100644 --- a/homeassistant/components/bond/button.py +++ b/homeassistant/components/bond/button.py @@ -39,14 +39,16 @@ class BondButtonEntityDescription( """Class to describe a Bond Button entity.""" +STOP_BUTTON = BondButtonEntityDescription( + key=Action.STOP, + name="Stop Actions", + icon="mdi:stop-circle-outline", + mutually_exclusive=None, + argument=None, +) + + BUTTONS: tuple[BondButtonEntityDescription, ...] = ( - BondButtonEntityDescription( - key=Action.STOP, - name="Stop Actions", - icon="mdi:stop-circle-outline", - mutually_exclusive=None, - argument=None, - ), BondButtonEntityDescription( key=Action.TOGGLE_POWER, name="Toggle Power", @@ -233,17 +235,29 @@ async def async_setup_entry( data = hass.data[DOMAIN][entry.entry_id] hub: BondHub = data[HUB] bpup_subs: BPUPSubscriptions = data[BPUP_SUBS] + entities: list[BondButtonEntity] = [] - async_add_entities( - BondButtonEntity(hub, device, bpup_subs, description) - for device in hub.devices - for description in BUTTONS - if device.has_action(description.key) - and ( - description.mutually_exclusive is None - or not device.has_action(description.mutually_exclusive) - ) - ) + for device in hub.devices: + device_entities = [ + BondButtonEntity(hub, device, bpup_subs, description) + for description in BUTTONS + if device.has_action(description.key) + and ( + description.mutually_exclusive is None + or not device.has_action(description.mutually_exclusive) + ) + ] + if device_entities and device.has_action(STOP_BUTTON.key): + # Most devices have the stop action available, but + # we only add the stop action button if we add actions + # since its not so useful if there are no actions to stop + device_entities.append( + BondButtonEntity(hub, device, bpup_subs, STOP_BUTTON) + ) + entities.extend(device_entities) + + if entities: + async_add_entities(entities) class BondButtonEntity(BondEntity, ButtonEntity): diff --git a/tests/components/bond/test_button.py b/tests/components/bond/test_button.py index 2d8578ec4455..ee6e98b84629 100644 --- a/tests/components/bond/test_button.py +++ b/tests/components/bond/test_button.py @@ -13,6 +13,15 @@ from homeassistant.helpers.entity_registry import EntityRegistry from .common import patch_bond_action, patch_bond_device_state, setup_platform +def ceiling_fan(name: str): + """Create a ceiling fan with given name.""" + return { + "name": name, + "type": DeviceType.CEILING_FAN, + "actions": [Action.SET_SPEED, Action.SET_DIRECTION, Action.STOP], + } + + def light_brightness_increase_decrease_only(name: str): """Create a light that can only increase or decrease brightness.""" return { @@ -80,6 +89,18 @@ async def test_mutually_exclusive_actions(hass: core.HomeAssistant): assert not hass.states.async_all("button") +async def test_stop_not_created_no_other_buttons(hass: core.HomeAssistant): + """Tests we do not create the stop button when there are no other buttons.""" + await setup_platform( + hass, + BUTTON_DOMAIN, + ceiling_fan("name-1"), + bond_device_id="test-device-id", + ) + + assert not hass.states.async_all("button") + + async def test_press_button_with_argument(hass: core.HomeAssistant): """Tests we can press a button with an argument.""" await setup_platform(