def test_sunset_trigger_with_offset(self): """Test the sunset trigger with offset.""" self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, { sun.STATE_ATTR_NEXT_SETTING: '2015-09-16T02:00:00Z', }) now = datetime(2015, 9, 15, 23, tzinfo=dt_util.UTC) trigger_time = datetime(2015, 9, 16, 2, 30, tzinfo=dt_util.UTC) with patch('blumate.util.dt.utcnow', return_value=now): _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'sun', 'event': 'sunset', 'offset': '0:30:00' }, 'action': { 'service': 'test.automation', 'data_template': { 'some': '{{ trigger.%s }}' % '}} - {{ trigger.'.join(( 'platform', 'event', 'offset')) }, } } }) fire_time_changed(self.hass, trigger_time) self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.assertEqual('sun - sunset - 0:30:00', self.calls[0].data['some'])
def test_sunrise_trigger(self): """Test the sunrise trigger.""" self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, { sun.STATE_ATTR_NEXT_RISING: '2015-09-16T14:00:00Z', }) now = datetime(2015, 9, 13, 23, tzinfo=dt_util.UTC) trigger_time = datetime(2015, 9, 16, 14, tzinfo=dt_util.UTC) with patch('blumate.util.dt.utcnow', return_value=now): _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'sun', 'event': 'sunrise', }, 'action': { 'service': 'test.automation', } } }) fire_time_changed(self.hass, trigger_time) self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_validate_component_config(self): """Test validating component configuration.""" config_schema = vol.Schema({'comp_conf': { 'hello': str }}, required=True) loader.set_component( 'comp_conf', MockModule('comp_conf', config_schema=config_schema)) assert not bootstrap._setup_component(self.hass, 'comp_conf', {}) assert not bootstrap._setup_component(self.hass, 'comp_conf', {'comp_conf': None}) assert not bootstrap._setup_component(self.hass, 'comp_conf', {'comp_conf': {}}) assert not bootstrap._setup_component( self.hass, 'comp_conf', {'comp_conf': { 'hello': 'world', 'invalid': 'extra', }}) assert bootstrap._setup_component(self.hass, 'comp_conf', {'comp_conf': { 'hello': 'world', }})
def test_default_config(self): """Test configuration.""" self.assertTrue( _setup_component( self.hass, 'rfxtrx', { 'rfxtrx': { 'device': '/dev/serial/by-id/usb' + '-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0', 'dummy': True } })) self.assertTrue( _setup_component( self.hass, 'sensor', { 'sensor': { 'platform': 'rfxtrx', 'automatic_add': True, 'devices': {} } })) while len(rfxtrx.RFX_DEVICES) < 2: time.sleep(0.1) self.assertEqual(len(rfxtrx.RFXOBJECT.sensors()), 2)
def test_validate_component_config(self): """Test validating component configuration.""" config_schema = vol.Schema({ 'comp_conf': { 'hello': str } }, required=True) loader.set_component( 'comp_conf', MockModule('comp_conf', config_schema=config_schema)) assert not bootstrap._setup_component(self.hass, 'comp_conf', {}) assert not bootstrap._setup_component(self.hass, 'comp_conf', { 'comp_conf': None }) assert not bootstrap._setup_component(self.hass, 'comp_conf', { 'comp_conf': {} }) assert not bootstrap._setup_component(self.hass, 'comp_conf', { 'comp_conf': { 'hello': 'world', 'invalid': 'extra', } }) assert bootstrap._setup_component(self.hass, 'comp_conf', { 'comp_conf': { 'hello': 'world', } })
def test_if_action_after(self): """Test if action was after.""" self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, { sun.STATE_ATTR_NEXT_RISING: '2015-09-16T14:00:00Z', }) _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'condition': { 'platform': 'sun', 'after': 'sunrise', }, 'action': { 'service': 'test.automation' } } }) now = datetime(2015, 9, 16, 13, tzinfo=dt_util.UTC) with patch('blumate.util.dt.now', return_value=now): self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls)) now = datetime(2015, 9, 16, 15, tzinfo=dt_util.UTC) with patch('blumate.util.dt.now', return_value=now): self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def mock_mqtt_component(hass, mock_mqtt): """Mock the MQTT component.""" _setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: { mqtt.CONF_BROKER: 'mock-broker', }}) return mock_mqtt
def mock_mqtt_component(hass, mock_mqtt): """Mock the MQTT component.""" _setup_component(hass, mqtt.DOMAIN, { mqtt.DOMAIN: { mqtt.CONF_BROKER: 'mock-broker', } }) return mock_mqtt
def test_fire_event(self): """Test fire event.""" self.assertTrue( _setup_component( self.hass, 'rfxtrx', { 'rfxtrx': { 'device': '/dev/serial/by-id/usb' + '-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0', 'dummy': True } })) self.assertTrue( _setup_component( self.hass, 'switch', { 'switch': { 'platform': 'rfxtrx', 'automatic_add': True, 'devices': { '0b1100cd0213c7f210010f51': { 'name': 'Test', rfxtrx.ATTR_FIREEVENT: True } } } })) calls = [] def record_event(event): """Add recorded event to set.""" calls.append(event) self.hass.bus.listen(rfxtrx.EVENT_BUTTON_PRESSED, record_event) entity = rfxtrx.RFX_DEVICES['213c7f216'] self.assertEqual('Test', entity.name) self.assertEqual('off', entity.state) self.assertTrue(entity.should_fire_event) event = rfxtrx.get_rfx_object('0b1100cd0213c7f210010f51') event.data = bytearray([ 0x0b, 0x11, 0x00, 0x10, 0x01, 0x18, 0xcd, 0xea, 0x01, 0x01, 0x0f, 0x70 ]) rfxtrx.RECEIVED_EVT_SUBSCRIBERS[0](event) self.hass.pool.block_till_done() self.assertEqual(event.values['Command'], "On") self.assertEqual('on', entity.state) self.assertEqual(1, len(rfxtrx.RFX_DEVICES)) self.assertEqual(1, len(calls)) self.assertEqual(calls[0].data, { 'entity_id': 'switch.test', 'state': 'on' })
def test_component_not_setup_missing_dependencies(self): """Test we do not setup a component if not all dependencies loaded.""" deps = ['non_existing'] loader.set_component('comp', MockModule('comp', dependencies=deps)) assert not bootstrap._setup_component(self.hass, 'comp', None) assert 'comp' not in self.hass.config.components self.hass.config.components.append('non_existing') assert bootstrap._setup_component(self.hass, 'comp', None)
def test_component_not_setup_missing_dependencies(self): """Test we do not setup a component if not all dependencies loaded.""" deps = ['non_existing'] loader.set_component('comp', MockModule('comp', dependencies=deps)) assert not bootstrap._setup_component(self.hass, 'comp', None) assert 'comp' not in self.hass.config.components self.hass.config.components.append('non_existing') assert bootstrap._setup_component(self.hass, 'comp', None)
def test_invalid_config(self): """Test configuration.""" self.assertFalse(_setup_component(self.hass, 'rfxtrx', {'rfxtrx': {}})) self.assertFalse( _setup_component( self.hass, 'rfxtrx', { 'rfxtrx': { 'device': '/dev/serial/by-id/usb' + '-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0', 'invalid_key': True } }))
def test_if_fires_on_for_condition(self): """Test for firing if contition is on.""" point1 = dt_util.utcnow() point2 = point1 + timedelta(seconds=10) with patch('blumate.core.dt_util.utcnow') as mock_utcnow: mock_utcnow.return_value = point1 self.hass.states.set('test.entity', 'on') assert _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'condition': { 'platform': 'state', 'entity_id': 'test.entity', 'state': 'on', 'for': { 'seconds': 5 }, }, 'action': {'service': 'test.automation'}, } }) # not enough time has passed self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls)) # Time travel 10 secs into the future mock_utcnow.return_value = point2 self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_controlling_state_via_topic_and_json_message(self): """Test the controlling state via topic and JSON message.""" self.hass.config.components = ['mqtt'] assert _setup_component( self.hass, lock.DOMAIN, { lock.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'payload_lock': 'LOCK', 'payload_unlock': 'UNLOCK', 'value_template': '{{ value_json.val }}' } }) state = self.hass.states.get('lock.test') self.assertEqual(STATE_UNLOCKED, state.state) fire_mqtt_message(self.hass, 'state-topic', '{"val":"LOCK"}') self.hass.pool.block_till_done() state = self.hass.states.get('lock.test') self.assertEqual(STATE_LOCKED, state.state) fire_mqtt_message(self.hass, 'state-topic', '{"val":"UNLOCK"}') self.hass.pool.block_till_done() state = self.hass.states.get('lock.test') self.assertEqual(STATE_UNLOCKED, state.state)
def test_automation_list_setting(self): """Event is not a valid condition.""" self.assertTrue( _setup_component( self.hass, automation.DOMAIN, { automation.DOMAIN: [{ 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'action': { 'service': 'test.automation', } }, { 'trigger': { 'platform': 'event', 'event_type': 'test_event_2', }, 'action': { 'service': 'test.automation', } }] })) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.hass.bus.fire('test_event_2') self.hass.pool.block_till_done() self.assertEqual(2, len(self.calls))
def test_several_rollershutters(self): """Test with 3 roller shutters.""" self.assertTrue(_setup_component(self.hass, 'rollershutter', { 'rollershutter': {'platform': 'rfxtrx', 'signal_repetitions': 3, 'devices': {'0b1100cd0213c7f230010f71': { 'name': 'Test'}, '0b1100100118cdea02010f70': { 'name': 'Bath'}, '0b1100101118cdea02010f70': { 'name': 'Living'} }}})) self.assertEqual(3, len(rfxtrx_core.RFX_DEVICES)) device_num = 0 for id in rfxtrx_core.RFX_DEVICES: entity = rfxtrx_core.RFX_DEVICES[id] self.assertEqual(entity.signal_repetitions, 3) if entity.name == 'Living': device_num = device_num + 1 elif entity.name == 'Bath': device_num = device_num + 1 elif entity.name == 'Test': device_num = device_num + 1 self.assertEqual(3, device_num)
def test_controlling_state_via_topic(self): """Test the controlling of the state via topic.""" self.hass.config.components = ['mqtt'] assert _setup_component(self.hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'test_light_rgb/status', 'command_topic': 'test_light_rgb/set', 'brightness_state_topic': 'test_light_rgb/brightness/status', 'brightness_command_topic': 'test_light_rgb/brightness/set', 'rgb_state_topic': 'test_light_rgb/rgb/status', 'rgb_command_topic': 'test_light_rgb/rgb/set', 'qos': '0', 'payload_on': 1, 'payload_off': 0 } }) state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) self.assertIsNone(state.attributes.get('rgb_color')) self.assertIsNone(state.attributes.get('brightness')) self.assertIsNone(state.attributes.get(ATTR_ASSUMED_STATE)) fire_mqtt_message(self.hass, 'test_light_rgb/status', '1') self.hass.pool.block_till_done() state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertEqual([255, 255, 255], state.attributes.get('rgb_color')) self.assertEqual(255, state.attributes.get('brightness')) fire_mqtt_message(self.hass, 'test_light_rgb/status', '0') self.hass.pool.block_till_done() state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) fire_mqtt_message(self.hass, 'test_light_rgb/status', '1') self.hass.pool.block_till_done() fire_mqtt_message(self.hass, 'test_light_rgb/brightness/status', '100') self.hass.pool.block_till_done() light_state = self.hass.states.get('light.test') self.hass.pool.block_till_done() self.assertEqual(100, light_state.attributes['brightness']) fire_mqtt_message(self.hass, 'test_light_rgb/status', '1') self.hass.pool.block_till_done() fire_mqtt_message(self.hass, 'test_light_rgb/rgb/status', '125,125,125') self.hass.pool.block_till_done() light_state = self.hass.states.get('light.test') self.assertEqual([125, 125, 125], light_state.attributes.get('rgb_color'))
def test_controlling_state_via_topic_and_json_message(self): """Test the controlling state via topic and JSON message.""" self.hass.config.components = ['mqtt'] assert _setup_component(self.hass, lock.DOMAIN, { lock.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'payload_lock': 'LOCK', 'payload_unlock': 'UNLOCK', 'value_template': '{{ value_json.val }}' } }) state = self.hass.states.get('lock.test') self.assertEqual(STATE_UNLOCKED, state.state) fire_mqtt_message(self.hass, 'state-topic', '{"val":"LOCK"}') self.hass.pool.block_till_done() state = self.hass.states.get('lock.test') self.assertEqual(STATE_LOCKED, state.state) fire_mqtt_message(self.hass, 'state-topic', '{"val":"UNLOCK"}') self.hass.pool.block_till_done() state = self.hass.states.get('lock.test') self.assertEqual(STATE_UNLOCKED, state.state)
def test_controlling_state_via_topic(self): """Test the controlling state via topic.""" assert _setup_component( self.hass, garage_door.DOMAIN, { garage_door.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'state_open': 1, 'state_closed': 0, 'service_open': 1, 'service_close': 0 } }) state = self.hass.states.get('garage_door.test') self.assertEqual(STATE_CLOSED, state.state) self.assertIsNone(state.attributes.get(ATTR_ASSUMED_STATE)) fire_mqtt_message(self.hass, 'state-topic', '1') self.hass.pool.block_till_done() state = self.hass.states.get('garage_door.test') self.assertEqual(STATE_OPEN, state.state) fire_mqtt_message(self.hass, 'state-topic', '0') self.hass.pool.block_till_done() state = self.hass.states.get('garage_door.test') self.assertEqual(STATE_CLOSED, state.state)
def test_several_rollershutters(self): """Test with 3 roller shutters.""" self.assertTrue( _setup_component( self.hass, 'rollershutter', { 'rollershutter': { 'platform': 'rfxtrx', 'signal_repetitions': 3, 'devices': { '0b1100cd0213c7f230010f71': { 'name': 'Test' }, '0b1100100118cdea02010f70': { 'name': 'Bath' }, '0b1100101118cdea02010f70': { 'name': 'Living' } } } })) self.assertEqual(3, len(rfxtrx_core.RFX_DEVICES)) device_num = 0 for id in rfxtrx_core.RFX_DEVICES: entity = rfxtrx_core.RFX_DEVICES[id] self.assertEqual(entity.signal_repetitions, 3) if entity.name == 'Living': device_num = device_num + 1 elif entity.name == 'Bath': device_num = device_num + 1 elif entity.name == 'Test': device_num = device_num + 1 self.assertEqual(3, device_num)
def test_automation_calling_two_actions(self): """Test if we can call two actions from automation definition.""" self.assertTrue(_setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'action': [{ 'service': 'test.automation', 'data': {'position': 0}, }, { 'service': 'test.automation', 'data': {'position': 1}, }], } })) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() assert len(self.calls) == 2 assert self.calls[0].data['position'] == 0 assert self.calls[1].data['position'] == 1
def test_if_action_one_weekday(self): """Test for if action with one weekday.""" assert _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event' }, 'condition': { 'platform': 'time', 'weekday': 'mon', }, 'action': { 'service': 'test.automation' } } }) days_past_monday = dt_util.now().weekday() monday = dt_util.now() - timedelta(days=days_past_monday) tuesday = monday + timedelta(days=1) with patch('blumate.helpers.condition.dt_util.now', return_value=monday): self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) with patch('blumate.helpers.condition.dt_util.now', return_value=tuesday): self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_using_trigger_as_condition_with_invalid_condition(self): """Event is not a valid condition.""" entity_id = 'test.entity' self.hass.states.set(entity_id, 100) assert _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': [ { 'platform': 'event', 'event_type': 'test_event', }, { 'platform': 'numeric_state', 'entity_id': entity_id, 'below': 150 } ], 'condition': 'use_trigger_values', 'action': { 'service': 'test.automation', } } }) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_config_not_valid_service_names(self): """Test if config contains invalid service names.""" assert not _setup_component(self.hass, shell_command.DOMAIN, { shell_command.DOMAIN: { 'this is invalid because space': 'touch bla.txt' } })
def test_template_string(self): """"Test template string.""" assert _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'numeric_state', 'entity_id': 'test.entity', 'value_template': '{{ state.attributes.test_attribute | multiply(10) }}', 'below': 10, }, 'action': { 'service': 'test.automation', 'data_template': { 'some': '{{ trigger.%s }}' % '}} - {{ trigger.'.join(( 'platform', 'entity_id', 'below', 'above', 'from_state.state', 'to_state.state')) }, } } }) self.hass.states.set('test.entity', 'test state 1', {'test_attribute': '1.2'}) self.hass.pool.block_till_done() self.hass.states.set('test.entity', 'test state 2', {'test_attribute': '0.9'}) self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.assertEqual( 'numeric_state - test.entity - 10.0 - None - test state 1 - ' 'test state 2', self.calls[0].data['some'])
def test_discover_sensor_noautoadd(self): """Test with discover of sensor when auto add is False.""" self.assertTrue(_setup_component(self.hass, 'sensor', { 'sensor': {'platform': 'rfxtrx', 'automatic_add': False, 'devices': {}}})) event = rfxtrx_core.get_rfx_object('0a520801070100b81b0279') event.data = bytearray(b'\nR\x08\x01\x07\x01\x00\xb8\x1b\x02y') self.assertEqual(0, len(rfxtrx_core.RFX_DEVICES)) rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event) self.assertEqual(0, len(rfxtrx_core.RFX_DEVICES)) rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event) self.assertEqual(0, len(rfxtrx_core.RFX_DEVICES)) event = rfxtrx_core.get_rfx_object('0a52080405020095240279') event.data = bytearray(b'\nR\x08\x04\x05\x02\x00\x95$\x02y') rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event) self.assertEqual(0, len(rfxtrx_core.RFX_DEVICES)) event = rfxtrx_core.get_rfx_object('0a52085e070100b31b0279') event.data = bytearray(b'\nR\x08^\x07\x01\x00\xb3\x1b\x02y') rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event) self.assertEqual(0, len(rfxtrx_core.RFX_DEVICES))
def test_turn_on_service(self): """Verify that the turn_on service.""" event = 'test_event' events = [] def record_event(event): """Add recorded event to set.""" events.append(event) self.hass.bus.listen(event, record_event) assert _setup_component(self.hass, 'script', { 'script': { 'test': { 'sequence': [{ 'delay': { 'seconds': 5 } }, { 'event': event, }] } } }) script.turn_on(self.hass, ENTITY_ID) self.hass.pool.block_till_done() self.assertTrue(script.is_on(self.hass, ENTITY_ID)) self.assertEqual(0, len(events)) # Calling turn_on a second time should not advance the script script.turn_on(self.hass, ENTITY_ID) self.hass.pool.block_till_done() self.assertEqual(0, len(events))
def test_default_config(self): """Test with 0 sensor.""" self.assertTrue(_setup_component(self.hass, 'sensor', { 'sensor': {'platform': 'rfxtrx', 'devices': {}}})) self.assertEqual(0, len(rfxtrx_core.RFX_DEVICES))
def test_controlling_state_via_topic(self): """Test the controlling state via topic.""" self.hass.config.components = ['mqtt'] assert _setup_component(self.hass, lock.DOMAIN, { lock.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'payload_lock': 'LOCK', 'payload_unlock': 'UNLOCK' } }) state = self.hass.states.get('lock.test') self.assertEqual(STATE_UNLOCKED, state.state) self.assertIsNone(state.attributes.get(ATTR_ASSUMED_STATE)) fire_mqtt_message(self.hass, 'state-topic', 'LOCK') self.hass.pool.block_till_done() state = self.hass.states.get('lock.test') self.assertEqual(STATE_LOCKED, state.state) fire_mqtt_message(self.hass, 'state-topic', 'UNLOCK') self.hass.pool.block_till_done() state = self.hass.states.get('lock.test') self.assertEqual(STATE_UNLOCKED, state.state)
def test_if_fires_using_after(self): """Test for firing after.""" assert _setup_component( self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'time', 'after': '5:00:00', }, 'action': { 'service': 'test.automation', 'data_template': { 'some': '{{ trigger.platform }} - ' '{{ trigger.now.hour }}' }, } } }) fire_time_changed(self.hass, dt_util.utcnow().replace(hour=5, minute=0, second=0)) self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.assertEqual('time - 5', self.calls[0].data['some'])
def test_controlling_state_via_topic_with_templates(self): """Test the setting og the state with a template.""" self.hass.config.components = ['mqtt'] assert _setup_component(self.hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'test_light_rgb/status', 'command_topic': 'test_light_rgb/set', 'brightness_state_topic': 'test_light_rgb/brightness/status', 'rgb_state_topic': 'test_light_rgb/rgb/status', 'state_value_template': '{{ value_json.hello }}', 'brightness_value_template': '{{ value_json.hello }}', 'rgb_value_template': '{{ value_json.hello | join(",") }}', } }) state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) self.assertIsNone(state.attributes.get('brightness')) self.assertIsNone(state.attributes.get('rgb_color')) fire_mqtt_message(self.hass, 'test_light_rgb/rgb/status', '{"hello": [1, 2, 3]}') fire_mqtt_message(self.hass, 'test_light_rgb/status', '{"hello": "ON"}') fire_mqtt_message(self.hass, 'test_light_rgb/brightness/status', '{"hello": "50"}') self.hass.pool.block_till_done() state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertEqual(50, state.attributes.get('brightness')) self.assertEqual([1, 2, 3], state.attributes.get('rgb_color'))
def test_two_triggers(self): """Test triggers.""" assert _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': [ { 'platform': 'event', 'event_type': 'test_event', }, { 'platform': 'state', 'entity_id': 'test.entity', } ], 'action': { 'service': 'test.automation', } } }) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.hass.states.set('test.entity', 'hello') self.hass.pool.block_till_done() self.assertEqual(2, len(self.calls))
def test_if_not_fires_using_wrong_after(self): """YAML translates time values to total seconds. This should break the before rule. """ assert not _setup_component( self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'time', 'after': 3605, # Total seconds. Hour = 3600 second }, 'action': { 'service': 'test.automation' } } }) fire_time_changed(self.hass, dt_util.utcnow().replace(hour=1, minute=0, second=5)) self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls))
def test_automation_list_setting(self): """Event is not a valid condition.""" self.assertTrue(_setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: [{ 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'action': { 'service': 'test.automation', } }, { 'trigger': { 'platform': 'event', 'event_type': 'test_event_2', }, 'action': { 'service': 'test.automation', } }] })) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.hass.bus.fire('test_event_2') self.hass.pool.block_till_done() self.assertEqual(2, len(self.calls))
def test_if_action_after(self): """Test for if action after.""" assert _setup_component( self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event' }, 'condition': { 'platform': 'time', 'after': '10:00', }, 'action': { 'service': 'test.automation' } } }) before_10 = dt_util.now().replace(hour=8) after_10 = dt_util.now().replace(hour=14) with patch('blumate.helpers.condition.dt_util.now', return_value=before_10): self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls)) with patch('blumate.helpers.condition.dt_util.now', return_value=after_10): self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_if_action_after(self): """Test for if action after.""" assert _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event' }, 'condition': { 'platform': 'time', 'after': '10:00', }, 'action': { 'service': 'test.automation' } } }) before_10 = dt_util.now().replace(hour=8) after_10 = dt_util.now().replace(hour=14) with patch('blumate.helpers.condition.dt_util.now', return_value=before_10): self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls)) with patch('blumate.helpers.condition.dt_util.now', return_value=after_10): self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_controlling_state_via_topic_and_json_message(self): """Test the controlling state via topic and JSON message.""" assert _setup_component(self.hass, garage_door.DOMAIN, { garage_door.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'state_open': 'beer open', 'state_closed': 'beer closed', 'service_open': 'beer service open', 'service_close': 'beer service close', 'value_template': '{{ value_json.val }}' } }) state = self.hass.states.get('garage_door.test') self.assertEqual(STATE_CLOSED, state.state) fire_mqtt_message(self.hass, 'state-topic', '{"val":"beer open"}') self.hass.pool.block_till_done() state = self.hass.states.get('garage_door.test') self.assertEqual(STATE_OPEN, state.state) fire_mqtt_message(self.hass, 'state-topic', '{"val":"beer closed"}') self.hass.pool.block_till_done() state = self.hass.states.get('garage_door.test') self.assertEqual(STATE_CLOSED, state.state)
def test_controlling_state_via_topic_and_json_message(self): """Test the controlling state via topic and JSON message.""" assert _setup_component( self.hass, garage_door.DOMAIN, { garage_door.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'state_open': 'beer open', 'state_closed': 'beer closed', 'service_open': 'beer service open', 'service_close': 'beer service close', 'value_template': '{{ value_json.val }}' } }) state = self.hass.states.get('garage_door.test') self.assertEqual(STATE_CLOSED, state.state) fire_mqtt_message(self.hass, 'state-topic', '{"val":"beer open"}') self.hass.pool.block_till_done() state = self.hass.states.get('garage_door.test') self.assertEqual(STATE_OPEN, state.state) fire_mqtt_message(self.hass, 'state-topic', '{"val":"beer closed"}') self.hass.pool.block_till_done() state = self.hass.states.get('garage_door.test') self.assertEqual(STATE_CLOSED, state.state)
def test_controlling_state_via_topic(self): """Test the controlling state via topic.""" assert _setup_component(self.hass, garage_door.DOMAIN, { garage_door.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'state_open': 1, 'state_closed': 0, 'service_open': 1, 'service_close': 0 } }) state = self.hass.states.get('garage_door.test') self.assertEqual(STATE_CLOSED, state.state) self.assertIsNone(state.attributes.get(ATTR_ASSUMED_STATE)) fire_mqtt_message(self.hass, 'state-topic', '1') self.hass.pool.block_till_done() state = self.hass.states.get('garage_door.test') self.assertEqual(STATE_OPEN, state.state) fire_mqtt_message(self.hass, 'state-topic', '0') self.hass.pool.block_till_done() state = self.hass.states.get('garage_door.test') self.assertEqual(STATE_CLOSED, state.state)
def test_sending_mqtt_commands_and_optimistic(self): """Test the sending MQTT commands in optimistic mode.""" assert _setup_component( self.hass, garage_door.DOMAIN, { garage_door.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'command_topic': 'command-topic', 'state_open': 'beer state open', 'state_closed': 'beer state closed', 'service_open': 'beer open', 'service_close': 'beer close', 'qos': '2' } }) state = self.hass.states.get('garage_door.test') self.assertEqual(STATE_CLOSED, state.state) self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE)) garage_door.open_door(self.hass, 'garage_door.test') self.hass.pool.block_till_done() self.assertEqual(('command-topic', 'beer open', 2, False), self.mock_publish.mock_calls[-1][1]) state = self.hass.states.get('garage_door.test') self.assertEqual(STATE_OPEN, state.state) garage_door.close_door(self.hass, 'garage_door.test') self.hass.pool.block_till_done() self.assertEqual(('command-topic', 'beer close', 2, False), self.mock_publish.mock_calls[-1][1]) state = self.hass.states.get('garage_door.test') self.assertEqual(STATE_CLOSED, state.state)
def test_sending_mqtt_commands_and_optimistic(self): """Test the sending MQTT commands in optimistic mode.""" assert _setup_component(self.hass, garage_door.DOMAIN, { garage_door.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'command_topic': 'command-topic', 'state_open': 'beer state open', 'state_closed': 'beer state closed', 'service_open': 'beer open', 'service_close': 'beer close', 'qos': '2' } }) state = self.hass.states.get('garage_door.test') self.assertEqual(STATE_CLOSED, state.state) self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE)) garage_door.open_door(self.hass, 'garage_door.test') self.hass.pool.block_till_done() self.assertEqual(('command-topic', 'beer open', 2, False), self.mock_publish.mock_calls[-1][1]) state = self.hass.states.get('garage_door.test') self.assertEqual(STATE_OPEN, state.state) garage_door.close_door(self.hass, 'garage_door.test') self.hass.pool.block_till_done() self.assertEqual(('command-topic', 'beer close', 2, False), self.mock_publish.mock_calls[-1][1]) state = self.hass.states.get('garage_door.test') self.assertEqual(STATE_CLOSED, state.state)
def test_toggle_service(self): """Test the toggling of a service.""" event = 'test_event' events = [] def record_event(event): """Add recorded event to set.""" events.append(event) self.hass.bus.listen(event, record_event) assert _setup_component(self.hass, 'script', { 'script': { 'test': { 'sequence': [{ 'delay': { 'seconds': 5 } }, { 'event': event, }] } } }) script.toggle(self.hass, ENTITY_ID) self.hass.pool.block_till_done() self.assertTrue(script.is_on(self.hass, ENTITY_ID)) self.assertEqual(0, len(events)) script.toggle(self.hass, ENTITY_ID) self.hass.pool.block_till_done() self.assertFalse(script.is_on(self.hass, ENTITY_ID)) self.assertEqual(0, len(events))
def test_old_config(self): """Test with 1 switch.""" self.assertTrue(_setup_component(self.hass, 'switch', { 'switch': {'platform': 'rfxtrx', 'devices': {'123efab1': { 'name': 'Test', 'packetid': '0b1100cd0213c7f210010f51'}}}})) import RFXtrx as rfxtrxmod rfxtrx_core.RFXOBJECT =\ rfxtrxmod.Core("", transport_protocol=rfxtrxmod.DummyTransport) self.assertEqual(1, len(rfxtrx_core.RFX_DEVICES)) entity = rfxtrx_core.RFX_DEVICES['213c7f216'] self.assertEqual('Test', entity.name) self.assertEqual('off', entity.state) self.assertTrue(entity.assumed_state) self.assertEqual(entity.signal_repetitions, 1) self.assertFalse(entity.should_fire_event) self.assertFalse(entity.should_poll) self.assertFalse(entity.is_on) entity.turn_on() self.assertTrue(entity.is_on) entity.turn_off() self.assertFalse(entity.is_on)
def test_sending_mqtt_commands_and_optimistic(self): """Test the sending MQTT commands in optimistic mode.""" self.hass.config.components = ['mqtt'] assert _setup_component(self.hass, lock.DOMAIN, { lock.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'command_topic': 'command-topic', 'payload_lock': 'LOCK', 'payload_unlock': 'UNLOCK', 'qos': 2 } }) state = self.hass.states.get('lock.test') self.assertEqual(STATE_UNLOCKED, state.state) self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE)) lock.lock(self.hass, 'lock.test') self.hass.pool.block_till_done() self.assertEqual(('command-topic', 'LOCK', 2, False), self.mock_publish.mock_calls[-1][1]) state = self.hass.states.get('lock.test') self.assertEqual(STATE_LOCKED, state.state) lock.unlock(self.hass, 'lock.test') self.hass.pool.block_till_done() self.assertEqual(('command-topic', 'UNLOCK', 2, False), self.mock_publish.mock_calls[-1][1]) state = self.hass.states.get('lock.test') self.assertEqual(STATE_UNLOCKED, state.state)
def test_zone_condition(self): """Test for zone condition.""" self.hass.states.set('test.entity', 'hello', { 'latitude': 32.880586, 'longitude': -117.237564 }) self.hass.pool.block_till_done() assert _setup_component( self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event' }, 'condition': { 'platform': 'zone', 'entity_id': 'test.entity', 'zone': 'zone.test', }, 'action': { 'service': 'test.automation', } } }) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_using_trigger_as_condition_with_invalid_condition(self): """Event is not a valid condition.""" entity_id = 'test.entity' self.hass.states.set(entity_id, 100) assert _setup_component( self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': [{ 'platform': 'event', 'event_type': 'test_event', }, { 'platform': 'numeric_state', 'entity_id': entity_id, 'below': 150 }], 'condition': 'use_trigger_values', 'action': { 'service': 'test.automation', } } }) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_if_not_fires_for_enter_on_zone_leave(self): """Test for not firing on zone leave.""" self.hass.states.set('test.entity', 'hello', { 'latitude': 32.880586, 'longitude': -117.237564 }) self.hass.pool.block_till_done() assert _setup_component( self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'zone', 'entity_id': 'test.entity', 'zone': 'zone.test', 'event': 'enter', }, 'action': { 'service': 'test.automation', } } }) self.hass.states.set('test.entity', 'hello', { 'latitude': 32.881011, 'longitude': -117.234758 }) self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls))
def test_automation_calling_two_actions(self): """Test if we can call two actions from automation definition.""" self.assertTrue( _setup_component( self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'action': [{ 'service': 'test.automation', 'data': { 'position': 0 }, }, { 'service': 'test.automation', 'data': { 'position': 1 }, }], } })) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() assert len(self.calls) == 2 assert self.calls[0].data['position'] == 0 assert self.calls[1].data['position'] == 1
def test_controlling_state_via_topic(self): """Test the controlling state via topic.""" self.hass.config.components = ['mqtt'] assert _setup_component( self.hass, lock.DOMAIN, { lock.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'payload_lock': 'LOCK', 'payload_unlock': 'UNLOCK' } }) state = self.hass.states.get('lock.test') self.assertEqual(STATE_UNLOCKED, state.state) self.assertIsNone(state.attributes.get(ATTR_ASSUMED_STATE)) fire_mqtt_message(self.hass, 'state-topic', 'LOCK') self.hass.pool.block_till_done() state = self.hass.states.get('lock.test') self.assertEqual(STATE_LOCKED, state.state) fire_mqtt_message(self.hass, 'state-topic', 'UNLOCK') self.hass.pool.block_till_done() state = self.hass.states.get('lock.test') self.assertEqual(STATE_UNLOCKED, state.state)
def test_several_lights(self): """Test with 3 lights.""" self.assertTrue(_setup_component(self.hass, 'light', { 'light': {'platform': 'rfxtrx', 'signal_repetitions': 3, 'devices': {'0b1100cd0213c7f230010f71': { 'name': 'Test'}, '0b1100100118cdea02010f70': { 'name': 'Bath'}, '0b1100101118cdea02010f70': { 'name': 'Living'}}}})) self.assertEqual(3, len(rfxtrx_core.RFX_DEVICES)) device_num = 0 for id in rfxtrx_core.RFX_DEVICES: entity = rfxtrx_core.RFX_DEVICES[id] self.assertEqual(entity.signal_repetitions, 3) if entity.name == 'Living': device_num = device_num + 1 self.assertEqual('off', entity.state) self.assertEqual('<Entity Living: off>', entity.__str__()) elif entity.name == 'Bath': device_num = device_num + 1 self.assertEqual('off', entity.state) self.assertEqual('<Entity Bath: off>', entity.__str__()) elif entity.name == 'Test': device_num = device_num + 1 self.assertEqual('off', entity.state) self.assertEqual('<Entity Test: off>', entity.__str__()) self.assertEqual(3, device_num)
def test_sending_mqtt_commands_and_optimistic(self): """Test the sending MQTT commands in optimistic mode.""" self.hass.config.components = ['mqtt'] assert _setup_component( self.hass, lock.DOMAIN, { lock.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'command_topic': 'command-topic', 'payload_lock': 'LOCK', 'payload_unlock': 'UNLOCK', 'qos': 2 } }) state = self.hass.states.get('lock.test') self.assertEqual(STATE_UNLOCKED, state.state) self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE)) lock.lock(self.hass, 'lock.test') self.hass.pool.block_till_done() self.assertEqual(('command-topic', 'LOCK', 2, False), self.mock_publish.mock_calls[-1][1]) state = self.hass.states.get('lock.test') self.assertEqual(STATE_LOCKED, state.state) lock.unlock(self.hass, 'lock.test') self.hass.pool.block_till_done() self.assertEqual(('command-topic', 'UNLOCK', 2, False), self.mock_publish.mock_calls[-1][1]) state = self.hass.states.get('lock.test') self.assertEqual(STATE_UNLOCKED, state.state)