Add xy support to Alexa HomeAPI v3 (#10268)
* Add xy support to Alexa HomeAPI v3 * Update smart_home.py * Update smart_home.py * fix lint * fix copy paste * Update smart_home.py * simplify * Add test for xy/rgb * Update test_smart_home.py * Update smart_home.py * add testpull/10279/head
parent
e2c6f538a8
commit
619d329a16
|
@ -26,6 +26,7 @@ MAPPING_COMPONENT = {
|
||||||
'LIGHT', ('Alexa.PowerController',), {
|
'LIGHT', ('Alexa.PowerController',), {
|
||||||
light.SUPPORT_BRIGHTNESS: 'Alexa.BrightnessController',
|
light.SUPPORT_BRIGHTNESS: 'Alexa.BrightnessController',
|
||||||
light.SUPPORT_RGB_COLOR: 'Alexa.ColorController',
|
light.SUPPORT_RGB_COLOR: 'Alexa.ColorController',
|
||||||
|
light.SUPPORT_XY_COLOR: 'Alexa.ColorController',
|
||||||
light.SUPPORT_COLOR_TEMP: 'Alexa.ColorTemperatureController',
|
light.SUPPORT_COLOR_TEMP: 'Alexa.ColorTemperatureController',
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
@ -219,10 +220,10 @@ def async_api_adjust_brightness(hass, request, entity):
|
||||||
current = math.floor(
|
current = math.floor(
|
||||||
int(entity.attributes.get(light.ATTR_BRIGHTNESS)) / 255 * 100)
|
int(entity.attributes.get(light.ATTR_BRIGHTNESS)) / 255 * 100)
|
||||||
except ZeroDivisionError:
|
except ZeroDivisionError:
|
||||||
return api_error(request, error_type='INVALID_VALUE')
|
current = 0
|
||||||
|
|
||||||
# set brightness
|
# set brightness
|
||||||
brightness = brightness_delta + current
|
brightness = max(0, brightness_delta + current)
|
||||||
yield from hass.services.async_call(entity.domain, SERVICE_TURN_ON, {
|
yield from hass.services.async_call(entity.domain, SERVICE_TURN_ON, {
|
||||||
ATTR_ENTITY_ID: entity.entity_id,
|
ATTR_ENTITY_ID: entity.entity_id,
|
||||||
light.ATTR_BRIGHTNESS_PCT: brightness,
|
light.ATTR_BRIGHTNESS_PCT: brightness,
|
||||||
|
@ -236,15 +237,25 @@ def async_api_adjust_brightness(hass, request, entity):
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_api_set_color(hass, request, entity):
|
def async_api_set_color(hass, request, entity):
|
||||||
"""Process a set color request."""
|
"""Process a set color request."""
|
||||||
hue = float(request[API_PAYLOAD]['color']['hue'])
|
supported = entity.attributes.get(ATTR_SUPPORTED_FEATURES)
|
||||||
saturation = float(request[API_PAYLOAD]['color']['saturation'])
|
rgb = color_util.color_hsb_to_RGB(
|
||||||
brightness = float(request[API_PAYLOAD]['color']['brightness'])
|
float(request[API_PAYLOAD]['color']['hue']),
|
||||||
|
float(request[API_PAYLOAD]['color']['saturation']),
|
||||||
|
float(request[API_PAYLOAD]['color']['brightness'])
|
||||||
|
)
|
||||||
|
|
||||||
rgb = color_util.color_hsb_to_RGB(hue, saturation, brightness)
|
if supported & light.SUPPORT_RGB_COLOR > 0:
|
||||||
yield from hass.services.async_call(entity.domain, SERVICE_TURN_ON, {
|
yield from hass.services.async_call(entity.domain, SERVICE_TURN_ON, {
|
||||||
ATTR_ENTITY_ID: entity.entity_id,
|
ATTR_ENTITY_ID: entity.entity_id,
|
||||||
light.ATTR_RGB_COLOR: rgb,
|
light.ATTR_RGB_COLOR: rgb,
|
||||||
}, blocking=True)
|
}, blocking=True)
|
||||||
|
else:
|
||||||
|
xyz = color_util.color_RGB_to_xy(*rgb)
|
||||||
|
yield from hass.services.async_call(entity.domain, SERVICE_TURN_ON, {
|
||||||
|
ATTR_ENTITY_ID: entity.entity_id,
|
||||||
|
light.ATTR_XY_COLOR: (xyz[0], xyz[1]),
|
||||||
|
light.ATTR_BRIGHTNESS: xyz[2],
|
||||||
|
}, blocking=True)
|
||||||
|
|
||||||
return api_message(request)
|
return api_message(request)
|
||||||
|
|
||||||
|
|
|
@ -282,7 +282,8 @@ def test_api_set_brightness(hass):
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
@pytest.mark.parametrize("result,adjust", [(25, '-5'), (35, '5')])
|
@pytest.mark.parametrize(
|
||||||
|
"result,adjust", [(25, '-5'), (35, '5'), (0, '-80')])
|
||||||
def test_api_adjust_brightness(hass, result, adjust):
|
def test_api_adjust_brightness(hass, result, adjust):
|
||||||
"""Test api adjust brightness process."""
|
"""Test api adjust brightness process."""
|
||||||
request = get_new_request(
|
request = get_new_request(
|
||||||
|
@ -311,7 +312,7 @@ def test_api_adjust_brightness(hass, result, adjust):
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def test_api_set_color(hass):
|
def test_api_set_color_rgb(hass):
|
||||||
"""Test api set color process."""
|
"""Test api set color process."""
|
||||||
request = get_new_request(
|
request = get_new_request(
|
||||||
'Alexa.ColorController', 'SetColor', 'light#test')
|
'Alexa.ColorController', 'SetColor', 'light#test')
|
||||||
|
@ -325,7 +326,10 @@ def test_api_set_color(hass):
|
||||||
|
|
||||||
# settup test devices
|
# settup test devices
|
||||||
hass.states.async_set(
|
hass.states.async_set(
|
||||||
'light.test', 'off', {'friendly_name': "Test light"})
|
'light.test', 'off', {
|
||||||
|
'friendly_name': "Test light",
|
||||||
|
'supported_features': 16,
|
||||||
|
})
|
||||||
|
|
||||||
call_light = async_mock_service(hass, 'light', 'turn_on')
|
call_light = async_mock_service(hass, 'light', 'turn_on')
|
||||||
|
|
||||||
|
@ -340,6 +344,40 @@ def test_api_set_color(hass):
|
||||||
assert msg['header']['name'] == 'Response'
|
assert msg['header']['name'] == 'Response'
|
||||||
|
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def test_api_set_color_xy(hass):
|
||||||
|
"""Test api set color process."""
|
||||||
|
request = get_new_request(
|
||||||
|
'Alexa.ColorController', 'SetColor', 'light#test')
|
||||||
|
|
||||||
|
# add payload
|
||||||
|
request['directive']['payload']['color'] = {
|
||||||
|
'hue': '120',
|
||||||
|
'saturation': '0.612',
|
||||||
|
'brightness': '0.342',
|
||||||
|
}
|
||||||
|
|
||||||
|
# settup test devices
|
||||||
|
hass.states.async_set(
|
||||||
|
'light.test', 'off', {
|
||||||
|
'friendly_name': "Test light",
|
||||||
|
'supported_features': 64,
|
||||||
|
})
|
||||||
|
|
||||||
|
call_light = async_mock_service(hass, 'light', 'turn_on')
|
||||||
|
|
||||||
|
msg = yield from smart_home.async_handle_message(hass, request)
|
||||||
|
|
||||||
|
assert 'event' in msg
|
||||||
|
msg = msg['event']
|
||||||
|
|
||||||
|
assert len(call_light) == 1
|
||||||
|
assert call_light[0].data['entity_id'] == 'light.test'
|
||||||
|
assert call_light[0].data['xy_color'] == (0.23, 0.585)
|
||||||
|
assert call_light[0].data['brightness'] == 18
|
||||||
|
assert msg['header']['name'] == 'Response'
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def test_api_set_color_temperature(hass):
|
def test_api_set_color_temperature(hass):
|
||||||
"""Test api set color temperature process."""
|
"""Test api set color temperature process."""
|
||||||
|
|
Loading…
Reference in New Issue