Async tests for counter (#18684)
This commit is contained in:
parent
66f1643de5
commit
6f0a3b4b22
2 changed files with 110 additions and 138 deletions
|
@ -10,12 +10,6 @@ from homeassistant.core import callback
|
|||
from homeassistant.loader import bind_hass
|
||||
|
||||
|
||||
@bind_hass
|
||||
def increment(hass, entity_id):
|
||||
"""Increment a counter."""
|
||||
hass.add_job(async_increment, hass, entity_id)
|
||||
|
||||
|
||||
@callback
|
||||
@bind_hass
|
||||
def async_increment(hass, entity_id):
|
||||
|
@ -24,12 +18,6 @@ def async_increment(hass, entity_id):
|
|||
DOMAIN, SERVICE_INCREMENT, {ATTR_ENTITY_ID: entity_id}))
|
||||
|
||||
|
||||
@bind_hass
|
||||
def decrement(hass, entity_id):
|
||||
"""Decrement a counter."""
|
||||
hass.add_job(async_decrement, hass, entity_id)
|
||||
|
||||
|
||||
@callback
|
||||
@bind_hass
|
||||
def async_decrement(hass, entity_id):
|
||||
|
@ -38,12 +26,6 @@ def async_decrement(hass, entity_id):
|
|||
DOMAIN, SERVICE_DECREMENT, {ATTR_ENTITY_ID: entity_id}))
|
||||
|
||||
|
||||
@bind_hass
|
||||
def reset(hass, entity_id):
|
||||
"""Reset a counter."""
|
||||
hass.add_job(async_reset, hass, entity_id)
|
||||
|
||||
|
||||
@callback
|
||||
@bind_hass
|
||||
def async_reset(hass, entity_id):
|
||||
|
|
|
@ -1,35 +1,22 @@
|
|||
"""The tests for the counter component."""
|
||||
# pylint: disable=protected-access
|
||||
import asyncio
|
||||
import unittest
|
||||
import logging
|
||||
|
||||
from homeassistant.core import CoreState, State, Context
|
||||
from homeassistant.setup import setup_component, async_setup_component
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.components.counter import (
|
||||
DOMAIN, CONF_INITIAL, CONF_RESTORE, CONF_STEP, CONF_NAME, CONF_ICON)
|
||||
from homeassistant.const import (ATTR_ICON, ATTR_FRIENDLY_NAME)
|
||||
|
||||
from tests.common import (get_test_home_assistant, mock_restore_cache)
|
||||
from tests.components.counter.common import decrement, increment, reset
|
||||
from tests.common import mock_restore_cache
|
||||
from tests.components.counter.common import (
|
||||
async_decrement, async_increment, async_reset)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class TestCounter(unittest.TestCase):
|
||||
"""Test the counter component."""
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
def setUp(self):
|
||||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
def tearDown(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def test_config(self):
|
||||
async def test_config(hass):
|
||||
"""Test config."""
|
||||
invalid_configs = [
|
||||
None,
|
||||
|
@ -39,13 +26,14 @@ class TestCounter(unittest.TestCase):
|
|||
]
|
||||
|
||||
for cfg in invalid_configs:
|
||||
assert not setup_component(self.hass, DOMAIN, {DOMAIN: cfg})
|
||||
assert not await async_setup_component(hass, DOMAIN, {DOMAIN: cfg})
|
||||
|
||||
def test_config_options(self):
|
||||
|
||||
async def test_config_options(hass):
|
||||
"""Test configuration options."""
|
||||
count_start = len(self.hass.states.entity_ids())
|
||||
count_start = len(hass.states.async_entity_ids())
|
||||
|
||||
_LOGGER.debug('ENTITIES @ start: %s', self.hass.states.entity_ids())
|
||||
_LOGGER.debug('ENTITIES @ start: %s', hass.states.async_entity_ids())
|
||||
|
||||
config = {
|
||||
DOMAIN: {
|
||||
|
@ -60,16 +48,16 @@ class TestCounter(unittest.TestCase):
|
|||
}
|
||||
}
|
||||
|
||||
assert setup_component(self.hass, 'counter', config)
|
||||
self.hass.block_till_done()
|
||||
assert await async_setup_component(hass, 'counter', config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
_LOGGER.debug('ENTITIES: %s', self.hass.states.entity_ids())
|
||||
_LOGGER.debug('ENTITIES: %s', hass.states.async_entity_ids())
|
||||
|
||||
assert count_start + 2 == len(self.hass.states.entity_ids())
|
||||
self.hass.block_till_done()
|
||||
assert count_start + 2 == len(hass.states.async_entity_ids())
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state_1 = self.hass.states.get('counter.test_1')
|
||||
state_2 = self.hass.states.get('counter.test_2')
|
||||
state_1 = hass.states.get('counter.test_1')
|
||||
state_2 = hass.states.get('counter.test_2')
|
||||
|
||||
assert state_1 is not None
|
||||
assert state_2 is not None
|
||||
|
@ -83,7 +71,8 @@ class TestCounter(unittest.TestCase):
|
|||
state_2.attributes.get(ATTR_FRIENDLY_NAME)
|
||||
assert 'mdi:work' == state_2.attributes.get(ATTR_ICON)
|
||||
|
||||
def test_methods(self):
|
||||
|
||||
async def test_methods(hass):
|
||||
"""Test increment, decrement, and reset methods."""
|
||||
config = {
|
||||
DOMAIN: {
|
||||
|
@ -91,38 +80,39 @@ class TestCounter(unittest.TestCase):
|
|||
}
|
||||
}
|
||||
|
||||
assert setup_component(self.hass, 'counter', config)
|
||||
assert await async_setup_component(hass, 'counter', config)
|
||||
|
||||
entity_id = 'counter.test_1'
|
||||
|
||||
state = self.hass.states.get(entity_id)
|
||||
state = hass.states.get(entity_id)
|
||||
assert 0 == int(state.state)
|
||||
|
||||
increment(self.hass, entity_id)
|
||||
self.hass.block_till_done()
|
||||
async_increment(hass, entity_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = self.hass.states.get(entity_id)
|
||||
state = hass.states.get(entity_id)
|
||||
assert 1 == int(state.state)
|
||||
|
||||
increment(self.hass, entity_id)
|
||||
self.hass.block_till_done()
|
||||
async_increment(hass, entity_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = self.hass.states.get(entity_id)
|
||||
state = hass.states.get(entity_id)
|
||||
assert 2 == int(state.state)
|
||||
|
||||
decrement(self.hass, entity_id)
|
||||
self.hass.block_till_done()
|
||||
async_decrement(hass, entity_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = self.hass.states.get(entity_id)
|
||||
state = hass.states.get(entity_id)
|
||||
assert 1 == int(state.state)
|
||||
|
||||
reset(self.hass, entity_id)
|
||||
self.hass.block_till_done()
|
||||
async_reset(hass, entity_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = self.hass.states.get(entity_id)
|
||||
state = hass.states.get(entity_id)
|
||||
assert 0 == int(state.state)
|
||||
|
||||
def test_methods_with_config(self):
|
||||
|
||||
async def test_methods_with_config(hass):
|
||||
"""Test increment, decrement, and reset methods with configuration."""
|
||||
config = {
|
||||
DOMAIN: {
|
||||
|
@ -134,29 +124,29 @@ class TestCounter(unittest.TestCase):
|
|||
}
|
||||
}
|
||||
|
||||
assert setup_component(self.hass, 'counter', config)
|
||||
assert await async_setup_component(hass, 'counter', config)
|
||||
|
||||
entity_id = 'counter.test'
|
||||
|
||||
state = self.hass.states.get(entity_id)
|
||||
state = hass.states.get(entity_id)
|
||||
assert 10 == int(state.state)
|
||||
|
||||
increment(self.hass, entity_id)
|
||||
self.hass.block_till_done()
|
||||
async_increment(hass, entity_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = self.hass.states.get(entity_id)
|
||||
state = hass.states.get(entity_id)
|
||||
assert 15 == int(state.state)
|
||||
|
||||
increment(self.hass, entity_id)
|
||||
self.hass.block_till_done()
|
||||
async_increment(hass, entity_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = self.hass.states.get(entity_id)
|
||||
state = hass.states.get(entity_id)
|
||||
assert 20 == int(state.state)
|
||||
|
||||
decrement(self.hass, entity_id)
|
||||
self.hass.block_till_done()
|
||||
async_decrement(hass, entity_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = self.hass.states.get(entity_id)
|
||||
state = hass.states.get(entity_id)
|
||||
assert 15 == int(state.state)
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue