Update fan/demo tests to async (#18109)

* Update fan/demo tests to async

* Use async_create_task
This commit is contained in:
Adam Mills 2018-11-02 16:08:22 -04:00 committed by Paulus Schoutsen
parent dd938d7460
commit 1f290bad94
3 changed files with 678 additions and 663 deletions

View File

@ -9,10 +9,12 @@ from homeassistant.components.fan import (
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, SERVICE_TURN_ON, SERVICE_TURN_OFF) ATTR_ENTITY_ID, SERVICE_TURN_ON, SERVICE_TURN_OFF)
from homeassistant.loader import bind_hass from homeassistant.loader import bind_hass
from homeassistant.core import callback
@callback
@bind_hass @bind_hass
def turn_on(hass, entity_id: str = None, speed: str = None) -> None: def async_turn_on(hass, entity_id: str = None, speed: str = None) -> None:
"""Turn all or specified fan on.""" """Turn all or specified fan on."""
data = { data = {
key: value for key, value in [ key: value for key, value in [
@ -21,19 +23,23 @@ def turn_on(hass, entity_id: str = None, speed: str = None) -> None:
] if value is not None ] if value is not None
} }
hass.services.call(DOMAIN, SERVICE_TURN_ON, data) hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_TURN_ON, data))
@callback
@bind_hass @bind_hass
def turn_off(hass, entity_id: str = None) -> None: def async_turn_off(hass, entity_id: str = None) -> None:
"""Turn all or specified fan off.""" """Turn all or specified fan off."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
hass.services.call(DOMAIN, SERVICE_TURN_OFF, data) hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_TURN_OFF, data))
@callback
@bind_hass @bind_hass
def oscillate(hass, entity_id: str = None, def async_oscillate(hass, entity_id: str = None,
should_oscillate: bool = True) -> None: should_oscillate: bool = True) -> None:
"""Set oscillation on all or specified fan.""" """Set oscillation on all or specified fan."""
data = { data = {
@ -43,11 +49,13 @@ def oscillate(hass, entity_id: str = None,
] if value is not None ] if value is not None
} }
hass.services.call(DOMAIN, SERVICE_OSCILLATE, data) hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_OSCILLATE, data))
@callback
@bind_hass @bind_hass
def set_speed(hass, entity_id: str = None, speed: str = None) -> None: def async_set_speed(hass, entity_id: str = None, speed: str = None) -> None:
"""Set speed for all or specified fan.""" """Set speed for all or specified fan."""
data = { data = {
key: value for key, value in [ key: value for key, value in [
@ -56,11 +64,14 @@ def set_speed(hass, entity_id: str = None, speed: str = None) -> None:
] if value is not None ] if value is not None
} }
hass.services.call(DOMAIN, SERVICE_SET_SPEED, data) hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_SET_SPEED, data))
@callback
@bind_hass @bind_hass
def set_direction(hass, entity_id: str = None, direction: str = None) -> None: def async_set_direction(
hass, entity_id: str = None, direction: str = None) -> None:
"""Set direction for all or specified fan.""" """Set direction for all or specified fan."""
data = { data = {
key: value for key, value in [ key: value for key, value in [
@ -69,4 +80,5 @@ def set_direction(hass, entity_id: str = None, direction: str = None) -> None:
] if value is not None ] if value is not None
} }
hass.services.call(DOMAIN, SERVICE_SET_DIRECTION, data) hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_SET_DIRECTION, data))

View File

@ -1,108 +1,108 @@
"""Test cases around the demo fan platform.""" """Test cases around the demo fan platform."""
import pytest
import unittest from homeassistant.setup import async_setup_component
from homeassistant.setup import setup_component
from homeassistant.components import fan from homeassistant.components import fan
from homeassistant.const import STATE_OFF, STATE_ON from homeassistant.const import STATE_OFF, STATE_ON
from tests.common import get_test_home_assistant
from tests.components.fan import common from tests.components.fan import common
FAN_ENTITY_ID = 'fan.living_room_fan' FAN_ENTITY_ID = 'fan.living_room_fan'
class TestDemoFan(unittest.TestCase): def get_entity(hass):
"""Test the fan demo platform."""
def get_entity(self):
"""Get the fan entity.""" """Get the fan entity."""
return self.hass.states.get(FAN_ENTITY_ID) return hass.states.get(FAN_ENTITY_ID)
def setUp(self):
"""Initialize unit test data.""" @pytest.fixture(autouse=True)
self.hass = get_test_home_assistant() def setup_comp(hass):
assert setup_component(self.hass, fan.DOMAIN, {'fan': { """Initialize components."""
hass.loop.run_until_complete(async_setup_component(hass, fan.DOMAIN, {
'fan': {
'platform': 'demo', 'platform': 'demo',
}}) }
self.hass.block_till_done() }))
def tearDown(self):
"""Tear down unit test data."""
self.hass.stop()
def test_turn_on(self): async def test_turn_on(hass):
"""Test turning on the device.""" """Test turning on the device."""
assert STATE_OFF == self.get_entity().state assert STATE_OFF == get_entity(hass).state
common.turn_on(self.hass, FAN_ENTITY_ID) common.async_turn_on(hass, FAN_ENTITY_ID)
self.hass.block_till_done() await hass.async_block_till_done()
assert STATE_OFF != self.get_entity().state assert STATE_OFF != get_entity(hass).state
common.turn_on(self.hass, FAN_ENTITY_ID, fan.SPEED_HIGH) common.async_turn_on(hass, FAN_ENTITY_ID, fan.SPEED_HIGH)
self.hass.block_till_done() await hass.async_block_till_done()
assert STATE_ON == self.get_entity().state assert STATE_ON == get_entity(hass).state
assert fan.SPEED_HIGH == \ assert fan.SPEED_HIGH == \
self.get_entity().attributes[fan.ATTR_SPEED] get_entity(hass).attributes[fan.ATTR_SPEED]
def test_turn_off(self):
async def test_turn_off(hass):
"""Test turning off the device.""" """Test turning off the device."""
assert STATE_OFF == self.get_entity().state assert STATE_OFF == get_entity(hass).state
common.turn_on(self.hass, FAN_ENTITY_ID) common.async_turn_on(hass, FAN_ENTITY_ID)
self.hass.block_till_done() await hass.async_block_till_done()
assert STATE_OFF != self.get_entity().state assert STATE_OFF != get_entity(hass).state
common.turn_off(self.hass, FAN_ENTITY_ID) common.async_turn_off(hass, FAN_ENTITY_ID)
self.hass.block_till_done() await hass.async_block_till_done()
assert STATE_OFF == self.get_entity().state assert STATE_OFF == get_entity(hass).state
def test_turn_off_without_entity_id(self):
async def test_turn_off_without_entity_id(hass):
"""Test turning off all fans.""" """Test turning off all fans."""
assert STATE_OFF == self.get_entity().state assert STATE_OFF == get_entity(hass).state
common.turn_on(self.hass, FAN_ENTITY_ID) common.async_turn_on(hass, FAN_ENTITY_ID)
self.hass.block_till_done() await hass.async_block_till_done()
assert STATE_OFF != self.get_entity().state assert STATE_OFF != get_entity(hass).state
common.turn_off(self.hass) common.async_turn_off(hass)
self.hass.block_till_done() await hass.async_block_till_done()
assert STATE_OFF == self.get_entity().state assert STATE_OFF == get_entity(hass).state
def test_set_direction(self):
async def test_set_direction(hass):
"""Test setting the direction of the device.""" """Test setting the direction of the device."""
assert STATE_OFF == self.get_entity().state assert STATE_OFF == get_entity(hass).state
common.set_direction(self.hass, FAN_ENTITY_ID, fan.DIRECTION_REVERSE) common.async_set_direction(hass, FAN_ENTITY_ID, fan.DIRECTION_REVERSE)
self.hass.block_till_done() await hass.async_block_till_done()
assert fan.DIRECTION_REVERSE == \ assert fan.DIRECTION_REVERSE == \
self.get_entity().attributes.get('direction') get_entity(hass).attributes.get('direction')
def test_set_speed(self):
async def test_set_speed(hass):
"""Test setting the speed of the device.""" """Test setting the speed of the device."""
assert STATE_OFF == self.get_entity().state assert STATE_OFF == get_entity(hass).state
common.set_speed(self.hass, FAN_ENTITY_ID, fan.SPEED_LOW) common.async_set_speed(hass, FAN_ENTITY_ID, fan.SPEED_LOW)
self.hass.block_till_done() await hass.async_block_till_done()
assert fan.SPEED_LOW == \ assert fan.SPEED_LOW == \
self.get_entity().attributes.get('speed') get_entity(hass).attributes.get('speed')
def test_oscillate(self):
async def test_oscillate(hass):
"""Test oscillating the fan.""" """Test oscillating the fan."""
assert not self.get_entity().attributes.get('oscillating') assert not get_entity(hass).attributes.get('oscillating')
common.oscillate(self.hass, FAN_ENTITY_ID, True) common.async_oscillate(hass, FAN_ENTITY_ID, True)
self.hass.block_till_done() await hass.async_block_till_done()
assert self.get_entity().attributes.get('oscillating') assert get_entity(hass).attributes.get('oscillating')
common.oscillate(self.hass, FAN_ENTITY_ID, False) common.async_oscillate(hass, FAN_ENTITY_ID, False)
self.hass.block_till_done() await hass.async_block_till_done()
assert not self.get_entity().attributes.get('oscillating') assert not get_entity(hass).attributes.get('oscillating')
def test_is_on(self):
async def test_is_on(hass):
"""Test is on service call.""" """Test is on service call."""
assert not fan.is_on(self.hass, FAN_ENTITY_ID) assert not fan.is_on(hass, FAN_ENTITY_ID)
common.turn_on(self.hass, FAN_ENTITY_ID) common.async_turn_on(hass, FAN_ENTITY_ID)
self.hass.block_till_done() await hass.async_block_till_done()
assert fan.is_on(self.hass, FAN_ENTITY_ID) assert fan.is_on(hass, FAN_ENTITY_ID)

View File

@ -1,7 +1,7 @@
"""The tests for the Template fan platform.""" """The tests for the Template fan platform."""
import logging import logging
import pytest
from homeassistant.core import callback
from homeassistant import setup from homeassistant import setup
from homeassistant.const import STATE_ON, STATE_OFF from homeassistant.const import STATE_ON, STATE_OFF
from homeassistant.components.fan import ( from homeassistant.components.fan import (
@ -9,7 +9,7 @@ from homeassistant.components.fan import (
ATTR_DIRECTION, DIRECTION_FORWARD, DIRECTION_REVERSE) ATTR_DIRECTION, DIRECTION_FORWARD, DIRECTION_REVERSE)
from tests.common import ( from tests.common import (
get_test_home_assistant, assert_setup_component) async_mock_service, assert_setup_component)
from tests.components.fan import common from tests.components.fan import common
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -26,35 +26,17 @@ _OSC_INPUT = 'input_select.osc'
_DIRECTION_INPUT_SELECT = 'input_select.direction' _DIRECTION_INPUT_SELECT = 'input_select.direction'
class TestTemplateFan: @pytest.fixture
"""Test the Template light.""" def calls(hass):
"""Track calls to a mock serivce."""
return async_mock_service(hass, 'test', 'automation')
hass = None
calls = None
# pylint: disable=invalid-name
def setup_method(self, method): # Configuration tests #
"""Set up.""" async def test_missing_optional_config(hass, calls):
self.hass = get_test_home_assistant()
self.calls = []
@callback
def record_call(service):
"""Track function calls.."""
self.calls.append(service)
self.hass.services.register('test', 'automation', record_call)
def teardown_method(self, method):
"""Stop everything that was started."""
self.hass.stop()
# Configuration tests #
def test_missing_optional_config(self):
"""Test: missing optional template is ok.""" """Test: missing optional template is ok."""
with assert_setup_component(1, 'fan'): with assert_setup_component(1, 'fan'):
assert setup.setup_component(self.hass, 'fan', { assert await setup.async_setup_component(hass, 'fan', {
'fan': { 'fan': {
'platform': 'template', 'platform': 'template',
'fans': { 'fans': {
@ -72,15 +54,16 @@ class TestTemplateFan:
} }
}) })
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
self._verify(STATE_ON, None, None, None) _verify(hass, STATE_ON, None, None, None)
def test_missing_value_template_config(self):
async def test_missing_value_template_config(hass, calls):
"""Test: missing 'value_template' will fail.""" """Test: missing 'value_template' will fail."""
with assert_setup_component(0, 'fan'): with assert_setup_component(0, 'fan'):
assert setup.setup_component(self.hass, 'fan', { assert await setup.async_setup_component(hass, 'fan', {
'fan': { 'fan': {
'platform': 'template', 'platform': 'template',
'fans': { 'fans': {
@ -96,15 +79,16 @@ class TestTemplateFan:
} }
}) })
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
assert self.hass.states.all() == [] assert hass.states.async_all() == []
def test_missing_turn_on_config(self):
async def test_missing_turn_on_config(hass, calls):
"""Test: missing 'turn_on' will fail.""" """Test: missing 'turn_on' will fail."""
with assert_setup_component(0, 'fan'): with assert_setup_component(0, 'fan'):
assert setup.setup_component(self.hass, 'fan', { assert await setup.async_setup_component(hass, 'fan', {
'fan': { 'fan': {
'platform': 'template', 'platform': 'template',
'fans': { 'fans': {
@ -118,15 +102,16 @@ class TestTemplateFan:
} }
}) })
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
assert self.hass.states.all() == [] assert hass.states.async_all() == []
def test_missing_turn_off_config(self):
async def test_missing_turn_off_config(hass, calls):
"""Test: missing 'turn_off' will fail.""" """Test: missing 'turn_off' will fail."""
with assert_setup_component(0, 'fan'): with assert_setup_component(0, 'fan'):
assert setup.setup_component(self.hass, 'fan', { assert await setup.async_setup_component(hass, 'fan', {
'fan': { 'fan': {
'platform': 'template', 'platform': 'template',
'fans': { 'fans': {
@ -140,15 +125,16 @@ class TestTemplateFan:
} }
}) })
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
assert self.hass.states.all() == [] assert hass.states.async_all() == []
def test_invalid_config(self):
async def test_invalid_config(hass, calls):
"""Test: missing 'turn_off' will fail.""" """Test: missing 'turn_off' will fail."""
with assert_setup_component(0, 'fan'): with assert_setup_component(0, 'fan'):
assert setup.setup_component(self.hass, 'fan', { assert await setup.async_setup_component(hass, 'fan', {
'platform': 'template', 'platform': 'template',
'fans': { 'fans': {
'test_fan': { 'test_fan': {
@ -160,15 +146,16 @@ class TestTemplateFan:
} }
}) })
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
assert self.hass.states.all() == [] assert hass.states.async_all() == []
# End of configuration tests # # End of configuration tests #
# Template tests #
def test_templates_with_entities(self): # Template tests #
async def test_templates_with_entities(hass, calls):
"""Test tempalates with values from other entities.""" """Test tempalates with values from other entities."""
value_template = """ value_template = """
{% if is_state('input_boolean.state', 'True') %} {% if is_state('input_boolean.state', 'True') %}
@ -179,7 +166,7 @@ class TestTemplateFan:
""" """
with assert_setup_component(1, 'fan'): with assert_setup_component(1, 'fan'):
assert setup.setup_component(self.hass, 'fan', { assert await setup.async_setup_component(hass, 'fan', {
'fan': { 'fan': {
'platform': 'template', 'platform': 'template',
'fans': { 'fans': {
@ -202,23 +189,24 @@ class TestTemplateFan:
} }
}) })
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
self._verify(STATE_OFF, None, None, None) _verify(hass, STATE_OFF, None, None, None)
self.hass.states.set(_STATE_INPUT_BOOLEAN, True) hass.states.async_set(_STATE_INPUT_BOOLEAN, True)
self.hass.states.set(_SPEED_INPUT_SELECT, SPEED_MEDIUM) hass.states.async_set(_SPEED_INPUT_SELECT, SPEED_MEDIUM)
self.hass.states.set(_OSC_INPUT, 'True') hass.states.async_set(_OSC_INPUT, 'True')
self.hass.states.set(_DIRECTION_INPUT_SELECT, DIRECTION_FORWARD) hass.states.async_set(_DIRECTION_INPUT_SELECT, DIRECTION_FORWARD)
self.hass.block_till_done() await hass.async_block_till_done()
self._verify(STATE_ON, SPEED_MEDIUM, True, DIRECTION_FORWARD) _verify(hass, STATE_ON, SPEED_MEDIUM, True, DIRECTION_FORWARD)
def test_templates_with_valid_values(self):
async def test_templates_with_valid_values(hass, calls):
"""Test templates with valid values.""" """Test templates with valid values."""
with assert_setup_component(1, 'fan'): with assert_setup_component(1, 'fan'):
assert setup.setup_component(self.hass, 'fan', { assert await setup.async_setup_component(hass, 'fan', {
'fan': { 'fan': {
'platform': 'template', 'platform': 'template',
'fans': { 'fans': {
@ -243,15 +231,16 @@ class TestTemplateFan:
} }
}) })
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
self._verify(STATE_ON, SPEED_MEDIUM, True, DIRECTION_FORWARD) _verify(hass, STATE_ON, SPEED_MEDIUM, True, DIRECTION_FORWARD)
def test_templates_invalid_values(self):
async def test_templates_invalid_values(hass, calls):
"""Test templates with invalid values.""" """Test templates with invalid values."""
with assert_setup_component(1, 'fan'): with assert_setup_component(1, 'fan'):
assert setup.setup_component(self.hass, 'fan', { assert await setup.async_setup_component(hass, 'fan', {
'fan': { 'fan': {
'platform': 'template', 'platform': 'template',
'fans': { 'fans': {
@ -276,288 +265,302 @@ class TestTemplateFan:
} }
}) })
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()
self._verify(STATE_OFF, None, None, None) _verify(hass, STATE_OFF, None, None, None)
# End of template tests # # End of template tests #
# Function tests #
def test_on_off(self): # Function tests #
async def test_on_off(hass, calls):
"""Test turn on and turn off.""" """Test turn on and turn off."""
self._register_components() await _register_components(hass)
# Turn on fan # Turn on fan
common.turn_on(self.hass, _TEST_FAN) common.async_turn_on(hass, _TEST_FAN)
self.hass.block_till_done() await hass.async_block_till_done()
# verify # verify
assert self.hass.states.get(_STATE_INPUT_BOOLEAN).state == STATE_ON assert hass.states.get(_STATE_INPUT_BOOLEAN).state == STATE_ON
self._verify(STATE_ON, None, None, None) _verify(hass, STATE_ON, None, None, None)
# Turn off fan # Turn off fan
common.turn_off(self.hass, _TEST_FAN) common.async_turn_off(hass, _TEST_FAN)
self.hass.block_till_done() await hass.async_block_till_done()
# verify # verify
assert self.hass.states.get(_STATE_INPUT_BOOLEAN).state == STATE_OFF assert hass.states.get(_STATE_INPUT_BOOLEAN).state == STATE_OFF
self._verify(STATE_OFF, None, None, None) _verify(hass, STATE_OFF, None, None, None)
def test_on_with_speed(self):
async def test_on_with_speed(hass, calls):
"""Test turn on with speed.""" """Test turn on with speed."""
self._register_components() await _register_components(hass)
# Turn on fan with high speed # Turn on fan with high speed
common.turn_on(self.hass, _TEST_FAN, SPEED_HIGH) common.async_turn_on(hass, _TEST_FAN, SPEED_HIGH)
self.hass.block_till_done() await hass.async_block_till_done()
# verify # verify
assert self.hass.states.get(_STATE_INPUT_BOOLEAN).state == STATE_ON assert hass.states.get(_STATE_INPUT_BOOLEAN).state == STATE_ON
assert self.hass.states.get(_SPEED_INPUT_SELECT).state == SPEED_HIGH assert hass.states.get(_SPEED_INPUT_SELECT).state == SPEED_HIGH
self._verify(STATE_ON, SPEED_HIGH, None, None) _verify(hass, STATE_ON, SPEED_HIGH, None, None)
def test_set_speed(self):
async def test_set_speed(hass, calls):
"""Test set valid speed.""" """Test set valid speed."""
self._register_components() await _register_components(hass)
# Turn on fan # Turn on fan
common.turn_on(self.hass, _TEST_FAN) common.async_turn_on(hass, _TEST_FAN)
self.hass.block_till_done() await hass.async_block_till_done()
# Set fan's speed to high # Set fan's speed to high
common.set_speed(self.hass, _TEST_FAN, SPEED_HIGH) common.async_set_speed(hass, _TEST_FAN, SPEED_HIGH)
self.hass.block_till_done() await hass.async_block_till_done()
# verify # verify
assert self.hass.states.get(_SPEED_INPUT_SELECT).state == SPEED_HIGH assert hass.states.get(_SPEED_INPUT_SELECT).state == SPEED_HIGH
self._verify(STATE_ON, SPEED_HIGH, None, None) _verify(hass, STATE_ON, SPEED_HIGH, None, None)
# Set fan's speed to medium # Set fan's speed to medium
common.set_speed(self.hass, _TEST_FAN, SPEED_MEDIUM) common.async_set_speed(hass, _TEST_FAN, SPEED_MEDIUM)
self.hass.block_till_done() await hass.async_block_till_done()
# verify # verify
assert self.hass.states.get(_SPEED_INPUT_SELECT).state == SPEED_MEDIUM assert hass.states.get(_SPEED_INPUT_SELECT).state == SPEED_MEDIUM
self._verify(STATE_ON, SPEED_MEDIUM, None, None) _verify(hass, STATE_ON, SPEED_MEDIUM, None, None)
def test_set_invalid_speed_from_initial_stage(self):
async def test_set_invalid_speed_from_initial_stage(hass, calls):
"""Test set invalid speed when fan is in initial state.""" """Test set invalid speed when fan is in initial state."""
self._register_components() await _register_components(hass)
# Turn on fan # Turn on fan
common.turn_on(self.hass, _TEST_FAN) common.async_turn_on(hass, _TEST_FAN)
self.hass.block_till_done() await hass.async_block_till_done()
# Set fan's speed to 'invalid' # Set fan's speed to 'invalid'
common.set_speed(self.hass, _TEST_FAN, 'invalid') common.async_set_speed(hass, _TEST_FAN, 'invalid')
self.hass.block_till_done() await hass.async_block_till_done()
# verify speed is unchanged # verify speed is unchanged
assert self.hass.states.get(_SPEED_INPUT_SELECT).state == '' assert hass.states.get(_SPEED_INPUT_SELECT).state == ''
self._verify(STATE_ON, None, None, None) _verify(hass, STATE_ON, None, None, None)
def test_set_invalid_speed(self):
async def test_set_invalid_speed(hass, calls):
"""Test set invalid speed when fan has valid speed.""" """Test set invalid speed when fan has valid speed."""
self._register_components() await _register_components(hass)
# Turn on fan # Turn on fan
common.turn_on(self.hass, _TEST_FAN) common.async_turn_on(hass, _TEST_FAN)
self.hass.block_till_done() await hass.async_block_till_done()
# Set fan's speed to high # Set fan's speed to high
common.set_speed(self.hass, _TEST_FAN, SPEED_HIGH) common.async_set_speed(hass, _TEST_FAN, SPEED_HIGH)
self.hass.block_till_done() await hass.async_block_till_done()
# verify # verify
assert self.hass.states.get(_SPEED_INPUT_SELECT).state == SPEED_HIGH assert hass.states.get(_SPEED_INPUT_SELECT).state == SPEED_HIGH
self._verify(STATE_ON, SPEED_HIGH, None, None) _verify(hass, STATE_ON, SPEED_HIGH, None, None)
# Set fan's speed to 'invalid' # Set fan's speed to 'invalid'
common.set_speed(self.hass, _TEST_FAN, 'invalid') common.async_set_speed(hass, _TEST_FAN, 'invalid')
self.hass.block_till_done() await hass.async_block_till_done()
# verify speed is unchanged # verify speed is unchanged
assert self.hass.states.get(_SPEED_INPUT_SELECT).state == SPEED_HIGH assert hass.states.get(_SPEED_INPUT_SELECT).state == SPEED_HIGH
self._verify(STATE_ON, SPEED_HIGH, None, None) _verify(hass, STATE_ON, SPEED_HIGH, None, None)
def test_custom_speed_list(self):
async def test_custom_speed_list(hass, calls):
"""Test set custom speed list.""" """Test set custom speed list."""
self._register_components(['1', '2', '3']) await _register_components(hass, ['1', '2', '3'])
# Turn on fan # Turn on fan
common.turn_on(self.hass, _TEST_FAN) common.async_turn_on(hass, _TEST_FAN)
self.hass.block_till_done() await hass.async_block_till_done()
# Set fan's speed to '1' # Set fan's speed to '1'
common.set_speed(self.hass, _TEST_FAN, '1') common.async_set_speed(hass, _TEST_FAN, '1')
self.hass.block_till_done() await hass.async_block_till_done()
# verify # verify
assert self.hass.states.get(_SPEED_INPUT_SELECT).state == '1' assert hass.states.get(_SPEED_INPUT_SELECT).state == '1'
self._verify(STATE_ON, '1', None, None) _verify(hass, STATE_ON, '1', None, None)
# Set fan's speed to 'medium' which is invalid # Set fan's speed to 'medium' which is invalid
common.set_speed(self.hass, _TEST_FAN, SPEED_MEDIUM) common.async_set_speed(hass, _TEST_FAN, SPEED_MEDIUM)
self.hass.block_till_done() await hass.async_block_till_done()
# verify that speed is unchanged # verify that speed is unchanged
assert self.hass.states.get(_SPEED_INPUT_SELECT).state == '1' assert hass.states.get(_SPEED_INPUT_SELECT).state == '1'
self._verify(STATE_ON, '1', None, None) _verify(hass, STATE_ON, '1', None, None)
def test_set_osc(self):
async def test_set_osc(hass, calls):
"""Test set oscillating.""" """Test set oscillating."""
self._register_components() await _register_components(hass)
# Turn on fan # Turn on fan
common.turn_on(self.hass, _TEST_FAN) common.async_turn_on(hass, _TEST_FAN)
self.hass.block_till_done() await hass.async_block_till_done()
# Set fan's osc to True # Set fan's osc to True
common.oscillate(self.hass, _TEST_FAN, True) common.async_oscillate(hass, _TEST_FAN, True)
self.hass.block_till_done() await hass.async_block_till_done()
# verify # verify
assert self.hass.states.get(_OSC_INPUT).state == 'True' assert hass.states.get(_OSC_INPUT).state == 'True'
self._verify(STATE_ON, None, True, None) _verify(hass, STATE_ON, None, True, None)
# Set fan's osc to False # Set fan's osc to False
common.oscillate(self.hass, _TEST_FAN, False) common.async_oscillate(hass, _TEST_FAN, False)
self.hass.block_till_done() await hass.async_block_till_done()
# verify # verify
assert self.hass.states.get(_OSC_INPUT).state == 'False' assert hass.states.get(_OSC_INPUT).state == 'False'
self._verify(STATE_ON, None, False, None) _verify(hass, STATE_ON, None, False, None)
def test_set_invalid_osc_from_initial_state(self):
async def test_set_invalid_osc_from_initial_state(hass, calls):
"""Test set invalid oscillating when fan is in initial state.""" """Test set invalid oscillating when fan is in initial state."""
self._register_components() await _register_components(hass)
# Turn on fan # Turn on fan
common.turn_on(self.hass, _TEST_FAN) common.async_turn_on(hass, _TEST_FAN)
self.hass.block_till_done() await hass.async_block_till_done()
# Set fan's osc to 'invalid' # Set fan's osc to 'invalid'
common.oscillate(self.hass, _TEST_FAN, 'invalid') common.async_oscillate(hass, _TEST_FAN, 'invalid')
self.hass.block_till_done() await hass.async_block_till_done()
# verify # verify
assert self.hass.states.get(_OSC_INPUT).state == '' assert hass.states.get(_OSC_INPUT).state == ''
self._verify(STATE_ON, None, None, None) _verify(hass, STATE_ON, None, None, None)
def test_set_invalid_osc(self):
async def test_set_invalid_osc(hass, calls):
"""Test set invalid oscillating when fan has valid osc.""" """Test set invalid oscillating when fan has valid osc."""
self._register_components() await _register_components(hass)
# Turn on fan # Turn on fan
common.turn_on(self.hass, _TEST_FAN) common.async_turn_on(hass, _TEST_FAN)
self.hass.block_till_done() await hass.async_block_till_done()
# Set fan's osc to True # Set fan's osc to True
common.oscillate(self.hass, _TEST_FAN, True) common.async_oscillate(hass, _TEST_FAN, True)
self.hass.block_till_done() await hass.async_block_till_done()
# verify # verify
assert self.hass.states.get(_OSC_INPUT).state == 'True' assert hass.states.get(_OSC_INPUT).state == 'True'
self._verify(STATE_ON, None, True, None) _verify(hass, STATE_ON, None, True, None)
# Set fan's osc to False # Set fan's osc to False
common.oscillate(self.hass, _TEST_FAN, None) common.async_oscillate(hass, _TEST_FAN, None)
self.hass.block_till_done() await hass.async_block_till_done()
# verify osc is unchanged # verify osc is unchanged
assert self.hass.states.get(_OSC_INPUT).state == 'True' assert hass.states.get(_OSC_INPUT).state == 'True'
self._verify(STATE_ON, None, True, None) _verify(hass, STATE_ON, None, True, None)
def test_set_direction(self):
async def test_set_direction(hass, calls):
"""Test set valid direction.""" """Test set valid direction."""
self._register_components() await _register_components(hass)
# Turn on fan # Turn on fan
common.turn_on(self.hass, _TEST_FAN) common.async_turn_on(hass, _TEST_FAN)
self.hass.block_till_done() await hass.async_block_till_done()
# Set fan's direction to forward # Set fan's direction to forward
common.set_direction(self.hass, _TEST_FAN, DIRECTION_FORWARD) common.async_set_direction(hass, _TEST_FAN, DIRECTION_FORWARD)
self.hass.block_till_done() await hass.async_block_till_done()
# verify # verify
assert self.hass.states.get(_DIRECTION_INPUT_SELECT).state \ assert hass.states.get(_DIRECTION_INPUT_SELECT).state \
== DIRECTION_FORWARD == DIRECTION_FORWARD
self._verify(STATE_ON, None, None, DIRECTION_FORWARD) _verify(hass, STATE_ON, None, None, DIRECTION_FORWARD)
# Set fan's direction to reverse # Set fan's direction to reverse
common.set_direction(self.hass, _TEST_FAN, DIRECTION_REVERSE) common.async_set_direction(hass, _TEST_FAN, DIRECTION_REVERSE)
self.hass.block_till_done() await hass.async_block_till_done()
# verify # verify
assert self.hass.states.get(_DIRECTION_INPUT_SELECT).state \ assert hass.states.get(_DIRECTION_INPUT_SELECT).state \
== DIRECTION_REVERSE == DIRECTION_REVERSE
self._verify(STATE_ON, None, None, DIRECTION_REVERSE) _verify(hass, STATE_ON, None, None, DIRECTION_REVERSE)
def test_set_invalid_direction_from_initial_stage(self):
async def test_set_invalid_direction_from_initial_stage(hass, calls):
"""Test set invalid direction when fan is in initial state.""" """Test set invalid direction when fan is in initial state."""
self._register_components() await _register_components(hass)
# Turn on fan # Turn on fan
common.turn_on(self.hass, _TEST_FAN) common.async_turn_on(hass, _TEST_FAN)
self.hass.block_till_done() await hass.async_block_till_done()
# Set fan's direction to 'invalid' # Set fan's direction to 'invalid'
common.set_direction(self.hass, _TEST_FAN, 'invalid') common.async_set_direction(hass, _TEST_FAN, 'invalid')
self.hass.block_till_done() await hass.async_block_till_done()
# verify direction is unchanged # verify direction is unchanged
assert self.hass.states.get(_DIRECTION_INPUT_SELECT).state == '' assert hass.states.get(_DIRECTION_INPUT_SELECT).state == ''
self._verify(STATE_ON, None, None, None) _verify(hass, STATE_ON, None, None, None)
def test_set_invalid_direction(self):
async def test_set_invalid_direction(hass, calls):
"""Test set invalid direction when fan has valid direction.""" """Test set invalid direction when fan has valid direction."""
self._register_components() await _register_components(hass)
# Turn on fan # Turn on fan
common.turn_on(self.hass, _TEST_FAN) common.async_turn_on(hass, _TEST_FAN)
self.hass.block_till_done() await hass.async_block_till_done()
# Set fan's direction to forward # Set fan's direction to forward
common.set_direction(self.hass, _TEST_FAN, DIRECTION_FORWARD) common.async_set_direction(hass, _TEST_FAN, DIRECTION_FORWARD)
self.hass.block_till_done() await hass.async_block_till_done()
# verify # verify
assert self.hass.states.get(_DIRECTION_INPUT_SELECT).state == \ assert hass.states.get(_DIRECTION_INPUT_SELECT).state == \
DIRECTION_FORWARD DIRECTION_FORWARD
self._verify(STATE_ON, None, None, DIRECTION_FORWARD) _verify(hass, STATE_ON, None, None, DIRECTION_FORWARD)
# Set fan's direction to 'invalid' # Set fan's direction to 'invalid'
common.set_direction(self.hass, _TEST_FAN, 'invalid') common.async_set_direction(hass, _TEST_FAN, 'invalid')
self.hass.block_till_done() await hass.async_block_till_done()
# verify direction is unchanged # verify direction is unchanged
assert self.hass.states.get(_DIRECTION_INPUT_SELECT).state == \ assert hass.states.get(_DIRECTION_INPUT_SELECT).state == \
DIRECTION_FORWARD DIRECTION_FORWARD
self._verify(STATE_ON, None, None, DIRECTION_FORWARD) _verify(hass, STATE_ON, None, None, DIRECTION_FORWARD)
def _verify(self, expected_state, expected_speed, expected_oscillating,
def _verify(hass, expected_state, expected_speed, expected_oscillating,
expected_direction): expected_direction):
"""Verify fan's state, speed and osc.""" """Verify fan's state, speed and osc."""
state = self.hass.states.get(_TEST_FAN) state = hass.states.get(_TEST_FAN)
attributes = state.attributes attributes = state.attributes
assert state.state == expected_state assert state.state == expected_state
assert attributes.get(ATTR_SPEED, None) == expected_speed assert attributes.get(ATTR_SPEED, None) == expected_speed
assert attributes.get(ATTR_OSCILLATING, None) == expected_oscillating assert attributes.get(ATTR_OSCILLATING, None) == expected_oscillating
assert attributes.get(ATTR_DIRECTION, None) == expected_direction assert attributes.get(ATTR_DIRECTION, None) == expected_direction
def _register_components(self, speed_list=None):
async def _register_components(hass, speed_list=None):
"""Register basic components for testing.""" """Register basic components for testing."""
with assert_setup_component(1, 'input_boolean'): with assert_setup_component(1, 'input_boolean'):
assert setup.setup_component( assert await setup.async_setup_component(
self.hass, hass,
'input_boolean', 'input_boolean',
{'input_boolean': {'state': None}} {'input_boolean': {'state': None}}
) )
with assert_setup_component(3, 'input_select'): with assert_setup_component(3, 'input_select'):
assert setup.setup_component(self.hass, 'input_select', { assert await setup.async_setup_component(hass, 'input_select', {
'input_select': { 'input_select': {
'speed': { 'speed': {
'name': 'Speed', 'name': 'Speed',
@ -632,7 +635,7 @@ class TestTemplateFan:
if speed_list: if speed_list:
test_fan_config['speeds'] = speed_list test_fan_config['speeds'] = speed_list
assert setup.setup_component(self.hass, 'fan', { assert await setup.async_setup_component(hass, 'fan', {
'fan': { 'fan': {
'platform': 'template', 'platform': 'template',
'fans': { 'fans': {
@ -641,5 +644,5 @@ class TestTemplateFan:
} }
}) })
self.hass.start() await hass.async_start()
self.hass.block_till_done() await hass.async_block_till_done()