Esempio n. 1
0
def mock_restore_cache_with_extra_data(hass, states):
    """Mock the DATA_RESTORE_CACHE."""
    key = restore_state.DATA_RESTORE_STATE_TASK
    data = restore_state.RestoreStateData(hass)
    now = date_util.utcnow()

    last_states = {}
    for state, extra_data in states:
        restored_state = state.as_dict()
        restored_state = {
            **restored_state,
            "attributes":
            json.loads(
                json.dumps(restored_state["attributes"], cls=JSONEncoder)),
        }
        last_states[state.entity_id] = restore_state.StoredState.from_dict({
            "state":
            restored_state,
            "extra_data":
            extra_data,
            "last_seen":
            now
        })
    data.last_states = last_states
    _LOGGER.debug("Restore cache: %s", data.last_states)
    assert len(
        data.last_states) == len(states), f"Duplicate entity_id? {states}"

    hass.data[key] = data
Esempio n. 2
0
def mock_restore_cache(hass, states):
    """Mock the DATA_RESTORE_CACHE."""
    key = restore_state.DATA_RESTORE_STATE_TASK
    data = restore_state.RestoreStateData(hass)
    now = date_util.utcnow()

    last_states = {}
    for state in states:
        restored_state = state.as_dict()
        restored_state["attributes"] = json.loads(
            json.dumps(restored_state["attributes"], cls=JSONEncoder)
        )
        last_states[state.entity_id] = restore_state.StoredState(
            State.from_dict(restored_state), now
        )
    data.last_states = last_states
    _LOGGER.debug("Restore cache: %s", data.last_states)
    assert len(data.last_states) == len(states), "Duplicate entity_id? {}".format(
        states
    )

    async def get_restore_state_data() -> restore_state.RestoreStateData:
        return data

    # Patch the singleton task in hass.data to return our new RestoreStateData
    hass.data[key] = hass.async_create_task(get_restore_state_data())
Esempio n. 3
0
def mock_restore_cache(hass, states):
    """Mock the DATA_RESTORE_CACHE."""
    key = restore_state.DATA_RESTORE_STATE_TASK
    data = restore_state.RestoreStateData(hass)
    now = date_util.utcnow()

    data.last_states = {
        state.entity_id: restore_state.StoredState(state, now)
        for state in states}
    _LOGGER.debug('Restore cache: %s', data.last_states)
    assert len(data.last_states) == len(states), \
        "Duplicate entity_id? {}".format(states)

    async def get_restore_state_data() -> restore_state.RestoreStateData:
        return data

    # Patch the singleton task in hass.data to return our new RestoreStateData
    hass.data[key] = hass.async_create_task(get_restore_state_data())