def test_methods(self): """ Test if methods call the services as expected. """ # Test is_on self.hass.states.set('light.test', STATE_ON) self.assertTrue(light.is_on(self.hass, 'light.test')) self.hass.states.set('light.test', STATE_OFF) self.assertFalse(light.is_on(self.hass, 'light.test')) self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_ON) self.assertTrue(light.is_on(self.hass)) self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_OFF) self.assertFalse(light.is_on(self.hass)) # Test turn_on turn_on_calls = mock_service( self.hass, light.DOMAIN, SERVICE_TURN_ON) light.turn_on( self.hass, entity_id='entity_id_val', transition='transition_val', brightness='brightness_val', rgb_color='rgb_color_val', xy_color='xy_color_val', profile='profile_val') self.hass.pool.block_till_done() self.assertEqual(1, len(turn_on_calls)) call = turn_on_calls[-1] self.assertEqual(light.DOMAIN, call.domain) self.assertEqual(SERVICE_TURN_ON, call.service) self.assertEqual('entity_id_val', call.data.get(ATTR_ENTITY_ID)) self.assertEqual( 'transition_val', call.data.get(light.ATTR_TRANSITION)) self.assertEqual( 'brightness_val', call.data.get(light.ATTR_BRIGHTNESS)) self.assertEqual('rgb_color_val', call.data.get(light.ATTR_RGB_COLOR)) self.assertEqual('xy_color_val', call.data.get(light.ATTR_XY_COLOR)) self.assertEqual('profile_val', call.data.get(light.ATTR_PROFILE)) # Test turn_off turn_off_calls = mock_service( self.hass, light.DOMAIN, SERVICE_TURN_OFF) light.turn_off( self.hass, entity_id='entity_id_val', transition='transition_val') self.hass.pool.block_till_done() self.assertEqual(1, len(turn_off_calls)) call = turn_off_calls[-1] self.assertEqual(light.DOMAIN, call.domain) self.assertEqual(SERVICE_TURN_OFF, call.service) self.assertEqual('entity_id_val', call.data[ATTR_ENTITY_ID]) self.assertEqual('transition_val', call.data[light.ATTR_TRANSITION])
def test_deactivated_event(self): """Test handling an event from LiteJet.""" # Initial state is on. self.mock_lj.get_load_level.return_value = 99 self.load_activated_callbacks[ENTITY_OTHER_LIGHT_NUMBER]() self.hass.block_till_done() assert light.is_on(self.hass, ENTITY_OTHER_LIGHT) # Event indicates it is off now. self.mock_lj.get_load_level.reset_mock() self.mock_lj.get_load_level.return_value = 0 self.load_deactivated_callbacks[ENTITY_OTHER_LIGHT_NUMBER]() self.hass.block_till_done() # (Requesting the level is not strictly needed with a deactivated # event but the implementation happens to do it. This could be # changed to a assert_not_called in the future.) self.mock_lj.get_load_level.assert_called_with( ENTITY_OTHER_LIGHT_NUMBER) assert not light.is_on(self.hass, ENTITY_OTHER_LIGHT) assert not light.is_on(self.hass, ENTITY_LIGHT) assert self.light().state == 'off' assert self.other_light().state == 'off'
def test_turn_off(self): """Test light turn off method.""" light.turn_on(self.hass, ENTITY_LIGHT) self.hass.block_till_done() self.assertTrue(light.is_on(self.hass, ENTITY_LIGHT)) light.turn_off(self.hass, ENTITY_LIGHT) self.hass.block_till_done() self.assertFalse(light.is_on(self.hass, ENTITY_LIGHT))
def test_turn_off_without_entity_id(self): """Test light turn off all lights.""" light.turn_on(self.hass, ENTITY_LIGHT) self.hass.block_till_done() self.assertTrue(light.is_on(self.hass, ENTITY_LIGHT)) light.turn_off(self.hass) self.hass.block_till_done() self.assertFalse(light.is_on(self.hass, ENTITY_LIGHT))
async def test_turn_off_without_entity_id(hass): """Test light turn off all lights.""" await hass.services.async_call('light', 'turn_on', { }, blocking=True) assert light.is_on(hass, ENTITY_LIGHT) await hass.services.async_call('light', 'turn_off', { }, blocking=True) assert not light.is_on(hass, ENTITY_LIGHT)
async def test_turn_off(hass): """Test light turn off method.""" await hass.services.async_call('light', 'turn_on', { 'entity_id': ENTITY_LIGHT }, blocking=True) assert light.is_on(hass, ENTITY_LIGHT) await hass.services.async_call('light', 'turn_off', { 'entity_id': ENTITY_LIGHT }, blocking=True) assert not light.is_on(hass, ENTITY_LIGHT)
def set_lights_rgb(hass, lights, rgb, transition): """Set color of array of lights.""" for light in lights: if is_on(hass, light): turn_on(hass, light, rgb_color=rgb, transition=transition)
async def test_state_attributes(hass): """Test light state attributes.""" common.async_turn_on( hass, ENTITY_LIGHT, xy_color=(.4, .4), brightness=25) await hass.async_block_till_done() state = hass.states.get(ENTITY_LIGHT) assert light.is_on(hass, ENTITY_LIGHT) assert (0.4, 0.4) == state.attributes.get(light.ATTR_XY_COLOR) assert 25 == state.attributes.get(light.ATTR_BRIGHTNESS) assert (255, 234, 164) == state.attributes.get(light.ATTR_RGB_COLOR) assert 'rainbow' == state.attributes.get(light.ATTR_EFFECT) common.async_turn_on( hass, ENTITY_LIGHT, rgb_color=(251, 253, 255), white_value=254) await hass.async_block_till_done() state = hass.states.get(ENTITY_LIGHT) assert 254 == state.attributes.get(light.ATTR_WHITE_VALUE) assert (250, 252, 255) == state.attributes.get(light.ATTR_RGB_COLOR) assert (0.319, 0.326) == state.attributes.get(light.ATTR_XY_COLOR) common.async_turn_on(hass, ENTITY_LIGHT, color_temp=400, effect='none') await hass.async_block_till_done() state = hass.states.get(ENTITY_LIGHT) assert 400 == state.attributes.get(light.ATTR_COLOR_TEMP) assert 153 == state.attributes.get(light.ATTR_MIN_MIREDS) assert 500 == state.attributes.get(light.ATTR_MAX_MIREDS) assert 'none' == state.attributes.get(light.ATTR_EFFECT) common.async_turn_on(hass, ENTITY_LIGHT, kelvin=3000, brightness_pct=50) await hass.async_block_till_done() state = hass.states.get(ENTITY_LIGHT) assert 333 == state.attributes.get(light.ATTR_COLOR_TEMP) assert 127 == state.attributes.get(light.ATTR_BRIGHTNESS)
def test_state_attributes(self): """Test light state attributes.""" light.turn_on( self.hass, ENTITY_LIGHT, xy_color=(.4, .6), brightness=25) self.hass.block_till_done() state = self.hass.states.get(ENTITY_LIGHT) self.assertTrue(light.is_on(self.hass, ENTITY_LIGHT)) self.assertEqual((.4, .6), state.attributes.get(light.ATTR_XY_COLOR)) self.assertEqual(25, state.attributes.get(light.ATTR_BRIGHTNESS)) self.assertEqual( (76, 95, 0), state.attributes.get(light.ATTR_RGB_COLOR)) self.assertEqual('rainbow', state.attributes.get(light.ATTR_EFFECT)) light.turn_on( self.hass, ENTITY_LIGHT, rgb_color=(251, 252, 253), white_value=254) self.hass.block_till_done() state = self.hass.states.get(ENTITY_LIGHT) self.assertEqual(254, state.attributes.get(light.ATTR_WHITE_VALUE)) self.assertEqual( (251, 252, 253), state.attributes.get(light.ATTR_RGB_COLOR)) light.turn_on(self.hass, ENTITY_LIGHT, color_temp=400, effect='none') self.hass.block_till_done() state = self.hass.states.get(ENTITY_LIGHT) self.assertEqual(400, state.attributes.get(light.ATTR_COLOR_TEMP)) self.assertEqual(154, state.attributes.get(light.ATTR_MIN_MIREDS)) self.assertEqual(500, state.attributes.get(light.ATTR_MAX_MIREDS)) self.assertEqual('none', state.attributes.get(light.ATTR_EFFECT)) light.turn_on(self.hass, ENTITY_LIGHT, kelvin=3000, brightness_pct=50) self.hass.block_till_done() state = self.hass.states.get(ENTITY_LIGHT) self.assertEqual(333, state.attributes.get(light.ATTR_COLOR_TEMP)) self.assertEqual(127, state.attributes.get(light.ATTR_BRIGHTNESS))
def set_lights_xy(hass, lights, x_val, y_val, brightness): """Set color of array of lights.""" for light in lights: if is_on(hass, light): turn_on(hass, light, xy_color=[x_val, y_val], brightness=brightness, transition=30)
def set_lights_temp(hass, lights, mired, brightness): """Set color of array of lights.""" for light in lights: if is_on(hass, light): turn_on(hass, light, color_temp=int(mired), brightness=brightness, transition=30)
async def _adjust_lights(self, lights, transition): if not self._should_adjust(): return tasks = [ await self._adjust_light(light, transition) for light in lights if is_on(self.hass, light) ] if tasks: await asyncio.wait(tasks)
def set_lights_temp(hass, lights, kelvin, mode): """Set color of array of lights.""" temp = kelvin if mode == MODE_MIRED: temp = 1000000 / kelvin for light in lights: if is_on(hass, light): turn_on(hass, light, color_temp=int(temp), transition=30)
async def test_light_profiles( hass, mock_light_profiles, profile_name, expected_data, last_call ): """Test light profiles.""" platform = getattr(hass.components, "test.light") platform.init() profile_mock_data = { "test": (0.4, 0.6, 100, 0), "color_no_brightness_no_transition": (0.4, 0.6, None, None), "no color": (None, None, 110, 0), "test_off": (0, 0, 0, 0), "no brightness": (0.4, 0.6, None), "color_and_brightness": (0.4, 0.6, 120), "color_and_transition": (0.4, 0.6, None, 4.2), "brightness_and_transition": (None, None, 130, 5.3), } for name, data in profile_mock_data.items(): mock_light_profiles[name] = light.Profile(*(name, *data)) assert await async_setup_component( hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}} ) await hass.async_block_till_done() ent1, _, _ = platform.ENTITIES await hass.services.async_call( light.DOMAIN, SERVICE_TURN_ON, { ATTR_ENTITY_ID: ent1.entity_id, light.ATTR_PROFILE: profile_name, }, blocking=True, ) _, data = ent1.last_call(last_call) if last_call == "turn_on": assert light.is_on(hass, ent1.entity_id) else: assert not light.is_on(hass, ent1.entity_id) assert data == expected_data
def set_lights_rgb(hass, lights, rgb, transition): """Set color of array of lights.""" for light in lights: if is_on(hass, light): service_data = {ATTR_ENTITY_ID: light} if rgb is not None: service_data[ATTR_RGB_COLOR] = rgb if transition is not None: service_data[ATTR_TRANSITION] = transition hass.services.call(LIGHT_DOMAIN, SERVICE_TURN_ON, service_data)
def getlights(key): light_ids = [] for light_id in config[DOMAIN].get(key): if hass.states.get(light_id) is None: logger.error('Light id %s could not be found in state machine', light_id) return [] if light.is_on(hass, light_id): light_ids.append(light_id) return light_ids
async def test_light_profiles(hass, mock_light_profiles): """Test light profiles.""" platform = getattr(hass.components, "test.light") platform.init() mock_light_profiles["test"] = color.color_xy_to_hs(0.4, 0.6) + (100, 0) mock_light_profiles["test_off"] = 0, 0, 0, 0 assert await async_setup_component( hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}} ) await hass.async_block_till_done() ent1, _, _ = platform.ENTITIES await hass.services.async_call( light.DOMAIN, SERVICE_TURN_ON, { ATTR_ENTITY_ID: ent1.entity_id, light.ATTR_PROFILE: "test", }, blocking=True, ) _, data = ent1.last_call("turn_on") assert light.is_on(hass, ent1.entity_id) assert data == { light.ATTR_HS_COLOR: (71.059, 100), light.ATTR_BRIGHTNESS: 100, light.ATTR_TRANSITION: 0, } await hass.services.async_call( light.DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: ent1.entity_id, light.ATTR_PROFILE: "test_off"}, blocking=True, ) _, data = ent1.last_call("turn_off") assert not light.is_on(hass, ent1.entity_id) assert data == {light.ATTR_TRANSITION: 0}
async def test_config_yaml_alias_anchor(hass, entities, enable_custom_integrations): """Test the usage of YAML aliases and anchors. The following test scene configuration is equivalent to: scene: - name: test entities: light_1: &light_1_state state: 'on' brightness: 100 light_2: *light_1_state When encountering a YAML alias/anchor, the PyYAML parser will use a reference to the original dictionary, instead of creating a copy, so care needs to be taken to not modify the original. """ light_1, light_2 = await setup_lights(hass, entities) entity_state = {"state": "on", "brightness": 100} assert await async_setup_component( hass, scene.DOMAIN, { "scene": [ { "name": "test", "entities": { light_1.entity_id: entity_state, light_2.entity_id: entity_state, }, } ] }, ) await hass.async_block_till_done() await activate(hass, "scene.test") assert light.is_on(hass, light_1.entity_id) assert light.is_on(hass, light_2.entity_id) assert light_1.last_call("turn_on")[1].get("brightness") == 100 assert light_2.last_call("turn_on")[1].get("brightness") == 100
def test_on_brightness(self): """Test turning the light on with brightness.""" assert self.light().state == "off" assert self.other_light().state == "off" assert not light.is_on(self.hass, ENTITY_LIGHT) common.turn_on(self.hass, ENTITY_LIGHT, brightness=102) self.hass.block_till_done() self.mock_lj.activate_load_at.assert_called_with(ENTITY_LIGHT_NUMBER, 39, 0)
def set_lights_rgb(hass, lights, rgb, transition, lastcolor): """Set color of array of lights.""" for light in lights: if is_on(hass, light): states = hass.states.get(light) if (lastcolor == 0) or (states.attributes.get('rgb_color') == lastcolor): turn_on(hass, light, rgb_color=rgb, transition=transition) return rgb
def set_lights_xy(hass, lights, x_val, y_val, brightness, transition, lastcolor): """Set color of array of lights.""" for light in lights: if is_on(hass, light): states = hass.states.get(light) if (lastcolor == 0) or (states.attributes.get('xy_color') == lastcolor): turn_on(hass, light, xy_color=[x_val, y_val], brightness=brightness, transition=transition) return [x_val, y_val]
def set_lights_temp(hass, lights, mired, brightness, transition, lastcolor): """Set color of array of lights.""" for light in lights: if is_on(hass, light): states = hass.states.get(light) if (lastcolor == 0) or (states.attributes.get('color_temp') == lastcolor): turn_on(hass, light, color_temp=int(mired), brightness=brightness, transition=transition) return int(mired)
def set_lights_rgb(hass, lights, rgb, transition): """Set color of array of lights.""" for light in lights: if is_on(hass, light): service_data = {ATTR_ENTITY_ID: light} if rgb is not None: service_data[ATTR_RGB_COLOR] = rgb if transition is not None: service_data[ATTR_TRANSITION] = transition hass.services.call( LIGHT_DOMAIN, SERVICE_TURN_ON, service_data)
def test_on_brightness(self): """Test turning the light on with brightness.""" assert self.light().state == 'off' assert self.other_light().state == 'off' assert not light.is_on(self.hass, ENTITY_LIGHT) light.turn_on(self.hass, ENTITY_LIGHT, brightness=102) self.hass.block_till_done() self.mock_lj.activate_load_at.assert_called_with( ENTITY_LIGHT_NUMBER, 39, 0)
def test_light_profiles_with_transition(self): """Test light profiles with transition.""" platform = getattr(self.hass.components, "test.light") platform.init() user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE) with open(user_light_file, "w") as user_file: user_file.write("id,x,y,brightness,transition\n") user_file.write("test,.4,.6,100,2\n") user_file.write("test_off,0,0,0,0\n") assert setup_component(self.hass, light.DOMAIN, {light.DOMAIN: { CONF_PLATFORM: "test" }}) self.hass.block_till_done() ent1, _, _ = platform.ENTITIES common.turn_on(self.hass, ent1.entity_id, profile="test") self.hass.block_till_done() _, data = ent1.last_call("turn_on") assert light.is_on(self.hass, ent1.entity_id) assert { light.ATTR_HS_COLOR: (71.059, 100), light.ATTR_BRIGHTNESS: 100, light.ATTR_TRANSITION: 2, } == data common.turn_on(self.hass, ent1.entity_id, profile="test_off") self.hass.block_till_done() _, data = ent1.last_call("turn_off") assert not light.is_on(self.hass, ent1.entity_id) assert {light.ATTR_TRANSITION: 0} == data
def set_lights_temp(hass, lights, mired, brightness, transition): """Set color of array of lights.""" for light in lights: if is_on(hass, light): service_data = {ATTR_ENTITY_ID: light} if mired is not None: service_data[ATTR_COLOR_TEMP] = int(mired) if brightness is not None: service_data[ATTR_BRIGHTNESS] = brightness if transition is not None: service_data[ATTR_TRANSITION] = transition hass.services.call(LIGHT_DOMAIN, SERVICE_TURN_ON, service_data)
async def test_lights_turn_off_when_everyone_leaves(hass, scanner): """Test lights turn off when everyone leaves the house.""" await common_light.async_turn_on(hass) assert await async_setup_component(hass, device_sun_light_trigger.DOMAIN, {device_sun_light_trigger.DOMAIN: {}}) hass.states.async_set(device_tracker.ENTITY_ID_ALL_DEVICES, STATE_NOT_HOME) await hass.async_block_till_done() assert not light.is_on(hass)
def set_lights_temp(hass, lights, mired, brightness, transition): """Set color of array of lights.""" for light in lights: if is_on(hass, light): service_data = {ATTR_ENTITY_ID: light} if mired is not None: service_data[ATTR_COLOR_TEMP] = int(mired) if brightness is not None: service_data[ATTR_BRIGHTNESS] = brightness if transition is not None: service_data[ATTR_TRANSITION] = transition hass.services.call( LIGHT_DOMAIN, SERVICE_TURN_ON, service_data)
def test_light_profiles(self): """Test light profiles.""" platform = getattr(self.hass.components, 'test.light') platform.init() user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE) with open(user_light_file, 'w') as user_file: user_file.write('id,x,y,brightness\n') user_file.write('test,.4,.6,100\n') user_file.write('test_off,0,0,0\n') assert setup_component(self.hass, light.DOMAIN, {light.DOMAIN: { CONF_PLATFORM: 'test' }}) dev1, _, _ = platform.DEVICES common.turn_on(self.hass, dev1.entity_id, profile='test') self.hass.block_till_done() _, data = dev1.last_call('turn_on') assert light.is_on(self.hass, dev1.entity_id) assert { light.ATTR_HS_COLOR: (71.059, 100), light.ATTR_BRIGHTNESS: 100 } == data common.turn_on(self.hass, dev1.entity_id, profile='test_off') self.hass.block_till_done() _, data = dev1.last_call('turn_off') assert not light.is_on(self.hass, dev1.entity_id) assert {} == data
async def test_activate_scene(hass, entities, enable_custom_integrations): """Test active scene.""" light_1, light_2 = await setup_lights(hass, entities) assert await async_setup_component( hass, scene.DOMAIN, { "scene": [ { "name": "test", "entities": { light_1.entity_id: "on", light_2.entity_id: {"state": "on", "brightness": 100}, }, } ] }, ) await hass.async_block_till_done() await activate(hass, "scene.test") assert light.is_on(hass, light_1.entity_id) assert light.is_on(hass, light_2.entity_id) assert light_2.last_call("turn_on")[1].get("brightness") == 100 await turn_off_lights(hass, [light_2.entity_id]) calls = async_mock_service(hass, "light", "turn_on") await hass.services.async_call( scene.DOMAIN, "turn_on", {"transition": 42, "entity_id": "scene.test"} ) await hass.async_block_till_done() assert len(calls) == 1 assert calls[0].domain == "light" assert calls[0].service == "turn_on" assert calls[0].data.get("transition") == 42
async def setup_lights(hass, entities): """Set up the light component.""" assert await async_setup_component(hass, light.DOMAIN, {light.DOMAIN: { "platform": "test" }}) await hass.async_block_till_done() light_1, light_2 = entities await hass.services.async_call( "light", "turn_off", {"entity_id": [light_1.entity_id, light_2.entity_id]}, blocking=True, ) await hass.async_block_till_done() assert not light.is_on(hass, light_1.entity_id) assert not light.is_on(hass, light_2.entity_id) return light_1, light_2
def set_lights_xy(hass, lights, x_val, y_val, brightness, transition): """Set color of array of lights.""" for light in lights: if is_on(hass, light): service_data = {ATTR_ENTITY_ID: light} if x_val is not None and y_val is not None: service_data[ATTR_XY_COLOR] = [x_val, y_val] if brightness is not None: service_data[ATTR_BRIGHTNESS] = brightness service_data[ATTR_WHITE_VALUE] = brightness if transition is not None: service_data[ATTR_TRANSITION] = transition hass.services.call(LIGHT_DOMAIN, SERVICE_TURN_ON, service_data)
async def test_config_yaml_bool(hass, entities, enable_custom_integrations): """Test parsing of booleans in yaml config.""" light_1, light_2 = await setup_lights(hass, entities) config = ("scene:\n" " - name: test\n" " entities:\n" f" {light_1.entity_id}: on\n" f" {light_2.entity_id}:\n" " state: on\n" " brightness: 100\n") with io.StringIO(config) as file: doc = yaml_loader.yaml.safe_load(file) assert await async_setup_component(hass, scene.DOMAIN, doc) await hass.async_block_till_done() await activate(hass, "scene.test") assert light.is_on(hass, light_1.entity_id) assert light.is_on(hass, light_2.entity_id) assert light_2.last_call("turn_on")[1].get("brightness") == 100
def set_lights_xy(hass, lights, x_val, y_val, brightness, transition): """Set color of array of lights.""" for light in lights: if is_on(hass, light): service_data = {ATTR_ENTITY_ID: light} if x_val is not None and y_val is not None: service_data[ATTR_XY_COLOR] = [x_val, y_val] if brightness is not None: service_data[ATTR_BRIGHTNESS] = brightness service_data[ATTR_WHITE_VALUE] = brightness if transition is not None: service_data[ATTR_TRANSITION] = transition hass.services.call( LIGHT_DOMAIN, SERVICE_TURN_ON, service_data)
def test_on_off(self): """Test turning the light on and off.""" assert self.light().state == 'off' assert self.other_light().state == 'off' assert not light.is_on(self.hass, ENTITY_LIGHT) common.turn_on(self.hass, ENTITY_LIGHT) self.hass.block_till_done() self.mock_lj.activate_load.assert_called_with(ENTITY_LIGHT_NUMBER) common.turn_off(self.hass, ENTITY_LIGHT) self.hass.block_till_done() self.mock_lj.deactivate_load.assert_called_with(ENTITY_LIGHT_NUMBER)
def test_on_off(self): """Test turning the light on and off.""" assert self.light().state == 'off' assert self.other_light().state == 'off' assert not light.is_on(self.hass, ENTITY_LIGHT) light.turn_on(self.hass, ENTITY_LIGHT) self.hass.block_till_done() self.mock_lj.activate_load.assert_called_with(ENTITY_LIGHT_NUMBER) light.turn_off(self.hass, ENTITY_LIGHT) self.hass.block_till_done() self.mock_lj.deactivate_load.assert_called_with(ENTITY_LIGHT_NUMBER)
async def test_lights_turn_on_when_coming_home_after_sun_set(hass, scanner): """Test lights turn on when coming home after sun set.""" test_time = datetime(2017, 4, 5, 3, 2, 3, tzinfo=dt_util.UTC) with patch("homeassistant.util.dt.utcnow", return_value=test_time): await common_light.async_turn_off(hass) assert await async_setup_component( hass, device_sun_light_trigger.DOMAIN, {device_sun_light_trigger.DOMAIN: {}} ) hass.states.async_set(DT_ENTITY_ID_FORMAT.format("device_2"), STATE_HOME) await hass.async_block_till_done() assert light.is_on(hass)
def test_lights_on_when_sun_sets(self): """Test lights go on when there is someone home and the sun sets.""" self.assertTrue(device_sun_light_trigger.setup( self.hass, {device_sun_light_trigger.DOMAIN: {}})) ensure_sun_risen(self.hass) light.turn_off(self.hass) self.hass.pool.block_till_done() ensure_sun_set(self.hass) self.hass.pool.block_till_done() self.assertTrue(light.is_on(self.hass))
def test_activated_event(self): """Test handling an event from LiteJet.""" self.mock_lj.get_load_level.return_value = 99 # Light 1 _LOGGER.info(self.load_activated_callbacks[ENTITY_LIGHT_NUMBER]) self.load_activated_callbacks[ENTITY_LIGHT_NUMBER]() self.hass.block_till_done() self.mock_lj.get_load_level.assert_called_once_with( ENTITY_LIGHT_NUMBER) assert light.is_on(self.hass, ENTITY_LIGHT) assert not light.is_on(self.hass, ENTITY_OTHER_LIGHT) assert self.light().state == 'on' assert self.other_light().state == 'off' assert self.light().attributes.get(light.ATTR_BRIGHTNESS) == 255 # Light 2 self.mock_lj.get_load_level.return_value = 40 self.mock_lj.get_load_level.reset_mock() self.load_activated_callbacks[ENTITY_OTHER_LIGHT_NUMBER]() self.hass.block_till_done() self.mock_lj.get_load_level.assert_called_once_with( ENTITY_OTHER_LIGHT_NUMBER) assert light.is_on(self.hass, ENTITY_OTHER_LIGHT) assert light.is_on(self.hass, ENTITY_LIGHT) assert self.light().state == 'on' assert self.other_light().state == 'on' assert int(self.other_light().attributes[light.ATTR_BRIGHTNESS]) == 103
def test_lights_turn_on_when_coming_home_after_sun_set(self): """Test lights turn on when coming home after sun set.""" test_time = datetime(2017, 4, 5, 3, 2, 3, tzinfo=dt_util.UTC) with patch('homeassistant.util.dt.utcnow', return_value=test_time): common_light.turn_off(self.hass) self.hass.block_till_done() assert setup_component(self.hass, device_sun_light_trigger.DOMAIN, {device_sun_light_trigger.DOMAIN: {}}) self.hass.states.set( device_tracker.ENTITY_ID_FORMAT.format('device_2'), STATE_HOME) self.hass.block_till_done() assert light.is_on(self.hass)
def test_lights_turn_off_when_everyone_leaves(self): """ Test lights turn off when everyone leaves the house. """ light.turn_on(self.hass) self.hass.pool.block_till_done() self.assertTrue(device_sun_light_trigger.setup( self.hass, {device_sun_light_trigger.DOMAIN: {}})) self.hass.states.set(device_tracker.ENTITY_ID_ALL_DEVICES, STATE_NOT_HOME) self.hass.pool.block_till_done() self.assertFalse(light.is_on(self.hass))
def test_lights_turn_on_when_coming_home_after_sun_set(self): """Test lights turn on when coming home after sun set.""" light.turn_off(self.hass) ensure_sun_set(self.hass) self.hass.pool.block_till_done() self.assertTrue(device_sun_light_trigger.setup( self.hass, {device_sun_light_trigger.DOMAIN: {}})) self.hass.states.set( device_tracker.ENTITY_ID_FORMAT.format('device_2'), STATE_HOME) self.hass.pool.block_till_done() self.assertTrue(light.is_on(self.hass))
def test_lights_turn_off_when_everyone_leaves(self): """Test lights turn off when everyone leaves the house.""" light.turn_on(self.hass) self.hass.pool.block_till_done() self.assertTrue(device_sun_light_trigger.setup( self.hass, {device_sun_light_trigger.DOMAIN: {}})) self.hass.states.set(device_tracker.ENTITY_ID_ALL_DEVICES, STATE_NOT_HOME) self.hass.pool.block_till_done() self.assertFalse(light.is_on(self.hass))
async def test_activated_event(hass, mock_litejet): """Test handling an event from LiteJet.""" await async_init_integration(hass) # Light 1 mock_litejet.get_load_level.return_value = 99 mock_litejet.get_load_level.reset_mock() mock_litejet.load_activated_callbacks[ENTITY_LIGHT_NUMBER]() await hass.async_block_till_done() mock_litejet.get_load_level.assert_called_once_with(ENTITY_LIGHT_NUMBER) assert light.is_on(hass, ENTITY_LIGHT) assert not light.is_on(hass, ENTITY_OTHER_LIGHT) assert hass.states.get(ENTITY_LIGHT).state == "on" assert hass.states.get(ENTITY_OTHER_LIGHT).state == "off" assert hass.states.get(ENTITY_LIGHT).attributes.get(ATTR_BRIGHTNESS) == 255 # Light 2 mock_litejet.get_load_level.return_value = 40 mock_litejet.get_load_level.reset_mock() mock_litejet.load_activated_callbacks[ENTITY_OTHER_LIGHT_NUMBER]() await hass.async_block_till_done() mock_litejet.get_load_level.assert_called_once_with( ENTITY_OTHER_LIGHT_NUMBER) assert light.is_on(hass, ENTITY_LIGHT) assert light.is_on(hass, ENTITY_OTHER_LIGHT) assert hass.states.get(ENTITY_LIGHT).state == "on" assert hass.states.get(ENTITY_OTHER_LIGHT).state == "on" assert (int( hass.states.get(ENTITY_OTHER_LIGHT).attributes.get(ATTR_BRIGHTNESS)) == 103)
def test_lights_turn_off_when_everyone_leaves(self): """Test lights turn off when everyone leaves the house.""" common_light.turn_on(self.hass) self.hass.block_till_done() assert setup_component(self.hass, device_sun_light_trigger.DOMAIN, {device_sun_light_trigger.DOMAIN: {}}) self.hass.states.set(device_tracker.ENTITY_ID_ALL_DEVICES, STATE_NOT_HOME) self.hass.block_till_done() assert not light.is_on(self.hass)
def test_lights_turn_on_when_coming_home_after_sun_set(self): """Test lights turn on when coming home after sun set.""" test_time = datetime(2017, 4, 5, 3, 2, 3, tzinfo=dt_util.UTC) with patch('homeassistant.util.dt.utcnow', return_value=test_time): light.turn_off(self.hass) self.hass.block_till_done() self.assertTrue(setup_component( self.hass, device_sun_light_trigger.DOMAIN, { device_sun_light_trigger.DOMAIN: {}})) self.hass.states.set( device_tracker.ENTITY_ID_FORMAT.format('device_2'), STATE_HOME) self.hass.block_till_done() self.assertTrue(light.is_on(self.hass))
async def test_lights_turn_off_when_everyone_leaves(hass, scanner): """Test lights turn off when everyone leaves the house.""" common_light.async_turn_on(hass) await hass.async_block_till_done() assert await async_setup_component( hass, device_sun_light_trigger.DOMAIN, { device_sun_light_trigger.DOMAIN: {}}) hass.states.async_set(device_tracker.ENTITY_ID_ALL_DEVICES, STATE_NOT_HOME) await hass.async_block_till_done() assert not light.is_on(hass)
def test_lights_turn_off_when_everyone_leaves(self): """ Test lights turn off when everyone leaves the house. """ light.turn_on(self.hass) self.hass.pool.block_till_done() device_sun_light_trigger.setup( self.hass, {device_sun_light_trigger.DOMAIN: {}}) self.scanner.leave_home('DEV1') trigger_device_tracker_scan(self.hass) self.hass.pool.block_till_done() self.assertFalse(light.is_on(self.hass))
async def test_lights_turn_on_when_coming_home_after_sun_set(hass, scanner): """Test lights turn on when coming home after sun set.""" test_time = datetime(2017, 4, 5, 3, 2, 3, tzinfo=dt_util.UTC) with patch('homeassistant.util.dt.utcnow', return_value=test_time): common_light.async_turn_off(hass) await hass.async_block_till_done() assert await async_setup_component( hass, device_sun_light_trigger.DOMAIN, { device_sun_light_trigger.DOMAIN: {}}) hass.states.async_set( device_tracker.ENTITY_ID_FORMAT.format('device_2'), STATE_HOME) await hass.async_block_till_done() assert light.is_on(hass)
def test_lights_turn_on_when_coming_home_after_sun_set(self): """ Test lights turn on when coming home after sun set. """ light.turn_off(self.hass) ensure_sun_set(self.hass) self.hass.pool.block_till_done() device_sun_light_trigger.setup( self.hass, {device_sun_light_trigger.DOMAIN: {}}) self.scanner.come_home('DEV2') trigger_device_tracker_scan(self.hass) self.hass.pool.block_till_done() self.assertTrue(light.is_on(self.hass))
def test_lights_turn_off_when_everyone_leaves(self): \ # pylint: disable=invalid-name """Test lights turn off when everyone leaves the house.""" light.turn_on(self.hass) self.hass.block_till_done() self.assertTrue(setup_component( self.hass, device_sun_light_trigger.DOMAIN, { device_sun_light_trigger.DOMAIN: {}})) self.hass.states.set(device_tracker.ENTITY_ID_ALL_DEVICES, STATE_NOT_HOME) self.hass.block_till_done() self.assertFalse(light.is_on(self.hass))
def test_lights_turn_on_when_coming_home_after_sun_set(self): # pylint: disable=invalid-name """Test lights turn on when coming home after sun set.""" light.turn_off(self.hass) ensure_sun_set(self.hass) self.hass.block_till_done() self.assertTrue( setup_component(self.hass, device_sun_light_trigger.DOMAIN, {device_sun_light_trigger.DOMAIN: {}}) ) self.hass.states.set(device_tracker.ENTITY_ID_FORMAT.format("device_2"), STATE_HOME) self.hass.block_till_done() self.assertTrue(light.is_on(self.hass))
def test_lights_on_when_sun_sets(self): """Test lights go on when there is someone home and the sun sets.""" test_time = datetime(2017, 4, 5, 1, 2, 3, tzinfo=dt_util.UTC) with patch('homeassistant.util.dt.utcnow', return_value=test_time): self.assertTrue(setup_component( self.hass, device_sun_light_trigger.DOMAIN, { device_sun_light_trigger.DOMAIN: {}})) light.turn_off(self.hass) self.hass.block_till_done() test_time = test_time.replace(hour=3) with patch('homeassistant.util.dt.utcnow', return_value=test_time): fire_time_changed(self.hass, test_time) self.hass.block_till_done() self.assertTrue(light.is_on(self.hass))
async def test_lights_on_when_sun_sets(hass, scanner): """Test lights go on when there is someone home and the sun sets.""" test_time = datetime(2017, 4, 5, 1, 2, 3, tzinfo=dt_util.UTC) with patch('homeassistant.util.dt.utcnow', return_value=test_time): assert await async_setup_component( hass, device_sun_light_trigger.DOMAIN, { device_sun_light_trigger.DOMAIN: {}}) common_light.async_turn_off(hass) await hass.async_block_till_done() test_time = test_time.replace(hour=3) with patch('homeassistant.util.dt.utcnow', return_value=test_time): async_fire_time_changed(hass, test_time) await hass.async_block_till_done() assert light.is_on(hass)