desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'Test if the MQTT server gets started and subscribe/publish msg.'
@patch('passlib.apps.custom_app_context', Mock(return_value='')) @patch('tempfile.NamedTemporaryFile', Mock(return_value=MagicMock())) @patch('hbmqtt.broker.Broker', Mock(return_value=MagicMock())) @patch('hbmqtt.broker.Broker.start', Mock(return_value=mock_coro())) @patch('homeassistant.components.mqtt.MQTT') def test...
mock_mqtt().async_connect.return_value = mock_coro(True) self.hass.bus.listen_once = MagicMock() password = 'super_secret' self.hass.config.api = MagicMock(api_password=password) assert setup_component(self.hass, mqtt.DOMAIN, {}) assert mock_mqtt.called from pprint import pprint pprint(m...
'Test if the MQTT server gets started and subscribe/publish msg.'
@patch('passlib.apps.custom_app_context', Mock(return_value='')) @patch('tempfile.NamedTemporaryFile', Mock(return_value=MagicMock())) @patch('hbmqtt.broker.Broker', Mock(return_value=MagicMock())) @patch('hbmqtt.broker.Broker.start', Mock(return_value=mock_coro())) @patch('homeassistant.components.mqtt.MQTT') def test...
mock_mqtt().async_connect.return_value = mock_coro(True) self.hass.bus.listen_once = MagicMock() self.hass.config.api = MagicMock(api_password=None) assert setup_component(self.hass, mqtt.DOMAIN, {}) assert mock_mqtt.called assert (mock_mqtt.mock_calls[1][1][5] is None) assert (mock_mqtt.moc...
'Test if the MQTT component fails if server fails.'
@patch('tempfile.NamedTemporaryFile', Mock(return_value=MagicMock())) @patch('hbmqtt.broker.Broker.start', return_value=mock_coro()) def test_broker_config_fails(self, mock_run):
from hbmqtt.broker import BrokerException mock_run.side_effect = BrokerException self.hass.config.api = MagicMock(api_password=None) assert (not setup_component(self.hass, mqtt.DOMAIN, {mqtt.DOMAIN: {mqtt.CONF_EMBEDDED: {}}}))
'Setup things to be run when tests are started.'
def setUp(self):
self.hass = get_test_home_assistant()
'Stop everything that was started.'
def tearDown(self):
self.hass.stop()
'Initialize the recorder.'
def init_recorder(self):
init_recorder_component(self.hass) self.hass.start() self.wait_recording_done()
'Block till recording is done.'
def wait_recording_done(self):
self.hass.block_till_done() self.hass.data[recorder.DATA_INSTANCE].block_till_done()
'Test setup method of history.'
def test_setup(self):
mock_http_component(self.hass) config = history.CONFIG_SCHEMA({history.DOMAIN: {history.CONF_INCLUDE: {history.CONF_DOMAINS: ['media_player'], history.CONF_ENTITIES: ['thermostat.test']}, history.CONF_EXCLUDE: {history.CONF_DOMAINS: ['thermostat'], history.CONF_ENTITIES: ['media_player.test']}}}) self.init_...
'Test getting states at a specific point in time.'
def test_get_states(self):
self.init_recorder() states = [] now = dt_util.utcnow() with patch('homeassistant.components.recorder.dt_util.utcnow', return_value=now): for i in range(5): state = ha.State('test.point_in_time_{}'.format((i % 5)), 'State {}'.format(i), {'attribute_test': i}) mock_stat...
'Test state change during period.'
def test_state_changes_during_period(self):
self.init_recorder() entity_id = 'media_player.test' def set_state(state): 'Set the state.' self.hass.states.set(entity_id, state) self.wait_recording_done() return self.hass.states.get(entity_id) start = dt_util.utcnow() point = (start + timedelta(seconds=1)) ...
'Test that only significant states are returned. We should get back every thermostat change that includes an attribute change, but only the state updates for media player (attribute changes are not significant and not returned).'
def test_get_significant_states(self):
(zero, four, states) = self.record_states() hist = history.get_significant_states(self.hass, zero, four, filters=history.Filters()) assert (states == hist)
'Test that only significant states are returned for one entity.'
def test_get_significant_states_entity_id(self):
(zero, four, states) = self.record_states() del states['media_player.test2'] del states['thermostat.test'] del states['thermostat.test2'] del states['script.can_cancel_this_one'] hist = history.get_significant_states(self.hass, zero, four, 'media_player.test', filters=history.Filters()) asse...
'Test if significant states are returned when excluding domains. We should get back every thermostat change that includes an attribute change, but no media player changes.'
def test_get_significant_states_exclude_domain(self):
(zero, four, states) = self.record_states() del states['media_player.test'] del states['media_player.test2'] config = history.CONFIG_SCHEMA({ha.DOMAIN: {}, history.DOMAIN: {history.CONF_EXCLUDE: {history.CONF_DOMAINS: ['media_player']}}}) self.check_significant_states(zero, four, states, config)
'Test if significant states are returned when excluding entities. We should get back every thermostat and script changes, but no media player changes.'
def test_get_significant_states_exclude_entity(self):
(zero, four, states) = self.record_states() del states['media_player.test'] config = history.CONFIG_SCHEMA({ha.DOMAIN: {}, history.DOMAIN: {history.CONF_EXCLUDE: {history.CONF_ENTITIES: ['media_player.test']}}}) self.check_significant_states(zero, four, states, config)
'Test significant states when excluding entities and domains. We should not get back every thermostat and media player test changes.'
def test_get_significant_states_exclude(self):
(zero, four, states) = self.record_states() del states['media_player.test'] del states['thermostat.test'] del states['thermostat.test2'] config = history.CONFIG_SCHEMA({ha.DOMAIN: {}, history.DOMAIN: {history.CONF_EXCLUDE: {history.CONF_DOMAINS: ['thermostat'], history.CONF_ENTITIES: ['media_player....
'Test significant states when excluding domains and include entities. We should not get back every thermostat and media player test changes.'
def test_get_significant_states_exclude_include_entity(self):
(zero, four, states) = self.record_states() del states['media_player.test2'] del states['thermostat.test'] del states['thermostat.test2'] del states['script.can_cancel_this_one'] config = history.CONFIG_SCHEMA({ha.DOMAIN: {}, history.DOMAIN: {history.CONF_INCLUDE: {history.CONF_ENTITIES: ['media...
'Test if significant states are returned when including domains. We should get back every thermostat and script changes, but no media player changes.'
def test_get_significant_states_include_domain(self):
(zero, four, states) = self.record_states() del states['media_player.test'] del states['media_player.test2'] config = history.CONFIG_SCHEMA({ha.DOMAIN: {}, history.DOMAIN: {history.CONF_INCLUDE: {history.CONF_DOMAINS: ['thermostat', 'script']}}}) self.check_significant_states(zero, four, states, con...
'Test if significant states are returned when including entities. We should only get back changes of the media_player.test entity.'
def test_get_significant_states_include_entity(self):
(zero, four, states) = self.record_states() del states['media_player.test2'] del states['thermostat.test'] del states['thermostat.test2'] del states['script.can_cancel_this_one'] config = history.CONFIG_SCHEMA({ha.DOMAIN: {}, history.DOMAIN: {history.CONF_INCLUDE: {history.CONF_ENTITIES: ['media...
'Test significant states when including domains and entities. We should only get back changes of the media_player.test entity and the thermostat domain.'
def test_get_significant_states_include(self):
(zero, four, states) = self.record_states() del states['media_player.test2'] del states['script.can_cancel_this_one'] config = history.CONFIG_SCHEMA({ha.DOMAIN: {}, history.DOMAIN: {history.CONF_INCLUDE: {history.CONF_DOMAINS: ['thermostat'], history.CONF_ENTITIES: ['media_player.test']}}}) self.che...
'Test if significant states when excluding and including domains. We should not get back any changes since we include only the media_player domain but also exclude it.'
def test_get_significant_states_include_exclude_domain(self):
(zero, four, states) = self.record_states() del states['media_player.test'] del states['media_player.test2'] del states['thermostat.test'] del states['thermostat.test2'] del states['script.can_cancel_this_one'] config = history.CONFIG_SCHEMA({ha.DOMAIN: {}, history.DOMAIN: {history.CONF_INCL...
'Test if significant states when excluding and including domains. We should not get back any changes since we include only media_player.test but also exclude it.'
def test_get_significant_states_include_exclude_entity(self):
(zero, four, states) = self.record_states() del states['media_player.test'] del states['media_player.test2'] del states['thermostat.test'] del states['thermostat.test2'] del states['script.can_cancel_this_one'] config = history.CONFIG_SCHEMA({ha.DOMAIN: {}, history.DOMAIN: {history.CONF_INCL...
'Test if significant states when in/excluding domains and entities. We should only get back changes of the media_player.test2 entity.'
def test_get_significant_states_include_exclude(self):
(zero, four, states) = self.record_states() del states['media_player.test'] del states['thermostat.test'] del states['thermostat.test2'] del states['script.can_cancel_this_one'] config = history.CONFIG_SCHEMA({ha.DOMAIN: {}, history.DOMAIN: {history.CONF_INCLUDE: {history.CONF_DOMAINS: ['media_p...
'Check if significant states are retrieved.'
def check_significant_states(self, zero, four, states, config):
filters = history.Filters() exclude = config[history.DOMAIN].get(history.CONF_EXCLUDE) if exclude: filters.excluded_entities = exclude[history.CONF_ENTITIES] filters.excluded_domains = exclude[history.CONF_DOMAINS] include = config[history.DOMAIN].get(history.CONF_INCLUDE) if include...
'Record some test states. We inject a bunch of state updates from media player, zone and thermostat.'
def record_states(self):
self.init_recorder() mp = 'media_player.test' mp2 = 'media_player.test2' therm = 'thermostat.test' therm2 = 'thermostat.test2' zone = 'zone.home' script_nc = 'script.cannot_cancel_this_one' script_c = 'script.can_cancel_this_one' def set_state(entity_id, state, **kwargs): 'Se...
'Setup things to be run when tests are started.'
def setUp(self):
self.hass = get_test_home_assistant()
'Stop everything that was started.'
def tearDown(self):
self.hass.stop()
'Test setup with all data.'
def test_setup_config_full(self):
config = {'logentries': {'token': 'secret'}} self.hass.bus.listen = mock.MagicMock() self.assertTrue(setup_component(self.hass, logentries.DOMAIN, config)) self.assertTrue(self.hass.bus.listen.called) self.assertEqual(EVENT_STATE_CHANGED, self.hass.bus.listen.call_args_list[0][0][0])
'Test setup with defaults.'
def test_setup_config_defaults(self):
config = {'logentries': {'token': 'token'}} self.hass.bus.listen = mock.MagicMock() self.assertTrue(setup_component(self.hass, logentries.DOMAIN, config)) self.assertTrue(self.hass.bus.listen.called) self.assertEqual(EVENT_STATE_CHANGED, self.hass.bus.listen.call_args_list[0][0][0])
'Test the setup.'
def _setup(self, mock_requests):
self.mock_post = mock_requests.post self.mock_request_exception = Exception mock_requests.exceptions.RequestException = self.mock_request_exception config = {'logentries': {'token': 'token'}} self.hass.bus.listen = mock.MagicMock() setup_component(self.hass, logentries.DOMAIN, config) self.h...
'Test event listener.'
@mock.patch.object(logentries, 'requests') @mock.patch('json.dumps') def test_event_listener(self, mock_dump, mock_requests):
mock_dump.side_effect = (lambda x: x) self._setup(mock_requests) valid = {'1': 1, '1.0': 1.0, STATE_ON: 1, STATE_OFF: 0, 'foo': 'foo'} for (in_, out) in valid.items(): state = mock.MagicMock(state=in_, domain='fake', object_id='entity', attributes={}) event = mock.MagicMock(data={'new_st...
'Setup things to be run when tests are started.'
def setUp(self):
self.hass = get_test_home_assistant() self.hass.config.units = METRIC_SYSTEM self.assertTrue(setup_component(self.hass, weather.DOMAIN, {'weather': {'platform': 'demo'}}))
'Stop down everything that was started.'
def tearDown(self):
self.hass.stop()
'Test weather attributes.'
def test_attributes(self):
state = self.hass.states.get('weather.demo_weather_south') assert (state is not None) assert (state.state == 'sunny') data = state.attributes assert (data.get(ATTR_WEATHER_TEMPERATURE) == 21) assert (data.get(ATTR_WEATHER_HUMIDITY) == 92) assert (data.get(ATTR_WEATHER_PRESSURE) == 1099) ...
'Test temperature conversion.'
def test_temperature_convert(self):
state = self.hass.states.get('weather.demo_weather_north') assert (state is not None) assert (state.state == 'rainy') data = state.attributes assert (data.get(ATTR_WEATHER_TEMPERATURE) == (-24.4))
'Setup things to be run when tests are started.'
def setUp(self):
self.hass = get_test_home_assistant() init_recorder_component(self.hass) mock_http_component(self.hass) self.hass.config.components |= set(['frontend', 'recorder', 'api']) with patch('homeassistant.components.logbook.register_built_in_panel'): assert setup_component(self.hass, logbook.DOMAIN...
'Stop everything that was started.'
def tearDown(self):
self.hass.stop()
'Test if service call create log book entry.'
def test_service_call_create_logbook_entry(self):
calls = [] @ha.callback def event_listener(event): 'Append on event.' calls.append(event) self.hass.bus.listen(logbook.EVENT_LOGBOOK_ENTRY, event_listener) self.hass.services.call(logbook.DOMAIN, 'log', {logbook.ATTR_NAME: 'Alarm', logbook.ATTR_MESSAGE: 'is triggered', logbo...
'Test if service call create log book entry without message.'
def test_service_call_create_log_book_entry_no_message(self):
calls = [] @ha.callback def event_listener(event): 'Append on event.' calls.append(event) self.hass.bus.listen(logbook.EVENT_LOGBOOK_ENTRY, event_listener) self.hass.services.call(logbook.DOMAIN, 'log', {}, True) self.hass.block_till_done() self.assertEqual(0, len(calls...
'Test humanify filter too frequent sensor values.'
def test_humanify_filter_sensor(self):
entity_id = 'sensor.bla' pointA = dt_util.utcnow().replace(minute=2) pointB = pointA.replace(minute=5) pointC = (pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)) eventA = self.create_state_changed_event(pointA, entity_id, 10) eventB = self.create_state_changed_event(pointB, entity_id, 20) ...
'Test remove continuous sensor events from logbook.'
def test_filter_continuous_sensor_values(self):
entity_id = 'sensor.bla' pointA = dt_util.utcnow() attributes = {'unit_of_measurement': 'foo'} eventA = self.create_state_changed_event(pointA, entity_id, 10, attributes) entries = list(logbook.humanify((eventA,))) self.assertEqual(0, len(entries))
'Test if events are excluded on first update.'
def test_exclude_new_entities(self):
entity_id = 'sensor.bla' entity_id2 = 'sensor.blu' pointA = dt_util.utcnow() pointB = (pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)) eventA = self.create_state_changed_event(pointA, entity_id, 10) eventB = self.create_state_changed_event(pointB, entity_id2, 20) eventA.data['old_state...
'Test if events are excluded on last update.'
def test_exclude_removed_entities(self):
entity_id = 'sensor.bla' entity_id2 = 'sensor.blu' pointA = dt_util.utcnow() pointB = (pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)) eventA = self.create_state_changed_event(pointA, entity_id, 10) eventB = self.create_state_changed_event(pointB, entity_id2, 20) eventA.data['new_state...
'Test if events are excluded if entity is hidden.'
def test_exclude_events_hidden(self):
entity_id = 'sensor.bla' entity_id2 = 'sensor.blu' pointA = dt_util.utcnow() pointB = (pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)) eventA = self.create_state_changed_event(pointA, entity_id, 10, {ATTR_HIDDEN: 'true'}) eventB = self.create_state_changed_event(pointB, entity_id2, 20) ...
'Test if events are filtered if entity is excluded in config.'
def test_exclude_events_entity(self):
entity_id = 'sensor.bla' entity_id2 = 'sensor.blu' pointA = dt_util.utcnow() pointB = (pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)) eventA = self.create_state_changed_event(pointA, entity_id, 10) eventB = self.create_state_changed_event(pointB, entity_id2, 20) config = logbook.CONFI...
'Test if events are filtered if domain is excluded in config.'
def test_exclude_events_domain(self):
entity_id = 'switch.bla' entity_id2 = 'sensor.blu' pointA = dt_util.utcnow() pointB = (pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)) eventA = self.create_state_changed_event(pointA, entity_id, 10) eventB = self.create_state_changed_event(pointB, entity_id2, 20) config = logbook.CONFI...
'Test if automation entries can be excluded by entity_id.'
def test_exclude_automation_events(self):
name = 'My Automation Rule' message = 'has been triggered' domain = 'automation' entity_id = 'automation.my_automation_rule' entity_id2 = 'automation.my_automation_rule_2' entity_id2 = 'sensor.blu' eventA = ha.Event(logbook.EVENT_LOGBOOK_ENTRY, {logbook.ATTR_NAME: name, logbook.A...
'Test if events are filtered if entity is included in config.'
def test_include_events_entity(self):
entity_id = 'sensor.bla' entity_id2 = 'sensor.blu' pointA = dt_util.utcnow() pointB = (pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)) eventA = self.create_state_changed_event(pointA, entity_id, 10) eventB = self.create_state_changed_event(pointB, entity_id2, 20) config = logbook.CONFI...
'Test if events are filtered if domain is included in config.'
def test_include_events_domain(self):
entity_id = 'switch.bla' entity_id2 = 'sensor.blu' pointA = dt_util.utcnow() pointB = (pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)) eventA = self.create_state_changed_event(pointA, entity_id, 10) eventB = self.create_state_changed_event(pointB, entity_id2, 20) config = logbook.CONFI...
'Test if events are filtered if include and exclude is configured.'
def test_include_exclude_events(self):
entity_id = 'switch.bla' entity_id2 = 'sensor.blu' entity_id3 = 'sensor.bli' pointA = dt_util.utcnow() pointB = (pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)) eventA1 = self.create_state_changed_event(pointA, entity_id, 10) eventA2 = self.create_state_changed_event(pointA, entity_id2...
'Test if events of automatically generated groups are filtered.'
def test_exclude_auto_groups(self):
entity_id = 'switch.bla' entity_id2 = 'group.switches' pointA = dt_util.utcnow() eventA = self.create_state_changed_event(pointA, entity_id, 10) eventB = self.create_state_changed_event(pointA, entity_id2, 20, {'auto': True}) entries = list(logbook.humanify((eventA, eventB))) self.assertEqua...
'Test if events of attribute changes are filtered.'
def test_exclude_attribute_changes(self):
entity_id = 'switch.bla' entity_id2 = 'switch.blu' pointA = dt_util.utcnow() pointB = (pointA + timedelta(minutes=1)) eventA = self.create_state_changed_event(pointA, entity_id, 10) eventB = self.create_state_changed_event(pointA, entity_id2, 20, last_changed=pointA, last_updated=pointB) ent...
'Test conversion of entry to dict.'
def test_entry_to_dict(self):
entry = logbook.Entry(dt_util.utcnow(), 'Alarm', 'is triggered', 'switch', 'test_switch') data = entry.as_dict() self.assertEqual('Alarm', data.get(logbook.ATTR_NAME)) self.assertEqual('is triggered', data.get(logbook.ATTR_MESSAGE)) self.assertEqual('switch', data.get(logbook.ATTR_DOMAIN)) ...
'Test if HA start and stop events are grouped. Events that are occuring in the same minute.'
def test_home_assistant_start_stop_grouped(self):
entries = list(logbook.humanify((ha.Event(EVENT_HOMEASSISTANT_STOP), ha.Event(EVENT_HOMEASSISTANT_START)))) self.assertEqual(1, len(entries)) self.assert_entry(entries[0], name='Home Assistant', message='restarted', domain=ha.DOMAIN)
'Test if HA start is not filtered or converted into a restart.'
def test_home_assistant_start(self):
entity_id = 'switch.bla' pointA = dt_util.utcnow() entries = list(logbook.humanify((ha.Event(EVENT_HOMEASSISTANT_START), self.create_state_changed_event(pointA, entity_id, 10)))) self.assertEqual(2, len(entries)) self.assert_entry(entries[0], name='Home Assistant', message='started', domain=ha.DO...
'Test if logbook message is correctly created for switches. Especially test if the special handling for turn on/off events is done.'
def test_entry_message_from_state_device(self):
pointA = dt_util.utcnow() eventA = self.create_state_changed_event(pointA, 'switch.bla', 10) to_state = ha.State.from_dict(eventA.data.get('new_state')) message = logbook._entry_message_from_state(to_state.domain, to_state) self.assertEqual('changed to 10', message) eventA = self.create_st...
'Test if logbook message is correctly created for device tracker.'
def test_entry_message_from_state_device_tracker(self):
pointA = dt_util.utcnow() eventA = self.create_state_changed_event(pointA, 'device_tracker.john', STATE_NOT_HOME) to_state = ha.State.from_dict(eventA.data.get('new_state')) message = logbook._entry_message_from_state(to_state.domain, to_state) self.assertEqual('is away', message) eventA = se...
'Test if logbook message is correctly created for sun.'
def test_entry_message_from_state_sun(self):
pointA = dt_util.utcnow() eventA = self.create_state_changed_event(pointA, 'sun.sun', sun.STATE_ABOVE_HORIZON) to_state = ha.State.from_dict(eventA.data.get('new_state')) message = logbook._entry_message_from_state(to_state.domain, to_state) self.assertEqual('has risen', message) eventA = sel...
'Test if custom log book entries get added as an entry.'
def test_process_custom_logbook_entries(self):
name = 'Nice name' message = 'has a custom entry' entity_id = 'sun.sun' entries = list(logbook.humanify((ha.Event(logbook.EVENT_LOGBOOK_ENTRY, {logbook.ATTR_NAME: name, logbook.ATTR_MESSAGE: message, logbook.ATTR_ENTITY_ID: entity_id}),))) self.assertEqual(1, len(entries)) self.asser...
'Assert an entry is what is expected.'
def assert_entry(self, entry, when=None, name=None, message=None, domain=None, entity_id=None):
if when: self.assertEqual(when, entry.when) if name: self.assertEqual(name, entry.name) if message: self.assertEqual(message, entry.message) if domain: self.assertEqual(domain, entry.domain) if entity_id: self.assertEqual(entity_id, entry.entity_id)
'Create state changed event.'
def create_state_changed_event(self, event_time_fired, entity_id, state, attributes=None, last_changed=None, last_updated=None):
state = ha.State(entity_id, state, attributes, last_changed, last_updated).as_dict() return ha.Event(EVENT_STATE_CHANGED, {'entity_id': entity_id, 'old_state': state, 'new_state': state}, time_fired=event_time_fired)
'Setup things to be run when tests are started.'
def setUp(self):
self.hass = get_test_home_assistant()
'Stop everything that was started.'
def tearDown(self):
self.hass.stop()
'Test is_on method.'
def test_is_on(self):
self.hass.states.set(ENTITY_ID, STATE_ON) self.hass.block_till_done() self.assertTrue(alert.is_on(self.hass, ENTITY_ID)) self.hass.states.set(ENTITY_ID, STATE_OFF) self.hass.block_till_done() self.assertFalse(alert.is_on(self.hass, ENTITY_ID))
'Test setup method.'
def test_setup(self):
assert setup_component(self.hass, alert.DOMAIN, TEST_CONFIG) self.assertEqual(STATE_IDLE, self.hass.states.get(ENTITY_ID).state)
'Test the alert firing.'
def test_fire(self):
assert setup_component(self.hass, alert.DOMAIN, TEST_CONFIG) self.hass.states.set('sensor.test', STATE_ON) self.hass.block_till_done() self.assertEqual(STATE_ON, self.hass.states.get(ENTITY_ID).state)
'Test silencing the alert.'
def test_silence(self):
assert setup_component(self.hass, alert.DOMAIN, TEST_CONFIG) self.hass.states.set('sensor.test', STATE_ON) self.hass.block_till_done() alert.turn_off(self.hass, ENTITY_ID) self.hass.block_till_done() self.assertEqual(STATE_OFF, self.hass.states.get(ENTITY_ID).state) self.hass.states.set('sen...
'Test resetting the alert.'
def test_reset(self):
assert setup_component(self.hass, alert.DOMAIN, TEST_CONFIG) self.hass.states.set('sensor.test', STATE_ON) self.hass.block_till_done() alert.turn_off(self.hass, ENTITY_ID) self.hass.block_till_done() self.assertEqual(STATE_OFF, self.hass.states.get(ENTITY_ID).state) alert.turn_on(self.hass, ...
'Test toggling alert.'
def test_toggle(self):
assert setup_component(self.hass, alert.DOMAIN, TEST_CONFIG) self.hass.states.set('sensor.test', STATE_ON) self.hass.block_till_done() self.assertEqual(STATE_ON, self.hass.states.get(ENTITY_ID).state) alert.toggle(self.hass, ENTITY_ID) self.hass.block_till_done() self.assertEqual(STATE_OFF, ...
'Test entity hidding.'
def test_hidden(self):
assert setup_component(self.hass, alert.DOMAIN, TEST_CONFIG) hidden = self.hass.states.get(ENTITY_ID).attributes.get('hidden') self.assertTrue(hidden) self.hass.states.set('sensor.test', STATE_ON) self.hass.block_till_done() hidden = self.hass.states.get(ENTITY_ID).attributes.get('hidden') s...
'Test notifications.'
def test_notification_no_done_message(self):
events = [] config = deepcopy(TEST_CONFIG) del config[alert.DOMAIN][NAME][alert.CONF_DONE_MESSAGE] @callback def record_event(event): 'Add recorded event to set.' events.append(event) self.hass.services.register(notify.DOMAIN, NOTIFIER, record_event) assert setup_...
'Test notifications.'
def test_notification(self):
events = [] @callback def record_event(event): 'Add recorded event to set.' events.append(event) self.hass.services.register(notify.DOMAIN, NOTIFIER, record_event) assert setup_component(self.hass, alert.DOMAIN, TEST_CONFIG) self.assertEqual(0, len(events)) self.h...
'Test skipping first notification.'
def test_skipfirst(self):
config = deepcopy(TEST_CONFIG) config[alert.DOMAIN][NAME][alert.CONF_SKIP_FIRST] = True events = [] @callback def record_event(event): 'Add recorded event to set.' events.append(event) self.hass.services.register(notify.DOMAIN, NOTIFIER, record_event) assert setup...
'Test no ack feature.'
def test_noack(self):
entity = alert.Alert(self.hass, *TEST_NOACK) self.hass.add_job(entity.begin_alerting) self.hass.block_till_done() self.assertEqual(True, entity.hidden)
'Test that the done message is reset when cancelled.'
def test_done_message_state_tracker_reset_on_cancel(self):
entity = alert.Alert(self.hass, *TEST_NOACK) entity._cancel = (lambda *args: None) assert (entity._send_done_message is False) entity._send_done_message = True self.hass.add_job(entity.end_alerting) self.hass.block_till_done() assert (entity._send_done_message is False)
'Setup things to be run when tests are started.'
def setUp(self):
self.hass = get_test_home_assistant() self._mock_client = mock.patch.object(nx584_client, 'Client') self._mock_client.start() self.fake_zones = [{'name': 'front', 'number': 1}, {'name': 'back', 'number': 2}, {'name': 'inside', 'number': 3}] client = nx584_client.Client.return_value client.list_z...
'Stop everything that was started.'
def tearDown(self):
self.hass.stop() self._mock_client.stop()
'Test the setup with no configuration.'
@mock.patch('homeassistant.components.binary_sensor.nx584.NX584Watcher') @mock.patch('homeassistant.components.binary_sensor.nx584.NX584ZoneSensor') def test_setup_defaults(self, mock_nx, mock_watcher):
add_devices = mock.MagicMock() config = {'host': nx584.DEFAULT_HOST, 'port': nx584.DEFAULT_PORT, 'exclude_zones': [], 'zone_types': {}} self.assertTrue(nx584.setup_platform(self.hass, config, add_devices)) mock_nx.assert_has_calls([mock.call(zone, 'opening') for zone in self.fake_zones]) self.assert...
'Test the setup with full configuration.'
@mock.patch('homeassistant.components.binary_sensor.nx584.NX584Watcher') @mock.patch('homeassistant.components.binary_sensor.nx584.NX584ZoneSensor') def test_setup_full_config(self, mock_nx, mock_watcher):
config = {'host': 'foo', 'port': 123, 'exclude_zones': [2], 'zone_types': {3: 'motion'}} add_devices = mock.MagicMock() self.assertTrue(nx584.setup_platform(self.hass, config, add_devices)) mock_nx.assert_has_calls([mock.call(self.fake_zones[0], 'opening'), mock.call(self.fake_zones[2], 'motion')]) ...
'Test the failing.'
def _test_assert_graceful_fail(self, config):
self.assertFalse(setup_component(self.hass, 'binary_sensor.nx584', config))
'Test the setup with bad configuration.'
def test_setup_bad_config(self):
bad_configs = [{'exclude_zones': ['a']}, {'zone_types': {'a': 'b'}}, {'zone_types': {1: 'notatype'}}, {'zone_types': {'notazone': 'motion'}}] for config in bad_configs: self._test_assert_graceful_fail(config)
'Test the setup with connection failure.'
def test_setup_connect_failed(self):
nx584_client.Client.return_value.list_zones.side_effect = requests.exceptions.ConnectionError self._test_assert_graceful_fail({})
'Test the setup with connection failure.'
def test_setup_no_partitions(self):
nx584_client.Client.return_value.list_zones.side_effect = IndexError self._test_assert_graceful_fail({})
'"Test if version is too old.'
def test_setup_version_too_old(self):
nx584_client.Client.return_value.get_version.return_value = '1.0' self._test_assert_graceful_fail({})
'Test the setup with no zones.'
def test_setup_no_zones(self):
nx584_client.Client.return_value.list_zones.return_value = [] add_devices = mock.MagicMock() self.assertTrue(nx584.setup_platform(self.hass, {}, add_devices)) self.assertFalse(add_devices.called)
'Test the sensor.'
def test_sensor_normal(self):
zone = {'number': 1, 'name': 'foo', 'state': True} sensor = nx584.NX584ZoneSensor(zone, 'motion') self.assertEqual('foo', sensor.name) self.assertFalse(sensor.should_poll) self.assertTrue(sensor.is_on) zone['state'] = False self.assertFalse(sensor.is_on)
'Test the processing of zone events.'
@mock.patch.object(nx584.NX584ZoneSensor, 'schedule_update_ha_state') def test_process_zone_event(self, mock_update):
zone1 = {'number': 1, 'name': 'foo', 'state': True} zone2 = {'number': 2, 'name': 'bar', 'state': True} zones = {1: nx584.NX584ZoneSensor(zone1, 'motion'), 2: nx584.NX584ZoneSensor(zone2, 'motion')} watcher = nx584.NX584Watcher(None, zones) watcher._process_zone_event({'zone': 1, 'zone_state': False...
'Test the processing of zone events with missing zones.'
@mock.patch.object(nx584.NX584ZoneSensor, 'schedule_update_ha_state') def test_process_zone_event_missing_zone(self, mock_update):
watcher = nx584.NX584Watcher(None, {}) watcher._process_zone_event({'zone': 1, 'zone_state': False}) self.assertFalse(mock_update.called)
'Test the zone events.'
def test_run_with_zone_events(self):
empty_me = [1, 2] def fake_get_events(): 'Return nothing twice, then some events.' if empty_me: empty_me.pop() else: return fake_events client = mock.MagicMock() fake_events = [{'zone': 1, 'zone_state': True, 'type': 'zone_status'}, {'zone':...
'Test the retries with failures.'
@mock.patch('time.sleep') def test_run_retries_failures(self, mock_sleep):
empty_me = [1, 2] def fake_run(): 'Fake runner.' if empty_me: empty_me.pop() raise requests.exceptions.ConnectionError() else: raise StopMe() watcher = nx584.NX584Watcher(None, {}) with mock.patch.object(watcher, '_run') as mock_inner: ...
'Setup things to be run when tests are started.'
def setUp(self):
self.hass = get_test_home_assistant() mock_mqtt_component(self.hass)
'Stop everything that was started.'
def tearDown(self):
self.hass.stop()
'Test the setting of the value via MQTT.'
def test_setting_sensor_value_via_mqtt_message(self):
assert setup_component(self.hass, binary_sensor.DOMAIN, {binary_sensor.DOMAIN: {'platform': 'mqtt', 'name': 'test', 'state_topic': 'test-topic', 'payload_on': 'ON', 'payload_off': 'OFF'}}) state = self.hass.states.get('binary_sensor.test') self.assertEqual(STATE_OFF, state.state) fire_mqtt_message(self....
'Test the setting of a valid sensor class.'
def test_valid_device_class(self):
assert setup_component(self.hass, binary_sensor.DOMAIN, {binary_sensor.DOMAIN: {'platform': 'mqtt', 'name': 'test', 'device_class': 'motion', 'state_topic': 'test-topic'}}) state = self.hass.states.get('binary_sensor.test') self.assertEqual('motion', state.attributes.get('device_class'))
'Test the setting of an invalid sensor class.'
def test_invalid_device_class(self):
assert setup_component(self.hass, binary_sensor.DOMAIN, {binary_sensor.DOMAIN: {'platform': 'mqtt', 'name': 'test', 'device_class': 'abc123', 'state_topic': 'test-topic'}}) state = self.hass.states.get('binary_sensor.test') self.assertIsNone(state)
'Setup things to be run when tests are started.'
def setup_method(self):
self.hass = get_test_home_assistant() self.config = {'ffmpeg': {'run_test': False}, 'binary_sensor': {'platform': 'ffmpeg_noise', 'input': 'testinputvideo'}}
'Stop everything that was started.'
def teardown_method(self):
self.hass.stop()
'Setup ffmpeg component.'
def test_setup_component(self):
with assert_setup_component(1, 'binary_sensor'): setup_component(self.hass, 'binary_sensor', self.config) assert (self.hass.data['ffmpeg'].binary == 'ffmpeg') assert (self.hass.states.get('binary_sensor.ffmpeg_noise') is not None)
'Setup ffmpeg component.'
@patch('haffmpeg.SensorNoise.open_sensor', return_value=mock_coro()) def test_setup_component_start(self, mock_start):
with assert_setup_component(1, 'binary_sensor'): setup_component(self.hass, 'binary_sensor', self.config) assert (self.hass.data['ffmpeg'].binary == 'ffmpeg') assert (self.hass.states.get('binary_sensor.ffmpeg_noise') is not None) self.hass.start() assert mock_start.called entity = self....
'Setup ffmpeg component.'
@patch('haffmpeg.SensorNoise') def test_setup_component_start_callback(self, mock_ffmpeg):
with assert_setup_component(1, 'binary_sensor'): setup_component(self.hass, 'binary_sensor', self.config) assert (self.hass.data['ffmpeg'].binary == 'ffmpeg') assert (self.hass.states.get('binary_sensor.ffmpeg_noise') is not None) self.hass.start() entity = self.hass.states.get('binary_senso...
'Setup things to be run when tests are started.'
def setup_method(self):
self.hass = get_test_home_assistant() self.config = {'ffmpeg': {'run_test': False}, 'binary_sensor': {'platform': 'ffmpeg_motion', 'input': 'testinputvideo'}}