mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Fix bond buttons where an argument is required (#64781)
This commit is contained in:
parent
654bd7dd1f
commit
d1d33f0dc5
@ -18,12 +18,18 @@ from .utils import BondDevice, BondHub
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# The api requires a step size even though it does not
|
||||||
|
# seem to matter what is is as the underlying device is likely
|
||||||
|
# getting an increase/decrease signal only
|
||||||
|
STEP_SIZE = 10
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class BondButtonEntityDescriptionMixin:
|
class BondButtonEntityDescriptionMixin:
|
||||||
"""Mixin to describe a Bond Button entity."""
|
"""Mixin to describe a Bond Button entity."""
|
||||||
|
|
||||||
mutually_exclusive: Action | None
|
mutually_exclusive: Action | None
|
||||||
|
argument: int | None
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@ -39,153 +45,181 @@ BUTTONS: tuple[BondButtonEntityDescription, ...] = (
|
|||||||
name="Stop Actions",
|
name="Stop Actions",
|
||||||
icon="mdi:stop-circle-outline",
|
icon="mdi:stop-circle-outline",
|
||||||
mutually_exclusive=None,
|
mutually_exclusive=None,
|
||||||
|
argument=None,
|
||||||
),
|
),
|
||||||
BondButtonEntityDescription(
|
BondButtonEntityDescription(
|
||||||
key=Action.TOGGLE_POWER,
|
key=Action.TOGGLE_POWER,
|
||||||
name="Toggle Power",
|
name="Toggle Power",
|
||||||
icon="mdi:power-cycle",
|
icon="mdi:power-cycle",
|
||||||
mutually_exclusive=Action.TURN_ON,
|
mutually_exclusive=Action.TURN_ON,
|
||||||
|
argument=None,
|
||||||
),
|
),
|
||||||
BondButtonEntityDescription(
|
BondButtonEntityDescription(
|
||||||
key=Action.TOGGLE_LIGHT,
|
key=Action.TOGGLE_LIGHT,
|
||||||
name="Toggle Light",
|
name="Toggle Light",
|
||||||
icon="mdi:lightbulb",
|
icon="mdi:lightbulb",
|
||||||
mutually_exclusive=Action.TURN_LIGHT_ON,
|
mutually_exclusive=Action.TURN_LIGHT_ON,
|
||||||
|
argument=None,
|
||||||
),
|
),
|
||||||
BondButtonEntityDescription(
|
BondButtonEntityDescription(
|
||||||
key=Action.INCREASE_BRIGHTNESS,
|
key=Action.INCREASE_BRIGHTNESS,
|
||||||
name="Increase Brightness",
|
name="Increase Brightness",
|
||||||
icon="mdi:brightness-7",
|
icon="mdi:brightness-7",
|
||||||
mutually_exclusive=Action.SET_BRIGHTNESS,
|
mutually_exclusive=Action.SET_BRIGHTNESS,
|
||||||
|
argument=STEP_SIZE,
|
||||||
),
|
),
|
||||||
BondButtonEntityDescription(
|
BondButtonEntityDescription(
|
||||||
key=Action.DECREASE_BRIGHTNESS,
|
key=Action.DECREASE_BRIGHTNESS,
|
||||||
name="Decrease Brightness",
|
name="Decrease Brightness",
|
||||||
icon="mdi:brightness-1",
|
icon="mdi:brightness-1",
|
||||||
mutually_exclusive=Action.SET_BRIGHTNESS,
|
mutually_exclusive=Action.SET_BRIGHTNESS,
|
||||||
|
argument=STEP_SIZE,
|
||||||
),
|
),
|
||||||
BondButtonEntityDescription(
|
BondButtonEntityDescription(
|
||||||
key=Action.TOGGLE_UP_LIGHT,
|
key=Action.TOGGLE_UP_LIGHT,
|
||||||
name="Toggle Up Light",
|
name="Toggle Up Light",
|
||||||
icon="mdi:lightbulb",
|
icon="mdi:lightbulb",
|
||||||
mutually_exclusive=Action.TURN_UP_LIGHT_ON,
|
mutually_exclusive=Action.TURN_UP_LIGHT_ON,
|
||||||
|
argument=None,
|
||||||
),
|
),
|
||||||
BondButtonEntityDescription(
|
BondButtonEntityDescription(
|
||||||
key=Action.TOGGLE_DOWN_LIGHT,
|
key=Action.TOGGLE_DOWN_LIGHT,
|
||||||
name="Toggle Down Light",
|
name="Toggle Down Light",
|
||||||
icon="mdi:lightbulb",
|
icon="mdi:lightbulb",
|
||||||
mutually_exclusive=Action.TURN_DOWN_LIGHT_ON,
|
mutually_exclusive=Action.TURN_DOWN_LIGHT_ON,
|
||||||
|
argument=None,
|
||||||
),
|
),
|
||||||
BondButtonEntityDescription(
|
BondButtonEntityDescription(
|
||||||
key=Action.START_UP_LIGHT_DIMMER,
|
key=Action.START_UP_LIGHT_DIMMER,
|
||||||
name="Start Up Light Dimmer",
|
name="Start Up Light Dimmer",
|
||||||
icon="mdi:brightness-percent",
|
icon="mdi:brightness-percent",
|
||||||
mutually_exclusive=Action.SET_UP_LIGHT_BRIGHTNESS,
|
mutually_exclusive=Action.SET_UP_LIGHT_BRIGHTNESS,
|
||||||
|
argument=None,
|
||||||
),
|
),
|
||||||
BondButtonEntityDescription(
|
BondButtonEntityDescription(
|
||||||
key=Action.START_DOWN_LIGHT_DIMMER,
|
key=Action.START_DOWN_LIGHT_DIMMER,
|
||||||
name="Start Down Light Dimmer",
|
name="Start Down Light Dimmer",
|
||||||
icon="mdi:brightness-percent",
|
icon="mdi:brightness-percent",
|
||||||
mutually_exclusive=Action.SET_DOWN_LIGHT_BRIGHTNESS,
|
mutually_exclusive=Action.SET_DOWN_LIGHT_BRIGHTNESS,
|
||||||
|
argument=None,
|
||||||
),
|
),
|
||||||
BondButtonEntityDescription(
|
BondButtonEntityDescription(
|
||||||
key=Action.START_INCREASING_BRIGHTNESS,
|
key=Action.START_INCREASING_BRIGHTNESS,
|
||||||
name="Start Increasing Brightness",
|
name="Start Increasing Brightness",
|
||||||
icon="mdi:brightness-percent",
|
icon="mdi:brightness-percent",
|
||||||
mutually_exclusive=Action.SET_BRIGHTNESS,
|
mutually_exclusive=Action.SET_BRIGHTNESS,
|
||||||
|
argument=None,
|
||||||
),
|
),
|
||||||
BondButtonEntityDescription(
|
BondButtonEntityDescription(
|
||||||
key=Action.START_DECREASING_BRIGHTNESS,
|
key=Action.START_DECREASING_BRIGHTNESS,
|
||||||
name="Start Decreasing Brightness",
|
name="Start Decreasing Brightness",
|
||||||
icon="mdi:brightness-percent",
|
icon="mdi:brightness-percent",
|
||||||
mutually_exclusive=Action.SET_BRIGHTNESS,
|
mutually_exclusive=Action.SET_BRIGHTNESS,
|
||||||
|
argument=None,
|
||||||
),
|
),
|
||||||
BondButtonEntityDescription(
|
BondButtonEntityDescription(
|
||||||
key=Action.INCREASE_UP_LIGHT_BRIGHTNESS,
|
key=Action.INCREASE_UP_LIGHT_BRIGHTNESS,
|
||||||
name="Increase Up Light Brightness",
|
name="Increase Up Light Brightness",
|
||||||
icon="mdi:brightness-percent",
|
icon="mdi:brightness-percent",
|
||||||
mutually_exclusive=Action.SET_UP_LIGHT_BRIGHTNESS,
|
mutually_exclusive=Action.SET_UP_LIGHT_BRIGHTNESS,
|
||||||
|
argument=STEP_SIZE,
|
||||||
),
|
),
|
||||||
BondButtonEntityDescription(
|
BondButtonEntityDescription(
|
||||||
key=Action.DECREASE_UP_LIGHT_BRIGHTNESS,
|
key=Action.DECREASE_UP_LIGHT_BRIGHTNESS,
|
||||||
name="Decrease Up Light Brightness",
|
name="Decrease Up Light Brightness",
|
||||||
icon="mdi:brightness-percent",
|
icon="mdi:brightness-percent",
|
||||||
mutually_exclusive=Action.SET_UP_LIGHT_BRIGHTNESS,
|
mutually_exclusive=Action.SET_UP_LIGHT_BRIGHTNESS,
|
||||||
|
argument=STEP_SIZE,
|
||||||
),
|
),
|
||||||
BondButtonEntityDescription(
|
BondButtonEntityDescription(
|
||||||
key=Action.INCREASE_DOWN_LIGHT_BRIGHTNESS,
|
key=Action.INCREASE_DOWN_LIGHT_BRIGHTNESS,
|
||||||
name="Increase Down Light Brightness",
|
name="Increase Down Light Brightness",
|
||||||
icon="mdi:brightness-percent",
|
icon="mdi:brightness-percent",
|
||||||
mutually_exclusive=Action.SET_DOWN_LIGHT_BRIGHTNESS,
|
mutually_exclusive=Action.SET_DOWN_LIGHT_BRIGHTNESS,
|
||||||
|
argument=STEP_SIZE,
|
||||||
),
|
),
|
||||||
BondButtonEntityDescription(
|
BondButtonEntityDescription(
|
||||||
key=Action.DECREASE_DOWN_LIGHT_BRIGHTNESS,
|
key=Action.DECREASE_DOWN_LIGHT_BRIGHTNESS,
|
||||||
name="Decrease Down Light Brightness",
|
name="Decrease Down Light Brightness",
|
||||||
icon="mdi:brightness-percent",
|
icon="mdi:brightness-percent",
|
||||||
mutually_exclusive=Action.SET_DOWN_LIGHT_BRIGHTNESS,
|
mutually_exclusive=Action.SET_DOWN_LIGHT_BRIGHTNESS,
|
||||||
|
argument=STEP_SIZE,
|
||||||
),
|
),
|
||||||
BondButtonEntityDescription(
|
BondButtonEntityDescription(
|
||||||
key=Action.CYCLE_UP_LIGHT_BRIGHTNESS,
|
key=Action.CYCLE_UP_LIGHT_BRIGHTNESS,
|
||||||
name="Cycle Up Light Brightness",
|
name="Cycle Up Light Brightness",
|
||||||
icon="mdi:brightness-percent",
|
icon="mdi:brightness-percent",
|
||||||
mutually_exclusive=Action.SET_UP_LIGHT_BRIGHTNESS,
|
mutually_exclusive=Action.SET_UP_LIGHT_BRIGHTNESS,
|
||||||
|
argument=STEP_SIZE,
|
||||||
),
|
),
|
||||||
BondButtonEntityDescription(
|
BondButtonEntityDescription(
|
||||||
key=Action.CYCLE_DOWN_LIGHT_BRIGHTNESS,
|
key=Action.CYCLE_DOWN_LIGHT_BRIGHTNESS,
|
||||||
name="Cycle Down Light Brightness",
|
name="Cycle Down Light Brightness",
|
||||||
icon="mdi:brightness-percent",
|
icon="mdi:brightness-percent",
|
||||||
mutually_exclusive=Action.SET_DOWN_LIGHT_BRIGHTNESS,
|
mutually_exclusive=Action.SET_DOWN_LIGHT_BRIGHTNESS,
|
||||||
|
argument=STEP_SIZE,
|
||||||
),
|
),
|
||||||
BondButtonEntityDescription(
|
BondButtonEntityDescription(
|
||||||
key=Action.CYCLE_BRIGHTNESS,
|
key=Action.CYCLE_BRIGHTNESS,
|
||||||
name="Cycle Brightness",
|
name="Cycle Brightness",
|
||||||
icon="mdi:brightness-percent",
|
icon="mdi:brightness-percent",
|
||||||
mutually_exclusive=Action.SET_BRIGHTNESS,
|
mutually_exclusive=Action.SET_BRIGHTNESS,
|
||||||
|
argument=STEP_SIZE,
|
||||||
),
|
),
|
||||||
BondButtonEntityDescription(
|
BondButtonEntityDescription(
|
||||||
key=Action.INCREASE_SPEED,
|
key=Action.INCREASE_SPEED,
|
||||||
name="Increase Speed",
|
name="Increase Speed",
|
||||||
icon="mdi:skew-more",
|
icon="mdi:skew-more",
|
||||||
mutually_exclusive=Action.SET_SPEED,
|
mutually_exclusive=Action.SET_SPEED,
|
||||||
|
argument=1,
|
||||||
),
|
),
|
||||||
BondButtonEntityDescription(
|
BondButtonEntityDescription(
|
||||||
key=Action.DECREASE_SPEED,
|
key=Action.DECREASE_SPEED,
|
||||||
name="Decrease Speed",
|
name="Decrease Speed",
|
||||||
icon="mdi:skew-less",
|
icon="mdi:skew-less",
|
||||||
mutually_exclusive=Action.SET_SPEED,
|
mutually_exclusive=Action.SET_SPEED,
|
||||||
|
argument=1,
|
||||||
),
|
),
|
||||||
BondButtonEntityDescription(
|
BondButtonEntityDescription(
|
||||||
key=Action.TOGGLE_DIRECTION,
|
key=Action.TOGGLE_DIRECTION,
|
||||||
name="Toggle Direction",
|
name="Toggle Direction",
|
||||||
icon="mdi:directions-fork",
|
icon="mdi:directions-fork",
|
||||||
mutually_exclusive=Action.SET_DIRECTION,
|
mutually_exclusive=Action.SET_DIRECTION,
|
||||||
|
argument=None,
|
||||||
),
|
),
|
||||||
BondButtonEntityDescription(
|
BondButtonEntityDescription(
|
||||||
key=Action.INCREASE_TEMPERATURE,
|
key=Action.INCREASE_TEMPERATURE,
|
||||||
name="Increase Temperature",
|
name="Increase Temperature",
|
||||||
icon="mdi:thermometer-plus",
|
icon="mdi:thermometer-plus",
|
||||||
mutually_exclusive=None,
|
mutually_exclusive=None,
|
||||||
|
argument=1,
|
||||||
),
|
),
|
||||||
BondButtonEntityDescription(
|
BondButtonEntityDescription(
|
||||||
key=Action.DECREASE_TEMPERATURE,
|
key=Action.DECREASE_TEMPERATURE,
|
||||||
name="Decrease Temperature",
|
name="Decrease Temperature",
|
||||||
icon="mdi:thermometer-minus",
|
icon="mdi:thermometer-minus",
|
||||||
mutually_exclusive=None,
|
mutually_exclusive=None,
|
||||||
|
argument=1,
|
||||||
),
|
),
|
||||||
BondButtonEntityDescription(
|
BondButtonEntityDescription(
|
||||||
key=Action.INCREASE_FLAME,
|
key=Action.INCREASE_FLAME,
|
||||||
name="Increase Flame",
|
name="Increase Flame",
|
||||||
icon="mdi:fire",
|
icon="mdi:fire",
|
||||||
mutually_exclusive=None,
|
mutually_exclusive=None,
|
||||||
|
argument=STEP_SIZE,
|
||||||
),
|
),
|
||||||
BondButtonEntityDescription(
|
BondButtonEntityDescription(
|
||||||
key=Action.DECREASE_FLAME,
|
key=Action.DECREASE_FLAME,
|
||||||
name="Decrease Flame",
|
name="Decrease Flame",
|
||||||
icon="mdi:fire-off",
|
icon="mdi:fire-off",
|
||||||
mutually_exclusive=None,
|
mutually_exclusive=None,
|
||||||
|
argument=STEP_SIZE,
|
||||||
),
|
),
|
||||||
BondButtonEntityDescription(
|
BondButtonEntityDescription(
|
||||||
key=Action.TOGGLE_OPEN, name="Toggle Open", mutually_exclusive=Action.OPEN
|
key=Action.TOGGLE_OPEN,
|
||||||
|
name="Toggle Open",
|
||||||
|
mutually_exclusive=Action.OPEN,
|
||||||
|
argument=None,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -215,12 +249,14 @@ async def async_setup_entry(
|
|||||||
class BondButtonEntity(BondEntity, ButtonEntity):
|
class BondButtonEntity(BondEntity, ButtonEntity):
|
||||||
"""Bond Button Device."""
|
"""Bond Button Device."""
|
||||||
|
|
||||||
|
entity_description: BondButtonEntityDescription
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
hub: BondHub,
|
hub: BondHub,
|
||||||
device: BondDevice,
|
device: BondDevice,
|
||||||
bpup_subs: BPUPSubscriptions,
|
bpup_subs: BPUPSubscriptions,
|
||||||
description: ButtonEntityDescription,
|
description: BondButtonEntityDescription,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Init Bond button."""
|
"""Init Bond button."""
|
||||||
super().__init__(
|
super().__init__(
|
||||||
@ -230,9 +266,13 @@ class BondButtonEntity(BondEntity, ButtonEntity):
|
|||||||
|
|
||||||
async def async_press(self, **kwargs: Any) -> None:
|
async def async_press(self, **kwargs: Any) -> None:
|
||||||
"""Press the button."""
|
"""Press the button."""
|
||||||
await self._hub.bond.action(
|
if self.entity_description.argument:
|
||||||
self._device.device_id, Action(self.entity_description.key)
|
action = Action(
|
||||||
)
|
self.entity_description.key, self.entity_description.argument
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
action = Action(self.entity_description.key)
|
||||||
|
await self._hub.bond.action(self._device.device_id, action)
|
||||||
|
|
||||||
def _apply_state(self, state: dict) -> None:
|
def _apply_state(self, state: dict) -> None:
|
||||||
"""Apply the state."""
|
"""Apply the state."""
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
from bond_api import Action, DeviceType
|
from bond_api import Action, DeviceType
|
||||||
|
|
||||||
from homeassistant import core
|
from homeassistant import core
|
||||||
|
from homeassistant.components.bond.button import STEP_SIZE
|
||||||
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN
|
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN
|
||||||
from homeassistant.components.button.const import SERVICE_PRESS
|
from homeassistant.components.button.const import SERVICE_PRESS
|
||||||
from homeassistant.const import ATTR_ENTITY_ID
|
from homeassistant.const import ATTR_ENTITY_ID
|
||||||
@ -79,8 +80,8 @@ 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_press_button(hass: core.HomeAssistant):
|
async def test_press_button_with_argument(hass: core.HomeAssistant):
|
||||||
"""Tests we can press a button."""
|
"""Tests we can press a button with an argument."""
|
||||||
await setup_platform(
|
await setup_platform(
|
||||||
hass,
|
hass,
|
||||||
BUTTON_DOMAIN,
|
BUTTON_DOMAIN,
|
||||||
@ -100,7 +101,9 @@ async def test_press_button(hass: core.HomeAssistant):
|
|||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
mock_action.assert_called_once_with("test-device-id", Action(Action.INCREASE_FLAME))
|
mock_action.assert_called_once_with(
|
||||||
|
"test-device-id", Action(Action.INCREASE_FLAME, STEP_SIZE)
|
||||||
|
)
|
||||||
|
|
||||||
with patch_bond_action() as mock_action, patch_bond_device_state():
|
with patch_bond_action() as mock_action, patch_bond_device_state():
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
@ -111,4 +114,45 @@ async def test_press_button(hass: core.HomeAssistant):
|
|||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
mock_action.assert_called_once_with("test-device-id", Action(Action.DECREASE_FLAME))
|
mock_action.assert_called_once_with(
|
||||||
|
"test-device-id", Action(Action.DECREASE_FLAME, STEP_SIZE)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_press_button(hass: core.HomeAssistant):
|
||||||
|
"""Tests we can press a button."""
|
||||||
|
await setup_platform(
|
||||||
|
hass,
|
||||||
|
BUTTON_DOMAIN,
|
||||||
|
light_brightness_increase_decrease_only("name-1"),
|
||||||
|
bond_device_id="test-device-id",
|
||||||
|
)
|
||||||
|
|
||||||
|
assert hass.states.get("button.name_1_start_increasing_brightness")
|
||||||
|
assert hass.states.get("button.name_1_start_decreasing_brightness")
|
||||||
|
|
||||||
|
with patch_bond_action() as mock_action, patch_bond_device_state():
|
||||||
|
await hass.services.async_call(
|
||||||
|
BUTTON_DOMAIN,
|
||||||
|
SERVICE_PRESS,
|
||||||
|
{ATTR_ENTITY_ID: "button.name_1_start_increasing_brightness"},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
mock_action.assert_called_once_with(
|
||||||
|
"test-device-id", Action(Action.START_INCREASING_BRIGHTNESS)
|
||||||
|
)
|
||||||
|
|
||||||
|
with patch_bond_action() as mock_action, patch_bond_device_state():
|
||||||
|
await hass.services.async_call(
|
||||||
|
BUTTON_DOMAIN,
|
||||||
|
SERVICE_PRESS,
|
||||||
|
{ATTR_ENTITY_ID: "button.name_1_start_decreasing_brightness"},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
mock_action.assert_called_once_with(
|
||||||
|
"test-device-id", Action(Action.START_DECREASING_BRIGHTNESS)
|
||||||
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user