Example #1
0
 def setUp(self):  # pylint: disable=invalid-name
     """Setup things to be run when tests are started."""
     self.hass = get_test_home_assistant()
     db_uri = 'sqlite://'  # In memory DB
     _setup_component(self.hass, recorder.DOMAIN, {
         recorder.DOMAIN: {recorder.CONF_DB_URL: db_uri}})
     self.hass.start()
     recorder._verify_instance()
     self.session = recorder.Session()
     recorder._INSTANCE.block_till_done()
Example #2
0
 def setUp(self):  # pylint: disable=invalid-name
     """Setup things to be run when tests are started."""
     self.hass = get_test_home_assistant()
     db_uri = 'sqlite://'  # In memory DB
     setup_component(self.hass, recorder.DOMAIN,
                     {recorder.DOMAIN: {
                         recorder.CONF_DB_URL: db_uri
                     }})
     self.hass.start()
     recorder._verify_instance()
     recorder._INSTANCE.block_till_done()
Example #3
0
 def setup_recorder(config={}):
     """Setup with params."""
     db_uri = 'sqlite://'  # In memory DB
     conf = {recorder.CONF_DB_URL: db_uri}
     conf.update(config)
     assert setup_component(hass, recorder.DOMAIN, {recorder.DOMAIN: conf})
     hass.start()
     hass.block_till_done()
     recorder._verify_instance()
     recorder._INSTANCE.block_till_done()
     return hass
Example #4
0
def hack_load_previous_state(entity_id):
    """ Hack to lookup the previous state if we can """
    try:
        from sqlalchemy import desc
        recorder._verify_instance()
        local = recorder._INSTANCE.engine.connect(
        )  # trying to avoid thread access issues I was having, this is a hack already, so...
        ret = local.execute(
            "select state from states where domain='usercode' and entity_id=:eid order by state_id desc",
            {
                'eid': entity_id
            }).first()[0]
        local.close()
        return ret
    except Exception as e:
        _LOGGER.debug("Failed to load previous states: {}".format(e))
        return STATE_UNKNOWN
Example #5
0
def test_recorder_errors_exceptions(hass_recorder):     \
        # pylint: disable=redefined-outer-name
    """Test session_scope and get_model errors."""
    # Model cannot be resolved
    assert recorder.get_model('dont-exist') is None

    # Verify the instance fails before setup
    with pytest.raises(RuntimeError):
        recorder._verify_instance()

    # Setup the recorder
    hass_recorder()

    recorder._verify_instance()

    # Verify session scope raises (and prints) an exception
    with patch('homeassistant.components.recorder._LOGGER.error') as e_mock, \
            pytest.raises(Exception) as err:
        with recorder.session_scope() as session:
            session.execute('select * from notthere')
    assert e_mock.call_count == 1
    assert recorder.ERROR_QUERY[:-4] in e_mock.call_args[0][0]
    assert 'no such table' in str(err.value)