Use supported_color_modes in homekit (#49177)

This commit is contained in:
Erik Montnemery 2021-04-14 09:18:34 +02:00 committed by GitHub
parent 1a5068f71d
commit e0ac12bd56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 20 deletions

View File

@ -10,10 +10,11 @@ from homeassistant.components.light import (
ATTR_HS_COLOR, ATTR_HS_COLOR,
ATTR_MAX_MIREDS, ATTR_MAX_MIREDS,
ATTR_MIN_MIREDS, ATTR_MIN_MIREDS,
ATTR_SUPPORTED_COLOR_MODES,
COLOR_MODE_COLOR_TEMP,
COLOR_MODES_BRIGHTNESS,
COLOR_MODES_COLOR,
DOMAIN, DOMAIN,
SUPPORT_BRIGHTNESS,
SUPPORT_COLOR,
SUPPORT_COLOR_TEMP,
) )
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_ENTITY_ID,
@ -61,14 +62,15 @@ class Light(HomeAccessory):
state = self.hass.states.get(self.entity_id) state = self.hass.states.get(self.entity_id)
self._features = state.attributes.get(ATTR_SUPPORTED_FEATURES, 0) self._features = state.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
self._color_modes = state.attributes.get(ATTR_SUPPORTED_COLOR_MODES, [])
if self._features & SUPPORT_BRIGHTNESS: if any(mode in self._color_modes for mode in COLOR_MODES_BRIGHTNESS):
self.chars.append(CHAR_BRIGHTNESS) self.chars.append(CHAR_BRIGHTNESS)
if self._features & SUPPORT_COLOR: if any(mode in self._color_modes for mode in COLOR_MODES_COLOR):
self.chars.append(CHAR_HUE) self.chars.append(CHAR_HUE)
self.chars.append(CHAR_SATURATION) self.chars.append(CHAR_SATURATION)
elif self._features & SUPPORT_COLOR_TEMP: elif COLOR_MODE_COLOR_TEMP in self._color_modes:
# ColorTemperature and Hue characteristic should not be # ColorTemperature and Hue characteristic should not be
# exposed both. Both states are tracked separately in HomeKit, # exposed both. Both states are tracked separately in HomeKit,
# causing "source of truth" problems. # causing "source of truth" problems.
@ -130,7 +132,7 @@ class Light(HomeAccessory):
events.append(f"color temperature at {char_values[CHAR_COLOR_TEMPERATURE]}") events.append(f"color temperature at {char_values[CHAR_COLOR_TEMPERATURE]}")
if ( if (
self._features & SUPPORT_COLOR any(mode in self._color_modes for mode in COLOR_MODES_COLOR)
and CHAR_HUE in char_values and CHAR_HUE in char_values
and CHAR_SATURATION in char_values and CHAR_SATURATION in char_values
): ):

View File

