Implement ToggleController, RangeController, and ModeController in alexa (#27302)
* Implement AlexaToggleController, AlexaRangeController, and AlexaModeController interfaces. * Implement AlexaToggleController, AlexaRangeController, and AlexaModeController interfaces. * Unkerfuffled comments to please the pydocstyle gods. * Unkerfuffled comments in Tests to please the pydocstyle gods. * Added additional test for more coverage. * Removed OSCILLATING property check from from ModeController. * Added capability report tests for ModeController, ToggleController, RangeController, PowerLevelController. * Update homeassistant/components/alexa/capabilities.py Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io> * Update homeassistant/components/alexa/capabilities.py Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io> * Corrected mis-spelling of AlexaCapability class. * Changed instance from method to property in AlexaCapability class. * Refactored to add {entity.domain}.{entity.attribute} to the instance name. * Improved type handling for configuration object. Added additional test for configuration object. * Added Tests for unsupported domains for ModeController and RangeController * Made changes to improve future scaling for other domains. * Split fan range to speed maps into multiple constants.
This commit is contained in:
1 parent
dc3aa43f73
commit
da094e09fa
10 files changed
+1190
-53
No files matched your search
@@ -67,13 +67,22 @@ def get_new_request(namespace, name, endpoint=None):
|
||||
|
||||
|
||||
async def assert_request_calls_service(
|
||||
namespace, name, endpoint, service, hass, response_type="Response", payload=None
|
||||
namespace,
|
||||
name,
|
||||
endpoint,
|
||||
service,
|
||||
hass,
|
||||
response_type="Response",
|
||||
payload=None,
|
||||
instance=None,
|
||||
):
|
||||
"""Assert an API request calls a hass service."""
|
||||
context = Context()
|
||||
request = get_new_request(namespace, name, endpoint)
|
||||
if payload:
|
||||
request["directive"]["payload"] = payload
|
||||
if instance:
|
||||
request["directive"]["header"]["instance"] = instance
|
||||
|
||||
domain, service_name = service.split(".")
|
||||
calls = async_mock_service(hass, domain, service_name)
|
||||
|
||||
@@ -305,7 +305,7 @@ async def test_report_colored_temp_light_state(hass):
|
||||
|
||||
|
||||
async def test_report_fan_speed_state(hass):
|
||||
"""Test PercentageController reports fan speed correctly."""
|
||||
"""Test PercentageController, PowerLevelController, RangeController reports fan speed correctly."""
|
||||
hass.states.async_set(
|
||||
"fan.off",
|
||||
"off",
|
||||
@@ -333,15 +333,82 @@ async def test_report_fan_speed_state(hass):
|
||||
|
||||
properties = await reported_properties(hass, "fan.off")
|
||||
properties.assert_equal("Alexa.PercentageController", "percentage", 0)
|
||||
properties.assert_equal("Alexa.PowerLevelController", "powerLevel", 0)
|
||||
properties.assert_equal("Alexa.RangeController", "rangeValue", 0)
|
||||
|
||||
properties = await reported_properties(hass, "fan.low_speed")
|
||||
properties.assert_equal("Alexa.PercentageController", "percentage", 33)
|
||||
properties.assert_equal("Alexa.PowerLevelController", "powerLevel", 33)
|
||||
properties.assert_equal("Alexa.RangeController", "rangeValue", 1)
|
||||
|
||||
properties = await reported_properties(hass, "fan.medium_speed")
|
||||
properties.assert_equal("Alexa.PercentageController", "percentage", 66)
|
||||
properties.assert_equal("Alexa.PowerLevelController", "powerLevel", 66)
|
||||
properties.assert_equal("Alexa.RangeController", "rangeValue", 2)
|
||||
|
||||
properties = await reported_properties(hass, "fan.high_speed")
|
||||
properties.assert_equal("Alexa.PercentageController", "percentage", 100)
|
||||
properties.assert_equal("Alexa.PowerLevelController", "powerLevel", 100)
|
||||
properties.assert_equal("Alexa.RangeController", "rangeValue", 3)
|
||||
|
||||
|
||||
async def test_report_fan_oscillating(hass):
|
||||
"""Test ToggleController reports fan oscillating correctly."""
|
||||
hass.states.async_set(
|
||||
"fan.off",
|
||||
"off",
|
||||
{"friendly_name": "Off fan", "speed": "off", "supported_features": 3},
|
||||
)
|
||||
hass.states.async_set(
|
||||
"fan.low_speed",
|
||||
"on",
|
||||
{
|
||||
"friendly_name": "Low speed fan",
|
||||
"speed": "low",
|
||||
"oscillating": True,
|
||||
"supported_features": 3,
|
||||
},
|
||||
)
|
||||
|
||||
properties = await reported_properties(hass, "fan.off")
|
||||
properties.assert_equal("Alexa.ToggleController", "toggleState", "OFF")
|
||||
|
||||
properties = await reported_properties(hass, "fan.low_speed")
|
||||
properties.assert_equal("Alexa.ToggleController", "toggleState", "ON")
|
||||
|
||||
|
||||
async def test_report_fan_direction(hass):
|
||||
"""Test ModeController reports fan direction correctly."""
|
||||
hass.states.async_set(
|
||||
"fan.off", "off", {"friendly_name": "Off fan", "supported_features": 4}
|
||||
)
|
||||
hass.states.async_set(
|
||||
"fan.reverse",
|
||||
"on",
|
||||
{
|
||||
"friendly_name": "Fan Reverse",
|
||||
"direction": "reverse",
|
||||
"supported_features": 4,
|
||||
},
|
||||
)
|
||||
hass.states.async_set(
|
||||
"fan.forward",
|
||||
"on",
|
||||
{
|
||||
"friendly_name": "Fan Forward",
|
||||
"direction": "forward",
|
||||
"supported_features": 4,
|
||||
},
|
||||
)
|
||||
|
||||
properties = await reported_properties(hass, "fan.off")
|
||||
properties.assert_not_has_property("Alexa.ModeController", "mode")
|
||||
|
||||
properties = await reported_properties(hass, "fan.reverse")
|
||||
properties.assert_equal("Alexa.ModeController", "mode", "reverse")
|
||||
|
||||
properties = await reported_properties(hass, "fan.forward")
|
||||
properties.assert_equal("Alexa.ModeController", "mode", "forward")
|
||||
|
||||
|
||||
async def test_report_cover_percentage_state(hass):
|
||||
|
||||
@@ -310,10 +310,14 @@ async def test_fan(hass):
|
||||
assert appliance["endpointId"] == "fan#test_1"
|
||||
assert appliance["displayCategories"][0] == "FAN"
|
||||
assert appliance["friendlyName"] == "Test fan 1"
|
||||
assert_endpoint_capabilities(
|
||||
capabilities = assert_endpoint_capabilities(
|
||||
appliance, "Alexa.PowerController", "Alexa.EndpointHealth"
|
||||
)
|
||||
|
||||
power_capability = get_capability(capabilities, "Alexa.PowerController")
|
||||
assert "capabilityResources" not in power_capability
|
||||
assert "configuration" not in power_capability
|
||||
|
||||
|
||||
async def test_variable_fan(hass):
|
||||
"""Test fan discovery.
|
||||
@@ -336,14 +340,33 @@ async def test_variable_fan(hass):
|
||||
assert appliance["displayCategories"][0] == "FAN"
|
||||
assert appliance["friendlyName"] == "Test fan 2"
|
||||
|
||||
assert_endpoint_capabilities(
|
||||
capabilities = assert_endpoint_capabilities(
|
||||
appliance,
|
||||
"Alexa.PercentageController",
|
||||
"Alexa.PowerController",
|
||||
"Alexa.PowerLevelController",
|
||||
"Alexa.RangeController",
|
||||
"Alexa.EndpointHealth",
|
||||
)
|
||||
|
||||
range_capability = get_capability(capabilities, "Alexa.RangeController")
|
||||
assert range_capability is not None
|
||||
assert range_capability["instance"] == "fan.speed"
|
||||
|
||||
properties = range_capability["properties"]
|
||||
assert properties["nonControllable"] is False
|
||||
assert {"name": "rangeValue"} in properties["supported"]
|
||||
|
||||
capability_resources = range_capability["capabilityResources"]
|
||||
assert capability_resources is not None
|
||||
assert {
|
||||
"@type": "asset",
|
||||
"value": {"assetId": "Alexa.Setting.FanSpeed"},
|
||||
} in capability_resources["friendlyNames"]
|
||||
|
||||
configuration = range_capability["configuration"]
|
||||
assert configuration is not None
|
||||
|
||||
call, _ = await assert_request_calls_service(
|
||||
"Alexa.PercentageController",
|
||||
"SetPercentage",
|
||||
@@ -377,7 +400,7 @@ async def test_variable_fan(hass):
|
||||
|
||||
await assert_percentage_changes(
|
||||
hass,
|
||||
[("high", "-5"), ("high", "5"), ("low", "-80")],
|
||||
[("high", "-5"), ("medium", "-50"), ("low", "-80")],
|
||||
"Alexa.PowerLevelController",
|
||||
"AdjustPowerLevel",
|
||||
"fan#test_2",
|
||||
@@ -387,6 +410,251 @@ async def test_variable_fan(hass):
|
||||
)
|
||||
|
||||
|
||||
async def test_oscillating_fan(hass):
|
||||
"""Test oscillating fan discovery."""
|
||||
device = (
|
||||
"fan.test_3",
|
||||
"off",
|
||||
{"friendly_name": "Test fan 3", "supported_features": 3},
|
||||
)
|
||||
appliance = await discovery_test(device, hass)
|
||||
|
||||
assert appliance["endpointId"] == "fan#test_3"
|
||||
assert appliance["displayCategories"][0] == "FAN"
|
||||
assert appliance["friendlyName"] == "Test fan 3"
|
||||
capabilities = assert_endpoint_capabilities(
|
||||
appliance,
|
||||
"Alexa.PercentageController",
|
||||
"Alexa.PowerController",
|
||||
"Alexa.PowerLevelController",
|
||||
"Alexa.RangeController",
|
||||
"Alexa.ToggleController",
|
||||
"Alexa.EndpointHealth",
|
||||
)
|
||||
|
||||
toggle_capability = get_capability(capabilities, "Alexa.ToggleController")
|
||||
assert toggle_capability is not None
|
||||
assert toggle_capability["instance"] == "fan.oscillating"
|
||||
|
||||
properties = toggle_capability["properties"]
|
||||
assert properties["nonControllable"] is False
|
||||
assert {"name": "toggleState"} in properties["supported"]
|
||||
|
||||
capability_resources = toggle_capability["capabilityResources"]
|
||||
assert capability_resources is not None
|
||||
assert {
|
||||
"@type": "asset",
|
||||
"value": {"assetId": "Alexa.Setting.Oscillate"},
|
||||
} in capability_resources["friendlyNames"]
|
||||
|
||||
call, _ = await assert_request_calls_service(
|
||||
"Alexa.ToggleController",
|
||||
"TurnOn",
|
||||
"fan#test_3",
|
||||
"fan.oscillate",
|
||||
hass,
|
||||
payload={},
|
||||
instance="fan.oscillating",
|
||||
)
|
||||
assert call.data["oscillating"]
|
||||
|
||||
call, _ = await assert_request_calls_service(
|
||||
"Alexa.ToggleController",
|
||||
"TurnOff",
|
||||
"fan#test_3",
|
||||
"fan.oscillate",
|
||||
hass,
|
||||
payload={},
|
||||
instance="fan.oscillating",
|
||||
)
|
||||
assert not call.data["oscillating"]
|
||||
|
||||
|
||||
async def test_direction_fan(hass):
|
||||
"""Test direction fan discovery."""
|
||||
device = (
|
||||
"fan.test_4",
|
||||
"on",
|
||||
{
|
||||
"friendly_name": "Test fan 4",
|
||||
"supported_features": 5,
|
||||
"direction": "forward",
|
||||
},
|
||||
)
|
||||
appliance = await discovery_test(device, hass)
|
||||
|
||||
assert appliance["endpointId"] == "fan#test_4"
|
||||
assert appliance["displayCategories"][0] == "FAN"
|
||||
assert appliance["friendlyName"] == "Test fan 4"
|
||||
capabilities = assert_endpoint_capabilities(
|
||||
appliance,
|
||||
"Alexa.PercentageController",
|
||||
"Alexa.PowerController",
|
||||
"Alexa.PowerLevelController",
|
||||
"Alexa.RangeController",
|
||||
"Alexa.ModeController",
|
||||
"Alexa.EndpointHealth",
|
||||
)
|
||||
|
||||
mode_capability = get_capability(capabilities, "Alexa.ModeController")
|
||||
assert mode_capability is not None
|
||||
assert mode_capability["instance"] == "fan.direction"
|
||||
|
||||
properties = mode_capability["properties"]
|
||||
assert properties["nonControllable"] is False
|
||||
assert {"name": "mode"} in properties["supported"]
|
||||
|
||||
capability_resources = mode_capability["capabilityResources"]
|
||||
assert capability_resources is not None
|
||||
assert {
|
||||
"@type": "asset",
|
||||
"value": {"assetId": "Alexa.Setting.Direction"},
|
||||
} in capability_resources["friendlyNames"]
|
||||
|
||||
configuration = mode_capability["configuration"]
|
||||
assert configuration is not None
|
||||
assert configuration["ordered"] is False
|
||||
|
||||
supported_modes = configuration["supportedModes"]
|
||||
assert supported_modes is not None
|
||||
assert {
|
||||
"value": "direction.forward",
|
||||
"modeResources": {
|
||||
"friendlyNames": [
|
||||
{"@type": "text", "value": {"text": "forward", "locale": "en-US"}}
|
||||
]
|
||||
},
|
||||
} in supported_modes
|
||||
assert {
|
||||
"value": "direction.reverse",
|
||||
"modeResources": {
|
||||
"friendlyNames": [
|
||||
{"@type": "text", "value": {"text": "reverse", "locale": "en-US"}}
|
||||
]
|
||||
},
|
||||
} in supported_modes
|
||||
|
||||
call, _ = await assert_request_calls_service(
|
||||
"Alexa.ModeController",
|
||||
"SetMode",
|
||||
"fan#test_4",
|
||||
"fan.set_direction",
|
||||
hass,
|
||||
payload={"mode": "direction.reverse"},
|
||||
instance="fan.direction",
|
||||
)
|
||||
assert call.data["direction"] == "reverse"
|
||||
|
||||
# Test for AdjustMode instance=None Error coverage
|
||||
with pytest.raises(AssertionError):
|
||||
call, _ = await assert_request_calls_service(
|
||||
"Alexa.ModeController",
|
||||
"AdjustMode",
|
||||
"fan#test_4",
|
||||
"fan.set_direction",
|
||||
hass,
|
||||
payload={},
|
||||
instance=None,
|
||||
)
|
||||
assert call.data
|
||||
|
||||
|
||||
async def test_fan_range(hass):
|
||||
"""Test fan discovery with range controller.
|
||||
|
||||
This one has variable speed.
|
||||
"""
|
||||
device = (
|
||||
"fan.test_5",
|
||||
"off",
|
||||
{
|
||||
"friendly_name": "Test fan 5",
|
||||
"supported_features": 1,
|
||||
"speed_list": ["low", "medium", "high"],
|
||||
"speed": "medium",
|
||||
},
|
||||
)
|
||||
appliance = await discovery_test(device, hass)
|
||||
|
||||
assert appliance["endpointId"] == "fan#test_5"
|
||||
assert appliance["displayCategories"][0] == "FAN"
|
||||
assert appliance["friendlyName"] == "Test fan 5"
|
||||
|
||||
capabilities = assert_endpoint_capabilities(
|
||||
appliance,
|
||||
"Alexa.PercentageController",
|
||||
"Alexa.PowerController",
|
||||
"Alexa.PowerLevelController",
|
||||
"Alexa.RangeController",
|
||||
"Alexa.EndpointHealth",
|
||||
)
|
||||
|
||||
range_capability = get_capability(capabilities, "Alexa.RangeController")
|
||||
assert range_capability is not None
|
||||
assert range_capability["instance"] == "fan.speed"
|
||||
|
||||
call, _ = await assert_request_calls_service(
|
||||
"Alexa.RangeController",
|
||||
"SetRangeValue",
|
||||
"fan#test_5",
|
||||
"fan.set_speed",
|
||||
hass,
|
||||
payload={"rangeValue": "1"},
|
||||
instance="fan.speed",
|
||||
)
|
||||
assert call.data["speed"] == "low"
|
||||
|
||||
await assert_range_changes(
|
||||
hass,
|
||||
[("low", "-1"), ("high", "1"), ("medium", "0")],
|
||||
"Alexa.RangeController",
|
||||
"AdjustRangeValue",
|
||||
"fan#test_5",
|
||||
False,
|
||||
"fan.set_speed",
|
||||
"speed",
|
||||
instance="fan.speed",
|
||||
)
|
||||
|
||||
|
||||
async def test_fan_range_off(hass):
|
||||
"""Test fan range controller 0 turns_off fan."""
|
||||
device = (
|
||||
"fan.test_6",
|
||||
"off",
|
||||
{
|
||||
"friendly_name": "Test fan 6",
|
||||
"supported_features": 1,
|
||||
"speed_list": ["low", "medium", "high"],
|
||||
"speed": "high",
|
||||
},
|
||||
)
|
||||
await discovery_test(device, hass)
|
||||
|
||||
call, _ = await assert_request_calls_service(
|
||||
"Alexa.RangeController",
|
||||
"SetRangeValue",
|
||||
"fan#test_6",
|
||||
"fan.turn_off",
|
||||
hass,
|
||||
payload={"rangeValue": "0"},
|
||||
instance="fan.speed",
|
||||
)
|
||||
assert call.data["speed"] == "off"
|
||||
|
||||
await assert_range_changes(
|
||||
hass,
|
||||
[("off", "-3")],
|
||||
"Alexa.RangeController",
|
||||
"AdjustRangeValue",
|
||||
"fan#test_6",
|
||||
False,
|
||||
"fan.turn_off",
|
||||
"speed",
|
||||
instance="fan.speed",
|
||||
)
|
||||
|
||||
|
||||
async def test_lock(hass):
|
||||
"""Test lock discovery."""
|
||||
device = ("lock.test", "off", {"friendly_name": "Test lock"})
|
||||
@@ -729,6 +997,33 @@ async def assert_percentage_changes(
|
||||
assert call.data[changed_parameter] == result_volume
|
||||
|
||||
|
||||
async def assert_range_changes(
|
||||
hass,
|
||||
adjustments,
|
||||
namespace,
|
||||
name,
|
||||
endpoint,
|
||||
delta_default,
|
||||
service,
|
||||
changed_parameter,
|
||||
instance,
|
||||
):
|
||||
"""Assert an API request making range changes works.
|
||||
|
||||
AdjustRangeValue are examples of such requests.
|
||||
"""
|
||||
for result_range, adjustment in adjustments:
|
||||
payload = {
|
||||
"rangeValueDelta": adjustment,
|
||||
"rangeValueDeltaDefault": delta_default,
|
||||
}
|
||||
|
||||
call, _ = await assert_request_calls_service(
|
||||
namespace, name, endpoint, service, hass, payload=payload, instance=instance
|
||||
)
|
||||
assert call.data[changed_parameter] == result_range
|
||||
|
||||
|
||||
async def test_temp_sensor(hass):
|
||||
"""Test temperature sensor discovery."""
|
||||
device = (
|
||||
@@ -1438,3 +1733,41 @@ async def test_alarm_control_panel_code_arm_required(hass):
|
||||
{"friendly_name": "Test Alarm Control Panel 3", "code_arm_required": True},
|
||||
)
|
||||
await discovery_test(device, hass, expected_endpoints=0)
|
||||
|
||||
|
||||
async def test_range_unsupported_domain(hass):
|
||||
"""Test rangeController with unsupported domain."""
|
||||
device = ("switch.test", "on", {"friendly_name": "Test switch"})
|
||||
await discovery_test(device, hass)
|
||||
|
||||
context = Context()
|
||||
request = get_new_request("Alexa.RangeController", "SetRangeValue", "switch#test")
|
||||
request["directive"]["payload"] = {"rangeValue": "1"}
|
||||
request["directive"]["header"]["instance"] = "switch.speed"
|
||||
|
||||
msg = await smart_home.async_handle_message(hass, DEFAULT_CONFIG, request, context)
|
||||
|
||||
assert "event" in msg
|
||||
msg = msg["event"]
|
||||
assert msg["header"]["name"] == "ErrorResponse"
|
||||
assert msg["header"]["namespace"] == "Alexa"
|
||||
assert msg["payload"]["type"] == "INVALID_DIRECTIVE"
|
||||
|
||||
|
||||
async def test_mode_unsupported_domain(hass):
|
||||
"""Test modeController with unsupported domain."""
|
||||
device = ("switch.test", "on", {"friendly_name": "Test switch"})
|
||||
await discovery_test(device, hass)
|
||||
|
||||
context = Context()
|
||||
request = get_new_request("Alexa.ModeController", "SetMode", "switch#test")
|
||||
request["directive"]["payload"] = {"mode": "testMode"}
|
||||
request["directive"]["header"]["instance"] = "switch.direction"
|
||||
|
||||
msg = await smart_home.async_handle_message(hass, DEFAULT_CONFIG, request, context)
|
||||
|
||||
assert "event" in msg
|
||||
msg = msg["event"]
|
||||
assert msg["header"]["name"] == "ErrorResponse"
|
||||
assert msg["header"]["namespace"] == "Alexa"
|
||||
assert msg["payload"]["type"] == "INVALID_DIRECTIVE"
|
||||
@@ -37,6 +37,54 @@ async def test_report_state(hass, aioclient_mock):
|
||||
assert call_json["event"]["endpoint"]["endpointId"] == "binary_sensor#test_contact"
|
||||
|
||||
|
||||
async def test_report_state_instance(hass, aioclient_mock):
|
||||
"""Test proactive state reports with instance."""
|
||||
aioclient_mock.post(TEST_URL, text="", status=202)
|
||||
|
||||
hass.states.async_set(
|
||||
"fan.test_fan",
|
||||
"off",
|
||||
{
|
||||
"friendly_name": "Test fan",
|
||||
"supported_features": 3,
|
||||
"speed": "off",
|
||||
"oscillating": False,
|
||||
},
|
||||
)
|
||||
|
||||
await state_report.async_enable_proactive_mode(hass, DEFAULT_CONFIG)
|
||||
|
||||
hass.states.async_set(
|
||||
"fan.test_fan",
|
||||
"on",
|
||||
{
|
||||
"friendly_name": "Test fan",
|
||||
"supported_features": 3,
|
||||
"speed": "high",
|
||||
"oscillating": True,
|
||||
},
|
||||
)
|
||||
|
||||
# To trigger event listener
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 1
|
||||
call = aioclient_mock.mock_calls
|
||||
|
||||
call_json = call[0][2]
|
||||
assert call_json["event"]["header"]["namespace"] == "Alexa"
|
||||
assert call_json["event"]["header"]["name"] == "ChangeReport"
|
||||
|
||||
change_reports = call_json["event"]["payload"]["change"]["properties"]
|
||||
for report in change_reports:
|
||||
if report["name"] == "toggleState":
|
||||
assert report["value"] == "ON"
|
||||
assert report["instance"] == "fan.oscillating"
|
||||
assert report["namespace"] == "Alexa.ToggleController"
|
||||
|
||||
assert call_json["event"]["endpoint"]["endpointId"] == "fan#test_fan"
|
||||
|
||||
|
||||
async def test_send_add_or_update_message(hass, aioclient_mock):
|
||||
"""Test sending an AddOrUpdateReport message."""
|
||||
aioclient_mock.post(TEST_URL, text="")
|
||||
|
||||
Reference in new issue
Block a user