Add recent context (#15989)
* Add recent context * Add async_set_context to components not using new services
This commit is contained in:
1 parent
d1e1b9b38a
commit
1be61df9c0
11 files changed
+211
-11
No files matched your search
@@ -4,7 +4,7 @@ import asyncio
|
||||
import unittest
|
||||
import logging
|
||||
|
||||
from homeassistant.core import CoreState, State
|
||||
from homeassistant.core import CoreState, State, Context
|
||||
from homeassistant.setup import setup_component, async_setup_component
|
||||
from homeassistant.components.counter import (
|
||||
DOMAIN, decrement, increment, reset, CONF_INITIAL, CONF_STEP, CONF_NAME,
|
||||
@@ -202,3 +202,24 @@ def test_no_initial_state_and_no_restore_state(hass):
|
||||
state = hass.states.get('counter.test1')
|
||||
assert state
|
||||
assert int(state.state) == 0
|
||||
|
||||
|
||||
async def test_counter_context(hass):
|
||||
"""Test that counter context works."""
|
||||
assert await async_setup_component(hass, 'counter', {
|
||||
'counter': {
|
||||
'test': {}
|
||||
}
|
||||
})
|
||||
|
||||
state = hass.states.get('counter.test')
|
||||
assert state is not None
|
||||
|
||||
await hass.services.async_call('counter', 'increment', {
|
||||
'entity_id': state.entity_id,
|
||||
}, True, Context(user_id='abcd'))
|
||||
|
||||
state2 = hass.states.get('counter.test')
|
||||
assert state2 is not None
|
||||
assert state.state != state2.state
|
||||
assert state2.context.user_id == 'abcd'
|
||||
@@ -4,7 +4,7 @@ import asyncio
|
||||
import unittest
|
||||
import logging
|
||||
|
||||
from homeassistant.core import CoreState, State
|
||||
from homeassistant.core import CoreState, State, Context
|
||||
from homeassistant.setup import setup_component, async_setup_component
|
||||
from homeassistant.components.input_boolean import (
|
||||
DOMAIN, is_on, toggle, turn_off, turn_on, CONF_INITIAL)
|
||||
@@ -158,3 +158,24 @@ def test_initial_state_overrules_restore_state(hass):
|
||||
state = hass.states.get('input_boolean.b2')
|
||||
assert state
|
||||
assert state.state == 'on'
|
||||
|
||||
|
||||
async def test_input_boolean_context(hass):
|
||||
"""Test that input_boolean context works."""
|
||||
assert await async_setup_component(hass, 'input_boolean', {
|
||||
'input_boolean': {
|
||||
'ac': {CONF_INITIAL: True},
|
||||
}
|
||||
})
|
||||
|
||||
state = hass.states.get('input_boolean.ac')
|
||||
assert state is not None
|
||||
|
||||
await hass.services.async_call('input_boolean', 'turn_off', {
|
||||
'entity_id': state.entity_id,
|
||||
}, True, Context(user_id='abcd'))
|
||||
|
||||
state2 = hass.states.get('input_boolean.ac')
|
||||
assert state2 is not None
|
||||
assert state.state != state2.state
|
||||
assert state2.context.user_id == 'abcd'
|
||||
@@ -4,7 +4,7 @@ import asyncio
|
||||
import unittest
|
||||
import datetime
|
||||
|
||||
from homeassistant.core import CoreState, State
|
||||
from homeassistant.core import CoreState, State, Context
|
||||
from homeassistant.setup import setup_component, async_setup_component
|
||||
from homeassistant.components.input_datetime import (
|
||||
DOMAIN, ATTR_ENTITY_ID, ATTR_DATE, ATTR_TIME, SERVICE_SET_DATETIME)
|
||||
@@ -208,3 +208,27 @@ def test_restore_state(hass):
|
||||
|
||||
state_bogus = hass.states.get('input_datetime.test_bogus_data')
|
||||
assert state_bogus.state == str(initial)
|
||||
|
||||
|
||||
async def test_input_datetime_context(hass):
|
||||
"""Test that input_datetime context works."""
|
||||
assert await async_setup_component(hass, 'input_datetime', {
|
||||
'input_datetime': {
|
||||
'only_date': {
|
||||
'has_date': True,
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
state = hass.states.get('input_datetime.only_date')
|
||||
assert state is not None
|
||||
|
||||
await hass.services.async_call('input_datetime', 'set_datetime', {
|
||||
'entity_id': state.entity_id,
|
||||
'date': '2018-01-02'
|
||||
}, True, Context(user_id='abcd'))
|
||||
|
||||
state2 = hass.states.get('input_datetime.only_date')
|
||||
assert state2 is not None
|
||||
assert state.state != state2.state
|
||||
assert state2.context.user_id == 'abcd'
|
||||
@@ -3,7 +3,7 @@
|
||||
import asyncio
|
||||
import unittest
|
||||
|
||||
from homeassistant.core import CoreState, State
|
||||
from homeassistant.core import CoreState, State, Context
|
||||
from homeassistant.setup import setup_component, async_setup_component
|
||||
from homeassistant.components.input_number import (
|
||||
DOMAIN, set_value, increment, decrement)
|
||||
@@ -236,3 +236,27 @@ def test_no_initial_state_and_no_restore_state(hass):
|
||||
state = hass.states.get('input_number.b1')
|
||||
assert state
|
||||
assert float(state.state) == 0
|
||||
|
||||
|
||||
async def test_input_number_context(hass):
|
||||
"""Test that input_number context works."""
|
||||
assert await async_setup_component(hass, 'input_number', {
|
||||
'input_number': {
|
||||
'b1': {
|
||||
'min': 0,
|
||||
'max': 100,
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
state = hass.states.get('input_number.b1')
|
||||
assert state is not None
|
||||
|
||||
await hass.services.async_call('input_number', 'increment', {
|
||||
'entity_id': state.entity_id,
|
||||
}, True, Context(user_id='abcd'))
|
||||
|
||||
state2 = hass.states.get('input_number.b1')
|
||||
assert state2 is not None
|
||||
assert state.state != state2.state
|
||||
assert state2.context.user_id == 'abcd'
|
||||
@@ -5,7 +5,7 @@ import unittest
|
||||
|
||||
from tests.common import get_test_home_assistant, mock_restore_cache
|
||||
|
||||
from homeassistant.core import State
|
||||
from homeassistant.core import State, Context
|
||||
from homeassistant.setup import setup_component, async_setup_component
|
||||
from homeassistant.components.input_select import (
|
||||
ATTR_OPTIONS, DOMAIN, SERVICE_SET_OPTIONS,
|
||||
@@ -276,3 +276,30 @@ def test_initial_state_overrules_restore_state(hass):
|
||||
state = hass.states.get('input_select.s2')
|
||||
assert state
|
||||
assert state.state == 'middle option'
|
||||
|
||||
|
||||
async def test_input_select_context(hass):
|
||||
"""Test that input_select context works."""
|
||||
assert await async_setup_component(hass, 'input_select', {
|
||||
'input_select': {
|
||||
's1': {
|
||||
'options': [
|
||||
'first option',
|
||||
'middle option',
|
||||
'last option',
|
||||
],
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
state = hass.states.get('input_select.s1')
|
||||
assert state is not None
|
||||
|
||||
await hass.services.async_call('input_select', 'select_next', {
|
||||
'entity_id': state.entity_id,
|
||||
}, True, Context(user_id='abcd'))
|
||||
|
||||
state2 = hass.states.get('input_select.s1')
|
||||
assert state2 is not None
|
||||
assert state.state != state2.state
|
||||
assert state2.context.user_id == 'abcd'
|
||||
@@ -3,7 +3,7 @@
|
||||
import asyncio
|
||||
import unittest
|
||||
|
||||
from homeassistant.core import CoreState, State
|
||||
from homeassistant.core import CoreState, State, Context
|
||||
from homeassistant.setup import setup_component, async_setup_component
|
||||
from homeassistant.components.input_text import (DOMAIN, set_value)
|
||||
|
||||
@@ -180,3 +180,27 @@ def test_no_initial_state_and_no_restore_state(hass):
|
||||
state = hass.states.get('input_text.b1')
|
||||
assert state
|
||||
assert str(state.state) == 'unknown'
|
||||
|
||||
|
||||
async def test_input_text_context(hass):
|
||||
"""Test that input_text context works."""
|
||||
assert await async_setup_component(hass, 'input_text', {
|
||||
'input_text': {
|
||||
't1': {
|
||||
'initial': 'bla',
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
state = hass.states.get('input_text.t1')
|
||||
assert state is not None
|
||||
|
||||
await hass.services.async_call('input_text', 'set_value', {
|
||||
'entity_id': state.entity_id,
|
||||
'value': 'new_value',
|
||||
}, True, Context(user_id='abcd'))
|
||||
|
||||
state2 = hass.states.get('input_text.t1')
|
||||
assert state2 is not None
|
||||
assert state.state != state2.state
|
||||
assert state2.context.user_id == 'abcd'
|
||||
Reference in new issue
Block a user