Fix flux switch update interval (#17458)

This commit is contained in:
Anders Melchiorsen 2018-10-18 23:04:22 +02:00 committed by GitHub
parent 10c1378195
commit cc4d29d42a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 225 additions and 211 deletions

View file

@ -19,7 +19,7 @@ from homeassistant.components.switch import DOMAIN, SwitchDevice
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, CONF_NAME, CONF_PLATFORM, CONF_LIGHTS, CONF_MODE, ATTR_ENTITY_ID, CONF_NAME, CONF_PLATFORM, CONF_LIGHTS, CONF_MODE,
SERVICE_TURN_ON) SERVICE_TURN_ON)
from homeassistant.helpers.event import track_time_change from homeassistant.helpers.event import track_time_interval
from homeassistant.helpers.sun import get_astral_event_date from homeassistant.helpers.sun import get_astral_event_date
from homeassistant.util import slugify from homeassistant.util import slugify
from homeassistant.util.color import ( from homeassistant.util.color import (
@ -180,8 +180,10 @@ class FluxSwitch(SwitchDevice):
# Make initial update # Make initial update
self.flux_update() self.flux_update()
self.unsub_tracker = track_time_change( self.unsub_tracker = track_time_interval(
self.hass, self.flux_update, second=[0, self._interval]) self.hass,
self.flux_update,
datetime.timedelta(seconds=self._interval))
self.schedule_update_ha_state() self.schedule_update_ha_state()

View file

@ -183,22 +183,23 @@ class TestSwitchFlux(unittest.TestCase):
return sunrise_time return sunrise_time
return sunset_time return sunset_time
with patch('homeassistant.util.dt.now', return_value=test_time): with patch('homeassistant.components.switch.flux.dt_now',
with patch('homeassistant.helpers.sun.get_astral_event_date', return_value=test_time), \
side_effect=event_date): patch('homeassistant.helpers.sun.get_astral_event_date',
assert setup_component(self.hass, switch.DOMAIN, { side_effect=event_date):
switch.DOMAIN: { assert setup_component(self.hass, switch.DOMAIN, {
'platform': 'flux', switch.DOMAIN: {
'name': 'flux', 'platform': 'flux',
'lights': [dev1.entity_id] 'name': 'flux',
} 'lights': [dev1.entity_id]
}) }
turn_on_calls = mock_service( })
self.hass, light.DOMAIN, SERVICE_TURN_ON) turn_on_calls = mock_service(
common.turn_on(self.hass, 'switch.flux') self.hass, light.DOMAIN, SERVICE_TURN_ON)
self.hass.block_till_done() common.turn_on(self.hass, 'switch.flux')
fire_time_changed(self.hass, test_time) self.hass.block_till_done()
self.hass.block_till_done() fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
call = turn_on_calls[-1] call = turn_on_calls[-1]
self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 173) self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 173)
self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.439, 0.37]) self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.439, 0.37])
@ -229,23 +230,24 @@ class TestSwitchFlux(unittest.TestCase):
return sunrise_time return sunrise_time
return sunset_time return sunset_time
with patch('homeassistant.util.dt.now', return_value=test_time): with patch('homeassistant.components.switch.flux.dt_now',
with patch('homeassistant.helpers.sun.get_astral_event_date', return_value=test_time), \
side_effect=event_date): patch('homeassistant.helpers.sun.get_astral_event_date',
assert setup_component(self.hass, switch.DOMAIN, { side_effect=event_date):
switch.DOMAIN: { assert setup_component(self.hass, switch.DOMAIN, {
'platform': 'flux', switch.DOMAIN: {
'name': 'flux', 'platform': 'flux',
'lights': [dev1.entity_id], 'name': 'flux',
'stop_time': '22:00' 'lights': [dev1.entity_id],
} 'stop_time': '22:00'
}) }
turn_on_calls = mock_service( })
self.hass, light.DOMAIN, SERVICE_TURN_ON) turn_on_calls = mock_service(
common.turn_on(self.hass, 'switch.flux') self.hass, light.DOMAIN, SERVICE_TURN_ON)
self.hass.block_till_done() common.turn_on(self.hass, 'switch.flux')
fire_time_changed(self.hass, test_time) self.hass.block_till_done()
self.hass.block_till_done() fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
call = turn_on_calls[-1] call = turn_on_calls[-1]
self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 146) self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 146)
self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.506, 0.385]) self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.506, 0.385])
@ -322,24 +324,25 @@ class TestSwitchFlux(unittest.TestCase):
return sunrise_time return sunrise_time
return sunset_time return sunset_time
with patch('homeassistant.util.dt.now', return_value=test_time): with patch('homeassistant.components.switch.flux.dt_now',
with patch('homeassistant.helpers.sun.get_astral_event_date', return_value=test_time), \
side_effect=event_date): patch('homeassistant.helpers.sun.get_astral_event_date',
assert setup_component(self.hass, switch.DOMAIN, { side_effect=event_date):
switch.DOMAIN: { assert setup_component(self.hass, switch.DOMAIN, {
'platform': 'flux', switch.DOMAIN: {
'name': 'flux', 'platform': 'flux',
'lights': [dev1.entity_id], 'name': 'flux',
'start_time': '6:00', 'lights': [dev1.entity_id],
'stop_time': '23:30' 'start_time': '6:00',
} 'stop_time': '23:30'
}) }
turn_on_calls = mock_service( })
self.hass, light.DOMAIN, SERVICE_TURN_ON) turn_on_calls = mock_service(
common.turn_on(self.hass, 'switch.flux') self.hass, light.DOMAIN, SERVICE_TURN_ON)
self.hass.block_till_done() common.turn_on(self.hass, 'switch.flux')
fire_time_changed(self.hass, test_time) self.hass.block_till_done()
self.hass.block_till_done() fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
call = turn_on_calls[-1] call = turn_on_calls[-1]
self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 147) self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 147)
self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.504, 0.385]) self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.504, 0.385])
@ -372,23 +375,24 @@ class TestSwitchFlux(unittest.TestCase):
return sunrise_time return sunrise_time
return sunset_time return sunset_time
with patch('homeassistant.util.dt.now', return_value=test_time): with patch('homeassistant.components.switch.flux.dt_now',
with patch('homeassistant.helpers.sun.get_astral_event_date', return_value=test_time), \
side_effect=event_date): patch('homeassistant.helpers.sun.get_astral_event_date',
assert setup_component(self.hass, switch.DOMAIN, { side_effect=event_date):
switch.DOMAIN: { assert setup_component(self.hass, switch.DOMAIN, {
'platform': 'flux', switch.DOMAIN: {
'name': 'flux', 'platform': 'flux',
'lights': [dev1.entity_id], 'name': 'flux',
'stop_time': '01:00' 'lights': [dev1.entity_id],
} 'stop_time': '01:00'
}) }
turn_on_calls = mock_service( })
self.hass, light.DOMAIN, SERVICE_TURN_ON) turn_on_calls = mock_service(
common.turn_on(self.hass, 'switch.flux') self.hass, light.DOMAIN, SERVICE_TURN_ON)
self.hass.block_till_done() common.turn_on(self.hass, 'switch.flux')
fire_time_changed(self.hass, test_time) self.hass.block_till_done()
self.hass.block_till_done() fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
call = turn_on_calls[-1] call = turn_on_calls[-1]
self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 112) self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 112)
self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.606, 0.379]) self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.606, 0.379])
@ -423,23 +427,24 @@ class TestSwitchFlux(unittest.TestCase):
return sunrise_time return sunrise_time
return sunset_time return sunset_time
with patch('homeassistant.util.dt.now', return_value=test_time): with patch('homeassistant.components.switch.flux.dt_now',
with patch('homeassistant.helpers.sun.get_astral_event_date', return_value=test_time), \
side_effect=event_date): patch('homeassistant.helpers.sun.get_astral_event_date',
assert setup_component(self.hass, switch.DOMAIN, { side_effect=event_date):
switch.DOMAIN: { assert setup_component(self.hass, switch.DOMAIN, {
'platform': 'flux', switch.DOMAIN: {
'name': 'flux', 'platform': 'flux',
'lights': [dev1.entity_id], 'name': 'flux',
'stop_time': '01:00' 'lights': [dev1.entity_id],
} 'stop_time': '01:00'
}) }
turn_on_calls = mock_service( })
self.hass, light.DOMAIN, SERVICE_TURN_ON) turn_on_calls = mock_service(
common.turn_on(self.hass, 'switch.flux') self.hass, light.DOMAIN, SERVICE_TURN_ON)
self.hass.block_till_done() common.turn_on(self.hass, 'switch.flux')
fire_time_changed(self.hass, test_time) self.hass.block_till_done()
self.hass.block_till_done() fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
call = turn_on_calls[-1] call = turn_on_calls[-1]
self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 173) self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 173)
self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.439, 0.37]) self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.439, 0.37])
@ -523,23 +528,24 @@ class TestSwitchFlux(unittest.TestCase):
return sunrise_time return sunrise_time
return sunset_time return sunset_time
with patch('homeassistant.util.dt.now', return_value=test_time): with patch('homeassistant.components.switch.flux.dt_now',
with patch('homeassistant.helpers.sun.get_astral_event_date', return_value=test_time), \
side_effect=event_date): patch('homeassistant.helpers.sun.get_astral_event_date',
assert setup_component(self.hass, switch.DOMAIN, { side_effect=event_date):
switch.DOMAIN: { assert setup_component(self.hass, switch.DOMAIN, {
'platform': 'flux', switch.DOMAIN: {
'name': 'flux', 'platform': 'flux',
'lights': [dev1.entity_id], 'name': 'flux',
'stop_time': '01:00' 'lights': [dev1.entity_id],
} 'stop_time': '01:00'
}) }
turn_on_calls = mock_service( })
self.hass, light.DOMAIN, SERVICE_TURN_ON) turn_on_calls = mock_service(
common.turn_on(self.hass, 'switch.flux') self.hass, light.DOMAIN, SERVICE_TURN_ON)
self.hass.block_till_done() common.turn_on(self.hass, 'switch.flux')
fire_time_changed(self.hass, test_time) self.hass.block_till_done()
self.hass.block_till_done() fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
call = turn_on_calls[-1] call = turn_on_calls[-1]
self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 114) self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 114)
self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.601, 0.382]) self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.601, 0.382])
@ -573,23 +579,24 @@ class TestSwitchFlux(unittest.TestCase):
return sunrise_time return sunrise_time
return sunset_time return sunset_time
with patch('homeassistant.util.dt.now', return_value=test_time): with patch('homeassistant.components.switch.flux.dt_now',
with patch('homeassistant.helpers.sun.get_astral_event_date', return_value=test_time), \
side_effect=event_date): patch('homeassistant.helpers.sun.get_astral_event_date',
assert setup_component(self.hass, switch.DOMAIN, { side_effect=event_date):
switch.DOMAIN: { assert setup_component(self.hass, switch.DOMAIN, {
'platform': 'flux', switch.DOMAIN: {
'name': 'flux', 'platform': 'flux',
'lights': [dev1.entity_id], 'name': 'flux',
'stop_time': '01:00' 'lights': [dev1.entity_id],
} 'stop_time': '01:00'
}) }
turn_on_calls = mock_service( })
self.hass, light.DOMAIN, SERVICE_TURN_ON) turn_on_calls = mock_service(
common.turn_on(self.hass, 'switch.flux') self.hass, light.DOMAIN, SERVICE_TURN_ON)
self.hass.block_till_done() common.turn_on(self.hass, 'switch.flux')
fire_time_changed(self.hass, test_time) self.hass.block_till_done()
self.hass.block_till_done() fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
call = turn_on_calls[-1] call = turn_on_calls[-1]
self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 112) self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 112)
self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.606, 0.379]) self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.606, 0.379])
@ -620,25 +627,26 @@ class TestSwitchFlux(unittest.TestCase):
return sunrise_time return sunrise_time
return sunset_time return sunset_time
with patch('homeassistant.util.dt.now', return_value=test_time): with patch('homeassistant.components.switch.flux.dt_now',
with patch('homeassistant.helpers.sun.get_astral_event_date', return_value=test_time), \
side_effect=event_date): patch('homeassistant.helpers.sun.get_astral_event_date',
assert setup_component(self.hass, switch.DOMAIN, { side_effect=event_date):
switch.DOMAIN: { assert setup_component(self.hass, switch.DOMAIN, {
'platform': 'flux', switch.DOMAIN: {
'name': 'flux', 'platform': 'flux',
'lights': [dev1.entity_id], 'name': 'flux',
'start_colortemp': '1000', 'lights': [dev1.entity_id],
'stop_colortemp': '6000', 'start_colortemp': '1000',
'stop_time': '22:00' 'stop_colortemp': '6000',
} 'stop_time': '22:00'
}) }
turn_on_calls = mock_service( })
self.hass, light.DOMAIN, SERVICE_TURN_ON) turn_on_calls = mock_service(
common.turn_on(self.hass, 'switch.flux') self.hass, light.DOMAIN, SERVICE_TURN_ON)
self.hass.block_till_done() common.turn_on(self.hass, 'switch.flux')
fire_time_changed(self.hass, test_time) self.hass.block_till_done()
self.hass.block_till_done() fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
call = turn_on_calls[-1] call = turn_on_calls[-1]
self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 159) self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 159)
self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.469, 0.378]) self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.469, 0.378])
@ -669,24 +677,25 @@ class TestSwitchFlux(unittest.TestCase):
return sunrise_time return sunrise_time
return sunset_time return sunset_time
with patch('homeassistant.util.dt.now', return_value=test_time): with patch('homeassistant.components.switch.flux.dt_now',
with patch('homeassistant.helpers.sun.get_astral_event_date', return_value=test_time), \
side_effect=event_date): patch('homeassistant.helpers.sun.get_astral_event_date',
assert setup_component(self.hass, switch.DOMAIN, { side_effect=event_date):
switch.DOMAIN: { assert setup_component(self.hass, switch.DOMAIN, {
'platform': 'flux', switch.DOMAIN: {
'name': 'flux', 'platform': 'flux',
'lights': [dev1.entity_id], 'name': 'flux',
'brightness': 255, 'lights': [dev1.entity_id],
'stop_time': '22:00' 'brightness': 255,
} 'stop_time': '22:00'
}) }
turn_on_calls = mock_service( })
self.hass, light.DOMAIN, SERVICE_TURN_ON) turn_on_calls = mock_service(
common.turn_on(self.hass, 'switch.flux') self.hass, light.DOMAIN, SERVICE_TURN_ON)
self.hass.block_till_done() common.turn_on(self.hass, 'switch.flux')
fire_time_changed(self.hass, test_time) self.hass.block_till_done()
self.hass.block_till_done() fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
call = turn_on_calls[-1] call = turn_on_calls[-1]
self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 255) self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 255)
self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.506, 0.385]) self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.506, 0.385])
@ -731,24 +740,25 @@ class TestSwitchFlux(unittest.TestCase):
print('sunset {}'.format(sunset_time)) print('sunset {}'.format(sunset_time))
return sunset_time return sunset_time
with patch('homeassistant.util.dt.now', return_value=test_time): with patch('homeassistant.components.switch.flux.dt_now',
with patch('homeassistant.helpers.sun.get_astral_event_date', return_value=test_time), \
side_effect=event_date): patch('homeassistant.helpers.sun.get_astral_event_date',
assert setup_component(self.hass, switch.DOMAIN, { side_effect=event_date):
switch.DOMAIN: { assert setup_component(self.hass, switch.DOMAIN, {
'platform': 'flux', switch.DOMAIN: {
'name': 'flux', 'platform': 'flux',
'lights': [dev1.entity_id, 'name': 'flux',
dev2.entity_id, 'lights': [dev1.entity_id,
dev3.entity_id] dev2.entity_id,
} dev3.entity_id]
}) }
turn_on_calls = mock_service( })
self.hass, light.DOMAIN, SERVICE_TURN_ON) turn_on_calls = mock_service(
common.turn_on(self.hass, 'switch.flux') self.hass, light.DOMAIN, SERVICE_TURN_ON)
self.hass.block_till_done() common.turn_on(self.hass, 'switch.flux')
fire_time_changed(self.hass, test_time) self.hass.block_till_done()
self.hass.block_till_done() fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
call = turn_on_calls[-1] call = turn_on_calls[-1]
self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 163) self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 163)
self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.46, 0.376]) self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.46, 0.376])
@ -783,23 +793,24 @@ class TestSwitchFlux(unittest.TestCase):
return sunrise_time return sunrise_time
return sunset_time return sunset_time
with patch('homeassistant.util.dt.now', return_value=test_time): with patch('homeassistant.components.switch.flux.dt_now',
with patch('homeassistant.helpers.sun.get_astral_event_date', return_value=test_time), \
side_effect=event_date): patch('homeassistant.helpers.sun.get_astral_event_date',
assert setup_component(self.hass, switch.DOMAIN, { side_effect=event_date):
switch.DOMAIN: { assert setup_component(self.hass, switch.DOMAIN, {
'platform': 'flux', switch.DOMAIN: {
'name': 'flux', 'platform': 'flux',
'lights': [dev1.entity_id], 'name': 'flux',
'mode': 'mired' 'lights': [dev1.entity_id],
} 'mode': 'mired'
}) }
turn_on_calls = mock_service( })
self.hass, light.DOMAIN, SERVICE_TURN_ON) turn_on_calls = mock_service(
common.turn_on(self.hass, 'switch.flux') self.hass, light.DOMAIN, SERVICE_TURN_ON)
self.hass.block_till_done() common.turn_on(self.hass, 'switch.flux')
fire_time_changed(self.hass, test_time) self.hass.block_till_done()
self.hass.block_till_done() fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
call = turn_on_calls[-1] call = turn_on_calls[-1]
self.assertEqual(call.data[light.ATTR_COLOR_TEMP], 269) self.assertEqual(call.data[light.ATTR_COLOR_TEMP], 269)
@ -827,23 +838,24 @@ class TestSwitchFlux(unittest.TestCase):
return sunrise_time return sunrise_time
return sunset_time return sunset_time
with patch('homeassistant.util.dt.now', return_value=test_time): with patch('homeassistant.components.switch.flux.dt_now',
with patch('homeassistant.helpers.sun.get_astral_event_date', return_value=test_time), \
side_effect=event_date): patch('homeassistant.helpers.sun.get_astral_event_date',
assert setup_component(self.hass, switch.DOMAIN, { side_effect=event_date):
switch.DOMAIN: { assert setup_component(self.hass, switch.DOMAIN, {
'platform': 'flux', switch.DOMAIN: {
'name': 'flux', 'platform': 'flux',
'lights': [dev1.entity_id], 'name': 'flux',
'mode': 'rgb' 'lights': [dev1.entity_id],
} 'mode': 'rgb'
}) }
turn_on_calls = mock_service( })
self.hass, light.DOMAIN, SERVICE_TURN_ON) turn_on_calls = mock_service(
common.turn_on(self.hass, 'switch.flux') self.hass, light.DOMAIN, SERVICE_TURN_ON)
self.hass.block_till_done() common.turn_on(self.hass, 'switch.flux')
fire_time_changed(self.hass, test_time) self.hass.block_till_done()
self.hass.block_till_done() fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
call = turn_on_calls[-1] call = turn_on_calls[-1]
rgb = (255, 198, 152) rgb = (255, 198, 152)
rounded_call = tuple(map(round, call.data[light.ATTR_RGB_COLOR])) rounded_call = tuple(map(round, call.data[light.ATTR_RGB_COLOR]))