Add support for rangeValueDeltaDefault in Alexa AdjustRangeValue directive (#31258)

* Update tests with rangeValue and rangeValueDelta to use int.

* Add support for rangeValueDeltaDefault for covers.

* Update tests for range changes.

* Test for AdjustRangeValue with rangeValueDeltaDefault True.

* Update tilt error.
This commit is contained in:
ochlocracy 2020-01-29 12:04:57 -05:00 committed by GitHub
parent 7116c7404a
commit 83dff16e1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 41 deletions

View File

@ -1195,6 +1195,7 @@ async def async_api_adjust_range(hass, config, directive, context):
service = None service = None
data = {ATTR_ENTITY_ID: entity.entity_id} data = {ATTR_ENTITY_ID: entity.entity_id}
range_delta = directive.payload["rangeValueDelta"] range_delta = directive.payload["rangeValueDelta"]
range_delta_default = bool(directive.payload["rangeValueDeltaDefault"])
response_value = 0 response_value = 0
# Fan Speed # Fan Speed
@ -1220,9 +1221,12 @@ async def async_api_adjust_range(hass, config, directive, context):
# Cover Position # Cover Position
elif instance == f"{cover.DOMAIN}.{cover.ATTR_POSITION}": elif instance == f"{cover.DOMAIN}.{cover.ATTR_POSITION}":
range_delta = int(range_delta) range_delta = int(range_delta * 20) if range_delta_default else int(range_delta)
service = SERVICE_SET_COVER_POSITION service = SERVICE_SET_COVER_POSITION
current = entity.attributes.get(cover.ATTR_POSITION) current = entity.attributes.get(cover.ATTR_POSITION)
if not current:
msg = "Unable to determine {} current position".format(entity.entity_id)
raise AlexaInvalidValueError(msg)
position = response_value = min(100, max(0, range_delta + current)) position = response_value = min(100, max(0, range_delta + current))
if position == 100: if position == 100:
service = cover.SERVICE_OPEN_COVER service = cover.SERVICE_OPEN_COVER
@ -1233,9 +1237,14 @@ async def async_api_adjust_range(hass, config, directive, context):
# Cover Tilt # Cover Tilt
elif instance == f"{cover.DOMAIN}.tilt": elif instance == f"{cover.DOMAIN}.tilt":
range_delta = int(range_delta) range_delta = int(range_delta * 20) if range_delta_default else int(range_delta)
service = SERVICE_SET_COVER_TILT_POSITION service = SERVICE_SET_COVER_TILT_POSITION
current = entity.attributes.get(cover.ATTR_TILT_POSITION) current = entity.attributes.get(cover.ATTR_TILT_POSITION)
if not current:
msg = "Unable to determine {} current tilt position".format(
entity.entity_id
)
raise AlexaInvalidValueError(msg)
tilt_position = response_value = min(100, max(0, range_delta + current)) tilt_position = response_value = min(100, max(0, range_delta + current))
if tilt_position == 100: if tilt_position == 100:
service = cover.SERVICE_OPEN_COVER_TILT service = cover.SERVICE_OPEN_COVER_TILT

View File

@ -753,7 +753,7 @@ async def test_fan_range(hass):
"fan#test_5", "fan#test_5",
"fan.set_speed", "fan.set_speed",
hass, hass,
payload={"rangeValue": "1"}, payload={"rangeValue": 1},
instance="fan.speed", instance="fan.speed",
) )
assert call.data["speed"] == "low" assert call.data["speed"] == "low"
@ -764,18 +764,22 @@ async def test_fan_range(hass):
"fan#test_5", "fan#test_5",
"fan.set_speed", "fan.set_speed",
hass, hass,
payload={"rangeValue": "5"}, payload={"rangeValue": 5},
instance="fan.speed", instance="fan.speed",
) )
assert call.data["speed"] == "warp_speed" assert call.data["speed"] == "warp_speed"
await assert_range_changes( await assert_range_changes(
hass, hass,
[("low", "-1"), ("high", "1"), ("medium", "0"), ("warp_speed", "99")], [
("low", -1, False),
("high", 1, False),
("medium", 0, False),
("warp_speed", 99, False),
],
"Alexa.RangeController", "Alexa.RangeController",
"AdjustRangeValue", "AdjustRangeValue",
"fan#test_5", "fan#test_5",
False,
"fan.set_speed", "fan.set_speed",
"speed", "speed",
instance="fan.speed", instance="fan.speed",
@ -802,18 +806,17 @@ async def test_fan_range_off(hass):
"fan#test_6", "fan#test_6",
"fan.turn_off", "fan.turn_off",
hass, hass,
payload={"rangeValue": "0"}, payload={"rangeValue": 0},
instance="fan.speed", instance="fan.speed",
) )
assert call.data["speed"] == "off" assert call.data["speed"] == "off"
await assert_range_changes( await assert_range_changes(
hass, hass,
[("off", "-3"), ("off", "-99")], [("off", -3, False), ("off", -99, False)],
"Alexa.RangeController", "Alexa.RangeController",
"AdjustRangeValue", "AdjustRangeValue",
"fan#test_6", "fan#test_6",
False,
"fan.turn_off", "fan.turn_off",
"speed", "speed",
instance="fan.speed", instance="fan.speed",
@ -1524,7 +1527,7 @@ async def test_cover_position_range(hass):
"cover#test_range", "cover#test_range",
"cover.set_cover_position", "cover.set_cover_position",
hass, hass,
payload={"rangeValue": "50"}, payload={"rangeValue": 50},
instance="cover.position", instance="cover.position",
) )
assert call.data["position"] == 50 assert call.data["position"] == 50
@ -1535,7 +1538,7 @@ async def test_cover_position_range(hass):
"cover#test_range", "cover#test_range",
"cover.close_cover", "cover.close_cover",
hass, hass,
payload={"rangeValue": "0"}, payload={"rangeValue": 0},
instance="cover.position", instance="cover.position",
) )
properties = msg["context"]["properties"][0] properties = msg["context"]["properties"][0]
@ -1549,7 +1552,7 @@ async def test_cover_position_range(hass):
"cover#test_range", "cover#test_range",
"cover.open_cover", "cover.open_cover",
hass, hass,
payload={"rangeValue": "100"}, payload={"rangeValue": 100},
instance="cover.position", instance="cover.position",
) )
properties = msg["context"]["properties"][0] properties = msg["context"]["properties"][0]
@ -1563,7 +1566,7 @@ async def test_cover_position_range(hass):
"cover#test_range", "cover#test_range",
"cover.open_cover", "cover.open_cover",
hass, hass,
payload={"rangeValueDelta": "99"}, payload={"rangeValueDelta": 99, "rangeValueDeltaDefault": False},
instance="cover.position", instance="cover.position",
) )
properties = msg["context"]["properties"][0] properties = msg["context"]["properties"][0]
@ -1577,7 +1580,7 @@ async def test_cover_position_range(hass):
"cover#test_range", "cover#test_range",
"cover.close_cover", "cover.close_cover",
hass, hass,
payload={"rangeValueDelta": "-99"}, payload={"rangeValueDelta": -99, "rangeValueDeltaDefault": False},
instance="cover.position", instance="cover.position",
) )
properties = msg["context"]["properties"][0] properties = msg["context"]["properties"][0]
@ -1587,11 +1590,10 @@ async def test_cover_position_range(hass):
await assert_range_changes( await assert_range_changes(
hass, hass,
[(25, "-5"), (35, "5")], [(25, -5, False), (35, 5, False), (50, 1, True), (10, -1, True)],
"Alexa.RangeController", "Alexa.RangeController",
"AdjustRangeValue", "AdjustRangeValue",
"cover#test_range", "cover#test_range",
False,
"cover.set_cover_position", "cover.set_cover_position",
"position", "position",
instance="cover.position", instance="cover.position",
@ -1618,21 +1620,13 @@ async def assert_percentage_changes(
async def assert_range_changes( async def assert_range_changes(
hass, hass, adjustments, namespace, name, endpoint, service, changed_parameter, instance
adjustments,
namespace,
name,
endpoint,
delta_default,
service,
changed_parameter,
instance,
): ):
"""Assert an API request making range changes works. """Assert an API request making range changes works.
AdjustRangeValue are examples of such requests. AdjustRangeValue are examples of such requests.
""" """
for result_range, adjustment in adjustments: for result_range, adjustment, delta_default in adjustments:
payload = { payload = {
"rangeValueDelta": adjustment, "rangeValueDelta": adjustment,
"rangeValueDeltaDefault": delta_default, "rangeValueDeltaDefault": delta_default,
@ -2488,7 +2482,7 @@ async def test_range_unsupported_domain(hass):
context = Context() context = Context()
request = get_new_request("Alexa.RangeController", "SetRangeValue", "switch#test") request = get_new_request("Alexa.RangeController", "SetRangeValue", "switch#test")
request["directive"]["payload"] = {"rangeValue": "1"} request["directive"]["payload"] = {"rangeValue": 1}
request["directive"]["header"]["instance"] = "switch.speed" request["directive"]["header"]["instance"] = "switch.speed"
msg = await smart_home.async_handle_message(hass, DEFAULT_CONFIG, request, context) msg = await smart_home.async_handle_message(hass, DEFAULT_CONFIG, request, context)
@ -2855,7 +2849,7 @@ async def test_cover_tilt_position_range(hass):
"cover#test_tilt_range", "cover#test_tilt_range",
"cover.open_cover_tilt", "cover.open_cover_tilt",
hass, hass,
payload={"rangeValueDelta": 99}, payload={"rangeValueDelta": 99, "rangeValueDeltaDefault": False},
instance="cover.tilt", instance="cover.tilt",
) )
properties = msg["context"]["properties"][0] properties = msg["context"]["properties"][0]
@ -2869,7 +2863,7 @@ async def test_cover_tilt_position_range(hass):
"cover#test_tilt_range", "cover#test_tilt_range",
"cover.close_cover_tilt", "cover.close_cover_tilt",
hass, hass,
payload={"rangeValueDelta": -99}, payload={"rangeValueDelta": -99, "rangeValueDeltaDefault": False},
instance="cover.tilt", instance="cover.tilt",
) )
properties = msg["context"]["properties"][0] properties = msg["context"]["properties"][0]
@ -2879,11 +2873,10 @@ async def test_cover_tilt_position_range(hass):
await assert_range_changes( await assert_range_changes(
hass, hass,
[(25, "-5"), (35, "5")], [(25, -5, False), (35, 5, False), (50, 1, True), (10, -1, True)],
"Alexa.RangeController", "Alexa.RangeController",
"AdjustRangeValue", "AdjustRangeValue",
"cover#test_tilt_range", "cover#test_tilt_range",
False,
"cover.set_cover_tilt_position", "cover.set_cover_tilt_position",
"tilt_position", "tilt_position",
instance="cover.tilt", instance="cover.tilt",
@ -3038,18 +3031,17 @@ async def test_input_number(hass):
"input_number#test_slider", "input_number#test_slider",
"input_number.set_value", "input_number.set_value",
hass, hass,
payload={"rangeValue": "10"}, payload={"rangeValue": 10},
instance="input_number.value", instance="input_number.value",
) )
assert call.data["value"] == 10 assert call.data["value"] == 10
await assert_range_changes( await assert_range_changes(
hass, hass,
[(25, "-5"), (35, "5"), (-20, "-100"), (35, "100")], [(25, -5, False), (35, 5, False), (-20, -100, False), (35, 100, False)],
"Alexa.RangeController", "Alexa.RangeController",
"AdjustRangeValue", "AdjustRangeValue",
"input_number#test_slider", "input_number#test_slider",
False,
"input_number.set_value", "input_number.set_value",
"value", "value",
instance="input_number.value", instance="input_number.value",
@ -3124,18 +3116,23 @@ async def test_input_number_float(hass):
"input_number#test_slider_float", "input_number#test_slider_float",
"input_number.set_value", "input_number.set_value",
hass, hass,
payload={"rangeValue": "0.333"}, payload={"rangeValue": 0.333},
instance="input_number.value", instance="input_number.value",
) )
assert call.data["value"] == 0.333 assert call.data["value"] == 0.333
await assert_range_changes( await assert_range_changes(
hass, hass,
[(0.4, "-0.1"), (0.6, "0.1"), (0, "-100"), (1, "100"), (0.51, "0.01")], [
(0.4, -0.1, False),
(0.6, 0.1, False),
(0, -100, False),
(1, 100, False),
(0.51, 0.01, False),
],
"Alexa.RangeController", "Alexa.RangeController",
"AdjustRangeValue", "AdjustRangeValue",
"input_number#test_slider_float", "input_number#test_slider_float",
False,
"input_number.set_value", "input_number.set_value",
"value", "value",
instance="input_number.value", instance="input_number.value",
@ -3432,7 +3429,7 @@ async def test_vacuum_fan_speed(hass):
"vacuum#test_2", "vacuum#test_2",
"vacuum.set_fan_speed", "vacuum.set_fan_speed",
hass, hass,
payload={"rangeValue": "1"}, payload={"rangeValue": 1},
instance="vacuum.fan_speed", instance="vacuum.fan_speed",
) )
assert call.data["fan_speed"] == "low" assert call.data["fan_speed"] == "low"
@ -3443,18 +3440,22 @@ async def test_vacuum_fan_speed(hass):
"vacuum#test_2", "vacuum#test_2",
"vacuum.set_fan_speed", "vacuum.set_fan_speed",
hass, hass,
payload={"rangeValue": "5"}, payload={"rangeValue": 5},
instance="vacuum.fan_speed", instance="vacuum.fan_speed",
) )
assert call.data["fan_speed"] == "super_sucker" assert call.data["fan_speed"] == "super_sucker"
await assert_range_changes( await assert_range_changes(
hass, hass,
[("low", "-1"), ("high", "1"), ("medium", "0"), ("super_sucker", "99")], [
("low", -1, False),
("high", 1, False),
("medium", 0, False),
("super_sucker", 99, False),
],
"Alexa.RangeController", "Alexa.RangeController",
"AdjustRangeValue", "AdjustRangeValue",
"vacuum#test_2", "vacuum#test_2",
False,
"vacuum.set_fan_speed", "vacuum.set_fan_speed",
"fan_speed", "fan_speed",
instance="vacuum.fan_speed", instance="vacuum.fan_speed",