Fix bond buttons where an argument is required (#64781)

pull/64869/head
J. Nick Koston 2022-01-24 07:41:23 -10:00 committed by GitHub
parent 654bd7dd1f
commit d1d33f0dc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 93 additions and 9 deletions

View File

@ -18,12 +18,18 @@ from .utils import BondDevice, BondHub
_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
class BondButtonEntityDescriptionMixin:
"""Mixin to describe a Bond Button entity."""
mutually_exclusive: Action | None
argument: int | None
@dataclass
@ -39,153 +45,181 @@ BUTTONS: tuple[BondButtonEntityDescription, ...] = (
name="Stop Actions",
icon="mdi:stop-circle-outline",
mutually_exclusive=None,
argument=None,
),
BondButtonEntityDescription(
key=Action.TOGGLE_POWER,
name="Toggle Power",
icon="mdi:power-cycle",
mutually_exclusive=Action.TURN_ON,
argument=None,
),
BondButtonEntityDescription(
key=Action.TOGGLE_LIGHT,
name="Toggle Light",
icon="mdi:lightbulb",
mutually_exclusive=Action.TURN_LIGHT_ON,
argument=None,
),
BondButtonEntityDescription(
key=Action.INCREASE_BRIGHTNESS,
name="Increase Brightness",
icon="mdi:brightness-7",
mutually_exclusive=Action.SET_BRIGHTNESS,
argument=STEP_SIZE,
),
BondButtonEntityDescription(
key=Action.DECREASE_BRIGHTNESS,
name="Decrease Brightness",
icon="mdi:brightness-1",
mutually_exclusive=Action.SET_BRIGHTNESS,
argument=STEP_SIZE,
),
BondButtonEntityDescription(
key=Action.TOGGLE_UP_LIGHT,
name="Toggle Up Light",
icon="mdi:lightbulb",
mutually_exclusive=Action.TURN_UP_LIGHT_ON,
argument=None,
),
BondButtonEntityDescription(
key=Action.TOGGLE_DOWN_LIGHT,
name="Toggle Down Light",
icon="mdi:lightbulb",
mutually_exclusive=Action.TURN_DOWN_LIGHT_ON,
argument=None,
),
BondButtonEntityDescription(
key=Action.START_UP_LIGHT_DIMMER,
name="Start Up Light Dimmer",
icon="mdi:brightness-percent",
mutually_exclusive=Action.SET_UP_LIGHT_BRIGHTNESS,
argument=None,
),
BondButtonEntityDescription(
key=Action.START_DOWN_LIGHT_DIMMER,
name="Start Down Light Dimmer",
icon="mdi:brightness-percent",
mutually_exclusive=Action.SET_DOWN_LIGHT_BRIGHTNESS,
argument=None,
),
BondButtonEntityDescription(
key=Action.START_INCREASING_BRIGHTNESS,
name="Start Increasing Brightness",
icon="mdi:brightness-percent",
mutually_exclusive=Action.SET_BRIGHTNESS,
argument=None,
),
BondButtonEntityDescription(
key=Action.START_DECREASING_BRIGHTNESS,
name="Start Decreasing Brightness",
icon="mdi:brightness-percent",
mutually_exclusive=Action.SET_BRIGHTNESS,
argument=None,
),
BondButtonEntityDescription(
key=Action.INCREASE_UP_LIGHT_BRIGHTNESS,
name="Increase Up Light Brightness",
icon="mdi:brightness-percent",
mutually_exclusive=Action.SET_UP_LIGHT_BRIGHTNESS,
argument=STEP_SIZE,
),
BondButtonEntityDescription(
key=Action.DECREASE_UP_LIGHT_BRIGHTNESS,
name="Decrease Up Light Brightness",
icon="mdi:brightness-percent",
mutually_exclusive=Action.SET_UP_LIGHT_BRIGHTNESS,
argument=STEP_SIZE,
),
BondButtonEntityDescription(
key=Action.INCREASE_DOWN_LIGHT_BRIGHTNESS,
name="Increase Down Light Brightness",
icon="mdi:brightness-percent",
mutually_exclusive=Action.SET_DOWN_LIGHT_BRIGHTNESS,
argument=STEP_SIZE,
),
BondButtonEntityDescription(
key=Action.DECREASE_DOWN_LIGHT_BRIGHTNESS,
name="Decrease Down Light Brightness",
icon="mdi:brightness-percent",
mutually_exclusive=Action.SET_DOWN_LIGHT_BRIGHTNESS,
argument=STEP_SIZE,
),
BondButtonEntityDescription(
key=Action.CYCLE_UP_LIGHT_BRIGHTNESS,
name="Cycle Up Light Brightness",
icon="mdi:brightness-percent",
mutually_exclusive=Action.SET_UP_LIGHT_BRIGHTNESS,
argument=STEP_SIZE,
),
BondButtonEntityDescription(
key=Action.CYCLE_DOWN_LIGHT_BRIGHTNESS,
name="Cycle Down Light Brightness",
icon="mdi:brightness-percent",
mutually_exclusive=Action.SET_DOWN_LIGHT_BRIGHTNESS,
argument=STEP_SIZE,
),
BondButtonEntityDescription(
key=Action.CYCLE_BRIGHTNESS,
name="Cycle Brightness",
icon="mdi:brightness-percent",
mutually_exclusive=Action.SET_BRIGHTNESS,
argument=STEP_SIZE,
),
BondButtonEntityDescription(
key=Action.INCREASE_SPEED,
name="Increase Speed",
icon="mdi:skew-more",
mutually_exclusive=Action.SET_SPEED,
argument=1,
),
BondButtonEntityDescription(
key=Action.DECREASE_SPEED,
name="Decrease Speed",
icon="mdi:skew-less",
mutually_exclusive=Action.SET_SPEED,
argument=1,
),
BondButtonEntityDescription(
key=Action.TOGGLE_DIRECTION,
name="Toggle Direction",
icon="mdi:directions-fork",
mutually_exclusive=Action.SET_DIRECTION,
argument=None,
),
BondButtonEntityDescription(
key=Action.INCREASE_TEMPERATURE,
name="Increase Temperature",
icon="mdi:thermometer-plus",
mutually_exclusive=None,
argument=1,
),
BondButtonEntityDescription(
key=Action.DECREASE_TEMPERATURE,
name="Decrease Temperature",
icon="mdi:thermometer-minus",
mutually_exclusive=None,
argument=1,
),
BondButtonEntityDescription(
key=Action.INCREASE_FLAME,
name="Increase Flame",
icon="mdi:fire",
mutually_exclusive=None,
argument=STEP_SIZE,
),
BondButtonEntityDescription(
key=Action.DECREASE_FLAME,
name="Decrease Flame",
icon="mdi:fire-off",
mutually_exclusive=None,
argument=STEP_SIZE,
),
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):
"""Bond Button Device."""
entity_description: BondButtonEntityDescription
def __init__(
self,
hub: BondHub,
device: BondDevice,
bpup_subs: BPUPSubscriptions,
description: ButtonEntityDescription,
description: BondButtonEntityDescription,
) -> None:
"""Init Bond button."""
super().__init__(
@ -230,9 +266,13 @@ class BondButtonEntity(BondEntity, ButtonEntity):
async def async_press(self, **kwargs: Any) -> None:
"""Press the button."""
await self._hub.bond.action(
self._device.device_id, Action(self.entity_description.key)
)
if self.entity_description.argument:
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:
"""Apply the state."""

View File

@ -3,6 +3,7 @@
from bond_api import Action, DeviceType
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.const import SERVICE_PRESS
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")
async def test_press_button(hass: core.HomeAssistant):
"""Tests we can press a button."""
async def test_press_button_with_argument(hass: core.HomeAssistant):
"""Tests we can press a button with an argument."""
await setup_platform(
hass,
BUTTON_DOMAIN,
@ -100,7 +101,9 @@ async def test_press_button(hass: core.HomeAssistant):
)
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():
await hass.services.async_call(
@ -111,4 +114,45 @@ async def test_press_button(hass: core.HomeAssistant):
)
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)
)