Exemple #1
0
def async_get_last_state(hass, entity_id: str):
    """Restore state."""
    if DATA_RESTORE_CACHE in hass.data:
        return hass.data[DATA_RESTORE_CACHE].get(entity_id)

    if _RECORDER not in hass.config.components:
        return None

    if hass.state not in (CoreState.starting, CoreState.not_running):
        _LOGGER.debug("Cache for %s can only be loaded during startup, not %s",
                      entity_id, hass.state)
        return None

    try:
        with async_timeout.timeout(RECORDER_TIMEOUT, loop=hass.loop):
            connected = yield from wait_connection_ready(hass)
    except asyncio.TimeoutError:
        return None

    if not connected:
        return None

    if _LOCK not in hass.data:
        hass.data[_LOCK] = asyncio.Lock(loop=hass.loop)

    with (yield from hass.data[_LOCK]):
        if DATA_RESTORE_CACHE not in hass.data:
            yield from hass.async_add_job(
                _load_restore_cache, hass)

    return hass.data.get(DATA_RESTORE_CACHE, {}).get(entity_id)
def test_schema_update_calls(hass):
    """Test that schema migrations occur in correct order."""
    with patch('sqlalchemy.create_engine', new=create_engine_test), \
        patch('homeassistant.components.recorder.migration._apply_update') as \
            update:
        yield from async_setup_component(hass, 'recorder', {
            'recorder': {
                'db_url': 'sqlite://'
            }
        })
        yield from wait_connection_ready(hass)

    update.assert_has_calls([
        call(hass.data[const.DATA_INSTANCE].engine, version+1, 0) for version
        in range(0, models.SCHEMA_VERSION)])
Exemple #3
0
def test_schema_update_calls(hass):
    """Test that schema migrations occur in correct order."""
    with patch('sqlalchemy.create_engine', new=create_engine_test), \
        patch('homeassistant.components.recorder.migration._apply_update') as \
            update:
        yield from async_setup_component(hass, 'recorder',
                                         {'recorder': {
                                             'db_url': 'sqlite://'
                                         }})
        yield from wait_connection_ready(hass)

    update.assert_has_calls([
        call(hass.data[const.DATA_INSTANCE].engine, version + 1, 0)
        for version in range(0, models.SCHEMA_VERSION)
    ])
Exemple #4
0
def test_schema_migrate(hass):
    """Test the full schema migration logic.

    We're just testing that the logic can execute successfully here without
    throwing exceptions. Maintaining a set of assertions based on schema
    inspection could quickly become quite cumbersome.
    """
    with patch('sqlalchemy.create_engine', new=create_engine_test), \
        patch('homeassistant.components.recorder.Recorder._setup_run') as \
            setup_run:
        yield from async_setup_component(hass, 'recorder',
                                         {'recorder': {
                                             'db_url': 'sqlite://'
                                         }})
        yield from wait_connection_ready(hass)
        assert setup_run.called
def test_schema_migrate(hass):
    """Test the full schema migration logic.

    We're just testing that the logic can execute successfully here without
    throwing exceptions. Maintaining a set of assertions based on schema
    inspection could quickly become quite cumbersome.
    """
    with patch('sqlalchemy.create_engine', new=create_engine_test), \
        patch('homeassistant.components.recorder.Recorder._setup_run') as \
            setup_run:
        yield from async_setup_component(hass, 'recorder', {
            'recorder': {
                'db_url': 'sqlite://'
            }
        })
        yield from wait_connection_ready(hass)
        assert setup_run.called