Add recent context (#15989)

* Add recent context

* Add async_set_context to components not using new services
This commit is contained in:
Paulus Schoutsen 2018-08-20 17:39:53 +02:00 committed by GitHub
parent d1e1b9b38a
commit 1be61df9c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 211 additions and 11 deletions

View file

@ -1,11 +1,13 @@
"""Test the entity helper."""
# pylint: disable=protected-access
import asyncio
from unittest.mock import MagicMock, patch
from datetime import timedelta
from unittest.mock import MagicMock, patch, PropertyMock
import pytest
import homeassistant.helpers.entity as entity
from homeassistant.core import Context
from homeassistant.const import ATTR_HIDDEN, ATTR_DEVICE_CLASS
from homeassistant.config import DATA_CUSTOMIZE
from homeassistant.helpers.entity_values import EntityValues
@ -412,3 +414,32 @@ async def test_async_remove_runs_callbacks(hass):
ent.async_on_remove(lambda: result.append(1))
await ent.async_remove()
assert len(result) == 1
async def test_set_context(hass):
"""Test setting context."""
context = Context()
ent = entity.Entity()
ent.hass = hass
ent.entity_id = 'hello.world'
ent.async_set_context(context)
await ent.async_update_ha_state()
assert hass.states.get('hello.world').context == context
async def test_set_context_expired(hass):
"""Test setting context."""
context = Context()
with patch.object(entity.Entity, 'context_recent_time',
new_callable=PropertyMock) as recent:
recent.return_value = timedelta(seconds=-5)
ent = entity.Entity()
ent.hass = hass
ent.entity_id = 'hello.world'
ent.async_set_context(context)
await ent.async_update_ha_state()
assert hass.states.get('hello.world').context != context
assert ent._context is None
assert ent._context_set is None