Add support for fan speed percentage and preset modes to google_assistant integration (#50283)

* support relative fan speeds

* fan preset modes

* improve tests

* Revert relative speed code report zero percentage
This commit is contained in:
Jan Bouwhuis
2021-06-02 22:09:22 +02:00
committed by GitHub
parent 132ee972bd
commit 2222a121f4
3 changed files with 149 additions and 26 deletions
+12 -3
View File
@@ -247,14 +247,20 @@ DEMO_DEVICES = [
{
"id": "fan.living_room_fan",
"name": {"name": "Living Room Fan"},
"traits": ["action.devices.traits.FanSpeed", "action.devices.traits.OnOff"],
"traits": [
"action.devices.traits.FanSpeed",
"action.devices.traits.OnOff",
],
"type": "action.devices.types.FAN",
"willReportState": False,
},
{
"id": "fan.ceiling_fan",
"name": {"name": "Ceiling Fan"},
"traits": ["action.devices.traits.FanSpeed", "action.devices.traits.OnOff"],
"traits": [
"action.devices.traits.FanSpeed",
"action.devices.traits.OnOff",
],
"type": "action.devices.types.FAN",
"willReportState": False,
},
@@ -275,7 +281,10 @@ DEMO_DEVICES = [
{
"id": "fan.preset_only_limited_fan",
"name": {"name": "Preset Only Limited Fan"},
"traits": ["action.devices.traits.OnOff"],
"traits": [
"action.devices.traits.OnOff",
"action.devices.traits.Modes",
],
"type": "action.devices.types.FAN",
"willReportState": False,
},
@@ -1429,6 +1429,7 @@ async def test_fan_speed(hass):
],
"speed": "low",
"percentage": 33,
"percentage_step": 1.0,
},
),
BASIC_CONFIG,
@@ -1951,6 +1952,97 @@ async def test_sound_modes(hass):
}
async def test_preset_modes(hass):
"""Test Mode trait for fan preset modes."""
assert helpers.get_google_type(fan.DOMAIN, None) is not None
assert trait.ModesTrait.supported(fan.DOMAIN, fan.SUPPORT_PRESET_MODE, None, None)
trt = trait.ModesTrait(
hass,
State(
"fan.living_room",
STATE_ON,
attributes={
fan.ATTR_PRESET_MODES: ["auto", "whoosh"],
fan.ATTR_PRESET_MODE: "auto",
ATTR_SUPPORTED_FEATURES: fan.SUPPORT_PRESET_MODE,
},
),
BASIC_CONFIG,
)
attribs = trt.sync_attributes()
assert attribs == {
"availableModes": [
{
"name": "preset mode",
"name_values": [
{"name_synonym": ["preset mode", "mode", "preset"], "lang": "en"}
],
"settings": [
{
"setting_name": "auto",
"setting_values": [{"setting_synonym": ["auto"], "lang": "en"}],
},
{
"setting_name": "whoosh",
"setting_values": [
{"setting_synonym": ["whoosh"], "lang": "en"}
],
},
],
"ordered": False,
}
]
}
assert trt.query_attributes() == {
"currentModeSettings": {"preset mode": "auto"},
"on": True,
}
assert trt.can_execute(
trait.COMMAND_MODES,
params={"updateModeSettings": {"preset mode": "auto"}},
)
calls = async_mock_service(hass, fan.DOMAIN, fan.SERVICE_SET_PRESET_MODE)
await trt.execute(
trait.COMMAND_MODES,
BASIC_DATA,
{"updateModeSettings": {"preset mode": "auto"}},
{},
)
assert len(calls) == 1
assert calls[0].data == {
"entity_id": "fan.living_room",
"preset_mode": "auto",
}
async def test_traits_unknown_domains(hass, caplog):
"""Test Mode trait for unsupported domain."""
trt = trait.ModesTrait(
hass,
State(
"switch.living_room",
STATE_ON,
),
BASIC_CONFIG,
)
assert trt.supported("not_supported_domain", False, None, None) is False
await trt.execute(
trait.COMMAND_MODES,
BASIC_DATA,
{"updateModeSettings": {}},
{},
)
assert "Received an Options command for unrecognised domain" in caplog.text
caplog.clear()
async def test_openclose_cover(hass):
"""Test OpenClose trait support for cover domain."""
assert helpers.get_google_type(cover.DOMAIN, None) is not None