Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions homeassistant/components/light/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
EFFECT_RANDOM = "random"
EFFECT_WHITE = "white"

COLOR_GROUP = "Color descriptors"

LIGHT_PROFILES_FILE = "light_profiles.csv"

PROP_TO_ATTR = {
Expand All @@ -98,17 +100,21 @@

LIGHT_TURN_ON_SCHEMA = vol.Schema({
ATTR_ENTITY_ID: cv.entity_ids,
ATTR_PROFILE: cv.string,
vol.Exclusive(ATTR_PROFILE, COLOR_GROUP): cv.string,
ATTR_TRANSITION: VALID_TRANSITION,
ATTR_BRIGHTNESS: VALID_BRIGHTNESS,
ATTR_BRIGHTNESS_PCT: VALID_BRIGHTNESS_PCT,
ATTR_COLOR_NAME: cv.string,
ATTR_RGB_COLOR: vol.All(vol.ExactSequence((cv.byte, cv.byte, cv.byte)),
vol.Coerce(tuple)),
ATTR_XY_COLOR: vol.All(vol.ExactSequence((cv.small_float, cv.small_float)),
vol.Coerce(tuple)),
ATTR_COLOR_TEMP: vol.All(vol.Coerce(int), vol.Range(min=1)),
ATTR_KELVIN: vol.All(vol.Coerce(int), vol.Range(min=0)),
vol.Exclusive(ATTR_COLOR_NAME, COLOR_GROUP): cv.string,
vol.Exclusive(ATTR_RGB_COLOR, COLOR_GROUP):
vol.All(vol.ExactSequence((cv.byte, cv.byte, cv.byte)),
vol.Coerce(tuple)),
vol.Exclusive(ATTR_XY_COLOR, COLOR_GROUP):
vol.All(vol.ExactSequence((cv.small_float, cv.small_float)),
vol.Coerce(tuple)),
vol.Exclusive(ATTR_COLOR_TEMP, COLOR_GROUP):
vol.All(vol.Coerce(int), vol.Range(min=1)),
vol.Exclusive(ATTR_KELVIN, COLOR_GROUP):
vol.All(vol.Coerce(int), vol.Range(min=0)),
ATTR_WHITE_VALUE: vol.All(vol.Coerce(int), vol.Range(min=0, max=255)),
ATTR_FLASH: vol.In([FLASH_SHORT, FLASH_LONG]),
ATTR_EFFECT: cv.string,
Expand Down
13 changes: 8 additions & 5 deletions tests/components/light/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,10 @@ def test_services(self):

# Test light profiles
light.turn_on(self.hass, dev1.entity_id, profile=prof_name)
# Specify a profile and attributes to overwrite it
# Specify a profile and a brightness attribute to overwrite it
light.turn_on(
self.hass, dev2.entity_id,
profile=prof_name, brightness=100, xy_color=(.4, .6))
profile=prof_name, brightness=100)

self.hass.block_till_done()

Expand All @@ -222,10 +222,10 @@ def test_services(self):
_, data = dev2.last_call('turn_on')
self.assertEqual(
{light.ATTR_BRIGHTNESS: 100,
light.ATTR_XY_COLOR: (.4, .6)},
light.ATTR_XY_COLOR: (.5119, .4147)},
data)

# Test shitty data
# Test bad data
light.turn_on(self.hass)
light.turn_on(self.hass, dev1.entity_id, profile="nonexisting")
light.turn_on(self.hass, dev2.entity_id, xy_color=["bla-di-bla", 5])
Expand All @@ -245,7 +245,10 @@ def test_services(self):
# faulty attributes will not trigger a service call
light.turn_on(
self.hass, dev1.entity_id,
profile=prof_name, brightness='bright', rgb_color='yellowish')
profile=prof_name, brightness='bright')
light.turn_on(
self.hass, dev1.entity_id,
rgb_color='yellowish')
light.turn_on(
self.hass, dev2.entity_id,
white_value='high')
Expand Down
4 changes: 3 additions & 1 deletion tests/components/light/test_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,10 @@ def test_sending_mqtt_commands_and_optimistic(self): \
self.assertEqual(STATE_OFF, state.state)

self.mock_publish.reset_mock()
light.turn_on(self.hass, 'light.test',
brightness=50, xy_color=[0.123, 0.123])
light.turn_on(self.hass, 'light.test', rgb_color=[75, 75, 75],
brightness=50, white_value=80, xy_color=[0.123, 0.123])
white_value=80)
self.hass.block_till_done()

self.mock_publish().async_publish.assert_has_calls([
Expand Down
6 changes: 1 addition & 5 deletions tests/components/light/test_mqtt_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def test_sending_mqtt_commands_and_optimistic(self): \
state = self.hass.states.get('light.test')
self.assertEqual(STATE_OFF, state.state)

light.turn_on(self.hass, 'light.test', rgb_color=[75, 75, 75],
light.turn_on(self.hass, 'light.test',
brightness=50, color_temp=155, effect='colorloop',
white_value=170)
self.hass.block_till_done()
Expand All @@ -308,15 +308,11 @@ def test_sending_mqtt_commands_and_optimistic(self): \
self.assertEqual(50, message_json["brightness"])
self.assertEqual(155, message_json["color_temp"])
self.assertEqual('colorloop', message_json["effect"])
self.assertEqual(75, message_json["color"]["r"])
self.assertEqual(75, message_json["color"]["g"])
self.assertEqual(75, message_json["color"]["b"])
self.assertEqual(170, message_json["white_value"])
self.assertEqual("ON", message_json["state"])

state = self.hass.states.get('light.test')
self.assertEqual(STATE_ON, state.state)
self.assertEqual((75, 75, 75), state.attributes['rgb_color'])
self.assertEqual(50, state.attributes['brightness'])
self.assertEqual(155, state.attributes['color_temp'])
self.assertEqual('colorloop', state.attributes['effect'])
Expand Down
18 changes: 13 additions & 5 deletions tests/components/light/test_mqtt_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,19 +245,27 @@ def test_optimistic(self): \
state = self.hass.states.get('light.test')
self.assertEqual(STATE_OFF, state.state)

# turn on the light with brightness, color, color temp and white val
# turn on the light with brightness, color
light.turn_on(self.hass, 'light.test', brightness=50,
rgb_color=[75, 75, 75], color_temp=200, white_value=139)
rgb_color=[75, 75, 75])
self.hass.block_till_done()

self.assertEqual('test_light_rgb/set',
self.mock_publish.mock_calls[-2][1][0])
self.assertEqual(2, self.mock_publish.mock_calls[-2][1][2])
self.assertEqual(False, self.mock_publish.mock_calls[-2][1][3])

# check the payload
payload = self.mock_publish.mock_calls[-2][1][1]
self.assertEqual('on,50,200,139,75-75-75', payload)
self.assertEqual('on,50,,,75-75-75', payload)

# turn on the light with color temp and white val
light.turn_on(self.hass, 'light.test', color_temp=200, white_value=139)
self.hass.block_till_done()

payload = self.mock_publish.mock_calls[-2][1][1]
self.assertEqual('on,,200,139,--', payload)

self.assertEqual(2, self.mock_publish.mock_calls[-2][1][2])
self.assertEqual(False, self.mock_publish.mock_calls[-2][1][3])

# check the state
state = self.hass.states.get('light.test')
Expand Down