@ -1,6 +1,7 @@
"""Test different accessory types: Lights.""" """Test different accessory types: Lights."""
from pyhap.const import HAP_REPR_AID, HAP_REPR_CHARS, HAP_REPR_IID, HAP_REPR_VALUE from pyhap.const import HAP_REPR_AID, HAP_REPR_CHARS, HAP_REPR_IID, HAP_REPR_VALUE
import pytest
from homeassistant.components.homekit.const import ATTR_VALUE from homeassistant.components.homekit.const import ATTR_VALUE
from homeassistant.components.homekit.type_lights import Light from homeassistant.components.homekit.type_lights import Light
@ -9,10 +10,8 @@ from homeassistant.components.light import (
ATTR_BRIGHTNESS_PCT, ATTR_BRIGHTNESS_PCT,
ATTR_COLOR_TEMP, ATTR_COLOR_TEMP,
ATTR_HS_COLOR, ATTR_HS_COLOR,
ATTR_SUPPORTED_COLOR_MODES,
DOMAIN, DOMAIN,
SUPPORT_BRIGHTNESS,
SUPPORT_COLOR,
SUPPORT_COLOR_TEMP,
) )
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_ENTITY_ID,
@ -98,14 +97,17 @@ async def test_light_basic(hass, hk_driver, events):
assert events[-1].data[ATTR_VALUE] == "Set state to 0" assert events[-1].data[ATTR_VALUE] == "Set state to 0"
async def test_light_brightness(hass, hk_driver, events): @pytest.mark.parametrize(
"supported_color_modes", [["brightness"], ["hs"], ["color_temp"]]
)
async def test_light_brightness(hass, hk_driver, events, supported_color_modes):
"""Test light with brightness.""" """Test light with brightness."""
entity_id = "light.demo" entity_id = "light.demo"
hass.states.async_set( hass.states.async_set(
entity_id, entity_id,
STATE_ON, STATE_ON,
{ATTR_SUPPORTED_FEATURES: SUPPORT_BRIGHTNESS, ATTR_BRIGHTNESS: 255}, {ATTR_SUPPORTED_COLOR_MODES: supported_color_modes, ATTR_BRIGHTNESS: 255},
) )
await hass.async_block_till_done() await hass.async_block_till_done()
acc = Light(hass, hk_driver, "Light", entity_id, 1, None) acc = Light(hass, hk_driver, "Light", entity_id, 1, None)
@ -223,7 +225,7 @@ async def test_light_color_temperature(hass, hk_driver, events):
hass.states.async_set( hass.states.async_set(
entity_id, entity_id,
STATE_ON, STATE_ON,
{ATTR_SUPPORTED_FEATURES: SUPPORT_COLOR_TEMP, ATTR_COLOR_TEMP: 190}, {ATTR_SUPPORTED_COLOR_MODES: ["color_temp"], ATTR_COLOR_TEMP: 190},
) )
await hass.async_block_till_done() await hass.async_block_till_done()
acc = Light(hass, hk_driver, "Light", entity_id, 1, None) acc = Light(hass, hk_driver, "Light", entity_id, 1, None)
@ -263,7 +265,12 @@ async def test_light_color_temperature(hass, hk_driver, events):
assert events[-1].data[ATTR_VALUE] == "color temperature at 250" assert events[-1].data[ATTR_VALUE] == "color temperature at 250"
async def test_light_color_temperature_and_rgb_color(hass, hk_driver, events): @pytest.mark.parametrize(
"supported_color_modes", [["ct", "hs"], ["ct", "rgb"], ["ct", "xy"]]
)
async def test_light_color_temperature_and_rgb_color(
hass, hk_driver, events, supported_color_modes
):
"""Test light with color temperature and rgb color not exposing temperature.""" """Test light with color temperature and rgb color not exposing temperature."""
entity_id = "light.demo" entity_id = "light.demo"
@ -271,7 +278,7 @@ async def test_light_color_temperature_and_rgb_color(hass, hk_driver, events):
entity_id, entity_id,
STATE_ON, STATE_ON,
{ {
ATTR_SUPPORTED_FEATURES: SUPPORT_COLOR_TEMP | SUPPORT_COLOR, ATTR_SUPPORTED_COLOR_MODES: supported_color_modes,
ATTR_COLOR_TEMP: 190, ATTR_COLOR_TEMP: 190,
ATTR_HS_COLOR: (260, 90), ATTR_HS_COLOR: (260, 90),
}, },
@ -298,14 +305,15 @@ async def test_light_color_temperature_and_rgb_color(hass, hk_driver, events):
assert acc.char_saturation.value == 61 assert acc.char_saturation.value == 61
async def test_light_rgb_color(hass, hk_driver, events): @pytest.mark.parametrize("supported_color_modes", [["hs"], ["rgb"], ["xy"]])
async def test_light_rgb_color(hass, hk_driver, events, supported_color_modes):
"""Test light with rgb_color.""" """Test light with rgb_color."""
entity_id = "light.demo" entity_id = "light.demo"
hass.states.async_set( hass.states.async_set(
entity_id, entity_id,
STATE_ON, STATE_ON,
{ATTR_SUPPORTED_FEATURES: SUPPORT_COLOR, ATTR_HS_COLOR: (260, 90)}, {ATTR_SUPPORTED_COLOR_MODES: supported_color_modes, ATTR_HS_COLOR: (260, 90)},
) )
await hass.async_block_till_done() await hass.async_block_till_done()
acc = Light(hass, hk_driver, "Light", entity_id, 1, None) acc = Light(hass, hk_driver, "Light", entity_id, 1, None)
@ -362,7 +370,7 @@ async def test_light_restore(hass, hk_driver, events):
"hue", "hue",
"9012", "9012",
suggested_object_id="all_info_set", suggested_object_id="all_info_set",
capabilities={"max": 100}, capabilities={"supported_color_modes": ["brightness"], "max": 100},
supported_features=5, supported_features=5,
device_class="mock-device-class", device_class="mock-device-class",
) )
@ -391,7 +399,7 @@ async def test_light_set_brightness_and_color(hass, hk_driver, events):
entity_id, entity_id,
STATE_ON, STATE_ON,
{ {
ATTR_SUPPORTED_FEATURES: SUPPORT_BRIGHTNESS | SUPPORT_COLOR, ATTR_SUPPORTED_COLOR_MODES: ["hs"],
ATTR_BRIGHTNESS: 255, ATTR_BRIGHTNESS: 255,
}, },
) )
@ -467,7 +475,7 @@ async def test_light_set_brightness_and_color_temp(hass, hk_driver, events):
entity_id, entity_id,
STATE_ON, STATE_ON,
{ {
ATTR_SUPPORTED_FEATURES: SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP, ATTR_SUPPORTED_COLOR_MODES: ["color_temp"],
ATTR_BRIGHTNESS: 255, ATTR_BRIGHTNESS: 255,
}, },
) )