mirror of
https://github.com/home-assistant/core
synced 2024-08-02 23:40:32 +02:00
Avoid creating bond stop action button when there are no other buttons (#64869)
This commit is contained in:
parent
70b24b7843
commit
0ca38c0928
@ -39,14 +39,16 @@ class BondButtonEntityDescription(
|
|||||||
"""Class to describe a Bond Button entity."""
|
"""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, ...] = (
|
BUTTONS: tuple[BondButtonEntityDescription, ...] = (
|
||||||
BondButtonEntityDescription(
|
|
||||||
key=Action.STOP,
|
|
||||||
name="Stop Actions",
|
|
||||||
icon="mdi:stop-circle-outline",
|
|
||||||
mutually_exclusive=None,
|
|
||||||
argument=None,
|
|
||||||
),
|
|
||||||
BondButtonEntityDescription(
|
BondButtonEntityDescription(
|
||||||
key=Action.TOGGLE_POWER,
|
key=Action.TOGGLE_POWER,
|
||||||
name="Toggle Power",
|
name="Toggle Power",
|
||||||
@ -233,17 +235,29 @@ async def async_setup_entry(
|
|||||||
data = hass.data[DOMAIN][entry.entry_id]
|
data = hass.data[DOMAIN][entry.entry_id]
|
||||||
hub: BondHub = data[HUB]
|
hub: BondHub = data[HUB]
|
||||||
bpup_subs: BPUPSubscriptions = data[BPUP_SUBS]
|
bpup_subs: BPUPSubscriptions = data[BPUP_SUBS]
|
||||||
|
entities: list[BondButtonEntity] = []
|
||||||
|
|
||||||
async_add_entities(
|
for device in hub.devices:
|
||||||
BondButtonEntity(hub, device, bpup_subs, description)
|
device_entities = [
|
||||||
for device in hub.devices
|
BondButtonEntity(hub, device, bpup_subs, description)
|
||||||
for description in BUTTONS
|
for description in BUTTONS
|
||||||
if device.has_action(description.key)
|
if device.has_action(description.key)
|
||||||
and (
|
and (
|
||||||
description.mutually_exclusive is None
|
description.mutually_exclusive is None
|
||||||
or not device.has_action(description.mutually_exclusive)
|
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):
|
class BondButtonEntity(BondEntity, ButtonEntity):
|
||||||
|
@ -13,6 +13,15 @@ from homeassistant.helpers.entity_registry import EntityRegistry
|
|||||||
from .common import patch_bond_action, patch_bond_device_state, setup_platform
|
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):
|
def light_brightness_increase_decrease_only(name: str):
|
||||||
"""Create a light that can only increase or decrease brightness."""
|
"""Create a light that can only increase or decrease brightness."""
|
||||||
return {
|
return {
|
||||||
@ -80,6 +89,18 @@ async def test_mutually_exclusive_actions(hass: core.HomeAssistant):
|
|||||||
assert not hass.states.async_all("button")
|
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):
|
async def test_press_button_with_argument(hass: core.HomeAssistant):
|
||||||
"""Tests we can press a button with an argument."""
|
"""Tests we can press a button with an argument."""
|
||||||
await setup_platform(
|
await setup_platform(
|
||||||
|
Loading…
Reference in New Issue
Block a user