Add input_button support to Google Assistant (#62593)

This commit is contained in:
Franck Nijhof 2021-12-23 07:38:35 +01:00 committed by GitHub
parent 99b2161365
commit dc47cbd01b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 9 deletions

View File

@ -10,6 +10,7 @@ from homeassistant.components import (
group, group,
humidifier, humidifier,
input_boolean, input_boolean,
input_button,
input_select, input_select,
light, light,
lock, lock,
@ -129,6 +130,7 @@ DOMAIN_TO_GOOGLE_TYPES = {
group.DOMAIN: TYPE_SWITCH, group.DOMAIN: TYPE_SWITCH,
humidifier.DOMAIN: TYPE_HUMIDIFIER, humidifier.DOMAIN: TYPE_HUMIDIFIER,
input_boolean.DOMAIN: TYPE_SWITCH, input_boolean.DOMAIN: TYPE_SWITCH,
input_button.DOMAIN: TYPE_SCENE,
input_select.DOMAIN: TYPE_SENSOR, input_select.DOMAIN: TYPE_SENSOR,
light.DOMAIN: TYPE_LIGHT, light.DOMAIN: TYPE_LIGHT,
lock.DOMAIN: TYPE_LOCK, lock.DOMAIN: TYPE_LOCK,

View File

@ -12,6 +12,7 @@ from homeassistant.components import (
fan, fan,
group, group,
input_boolean, input_boolean,
input_button,
input_select, input_select,
light, light,
lock, lock,
@ -514,7 +515,12 @@ class SceneTrait(_Trait):
@staticmethod @staticmethod
def supported(domain, features, device_class, _): def supported(domain, features, device_class, _):
"""Test if state is supported.""" """Test if state is supported."""
return domain in (button.DOMAIN, scene.DOMAIN, script.DOMAIN) return domain in (
button.DOMAIN,
input_button.DOMAIN,
scene.DOMAIN,
script.DOMAIN,
)
def sync_attributes(self): def sync_attributes(self):
"""Return scene attributes for a sync request.""" """Return scene attributes for a sync request."""
@ -530,6 +536,8 @@ class SceneTrait(_Trait):
service = SERVICE_TURN_ON service = SERVICE_TURN_ON
if self.state.domain == button.DOMAIN: if self.state.domain == button.DOMAIN:
service = button.SERVICE_PRESS service = button.SERVICE_PRESS
elif self.state.domain == input_button.DOMAIN:
service = input_button.SERVICE_PRESS
# Don't block for scripts or buttons, as they can be slow. # Don't block for scripts or buttons, as they can be slow.
await self.hass.services.async_call( await self.hass.services.async_call(
@ -537,7 +545,8 @@ class SceneTrait(_Trait):
service, service,
{ATTR_ENTITY_ID: self.state.entity_id}, {ATTR_ENTITY_ID: self.state.entity_id},
blocking=(not self.config.should_report_state) blocking=(not self.config.should_report_state)
and self.state.domain not in (button.DOMAIN, script.DOMAIN), and self.state.domain
not in (button.DOMAIN, input_button.DOMAIN, script.DOMAIN),
context=data.context, context=data.context,
) )

View File

@ -13,6 +13,7 @@ from homeassistant.components import (
fan, fan,
group, group,
input_boolean, input_boolean,
input_button,
input_select, input_select,
light, light,
lock, lock,
@ -768,24 +769,30 @@ async def test_light_modes(hass):
} }
async def test_scene_button(hass): @pytest.mark.parametrize(
"""Test Scene trait support for the button domain.""" "component",
assert helpers.get_google_type(button.DOMAIN, None) is not None [button, input_button],
assert trait.SceneTrait.supported(button.DOMAIN, 0, None, None) )
async def test_scene_button(hass, component):
"""Test Scene trait support for the (input) button domain."""
assert helpers.get_google_type(component.DOMAIN, None) is not None
assert trait.SceneTrait.supported(component.DOMAIN, 0, None, None)
trt = trait.SceneTrait(hass, State("button.bla", STATE_UNKNOWN), BASIC_CONFIG) trt = trait.SceneTrait(
hass, State(f"{component.DOMAIN}.bla", STATE_UNKNOWN), BASIC_CONFIG
)
assert trt.sync_attributes() == {} assert trt.sync_attributes() == {}
assert trt.query_attributes() == {} assert trt.query_attributes() == {}
assert trt.can_execute(trait.COMMAND_ACTIVATE_SCENE, {}) assert trt.can_execute(trait.COMMAND_ACTIVATE_SCENE, {})
calls = async_mock_service(hass, button.DOMAIN, button.SERVICE_PRESS) calls = async_mock_service(hass, component.DOMAIN, component.SERVICE_PRESS)
await trt.execute(trait.COMMAND_ACTIVATE_SCENE, BASIC_DATA, {}, {}) await trt.execute(trait.COMMAND_ACTIVATE_SCENE, BASIC_DATA, {}, {})
# We don't wait till button press is done. # We don't wait till button press is done.
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1
assert calls[0].data == {ATTR_ENTITY_ID: "button.bla"} assert calls[0].data == {ATTR_ENTITY_ID: f"{component.DOMAIN}.bla"}
async def test_scene_scene(hass): async def test_scene_scene(hass):