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,20 +23,24 @@ 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 = {
key: value for key, value in [ key: value for key, value in [
@ -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.""" """Get the fan entity."""
return hass.states.get(FAN_ENTITY_ID)
def get_entity(self):
"""Get the fan entity."""
return self.hass.states.get(FAN_ENTITY_ID)
def setUp(self): @pytest.fixture(autouse=True)
"""Initialize unit test data.""" def setup_comp(hass):
self.hass = get_test_home_assistant() """Initialize components."""
assert setup_component(self.hass, fan.DOMAIN, {'fan': { 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):
"""Test turning off the device."""
assert STATE_OFF == self.get_entity().state
common.turn_on(self.hass, FAN_ENTITY_ID) async def test_turn_off(hass):
self.hass.block_till_done() """Test turning off the device."""
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_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
def test_turn_off_without_entity_id(self): common.async_turn_off(hass, FAN_ENTITY_ID)
"""Test turning off all fans.""" 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)
self.hass.block_till_done()
assert STATE_OFF != self.get_entity().state
common.turn_off(self.hass) async def test_turn_off_without_entity_id(hass):
self.hass.block_till_done() """Test turning off all fans."""
assert STATE_OFF == self.get_entity().state assert STATE_OFF == get_entity(hass).state
def test_set_direction(self): common.async_turn_on(hass, FAN_ENTITY_ID)
"""Test setting the direction of the device.""" await hass.async_block_till_done()
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_turn_off(hass)
self.hass.block_till_done() await hass.async_block_till_done()
assert fan.DIRECTION_REVERSE == \ assert STATE_OFF == get_entity(hass).state
self.get_entity().attributes.get('direction')
def test_set_speed(self):
"""Test setting the speed of the device."""
assert STATE_OFF == self.get_entity().state
common.set_speed(self.hass, FAN_ENTITY_ID, fan.SPEED_LOW) async def test_set_direction(hass):
self.hass.block_till_done() """Test setting the direction of the device."""
assert fan.SPEED_LOW == \ assert STATE_OFF == get_entity(hass).state
self.get_entity().attributes.get('speed')
def test_oscillate(self): common.async_set_direction(hass, FAN_ENTITY_ID, fan.DIRECTION_REVERSE)
"""Test oscillating the fan.""" await hass.async_block_till_done()
assert not self.get_entity().attributes.get('oscillating') assert fan.DIRECTION_REVERSE == \
get_entity(hass).attributes.get('direction')
common.oscillate(self.hass, FAN_ENTITY_ID, True)
self.hass.block_till_done()
assert self.get_entity().attributes.get('oscillating')
common.oscillate(self.hass, FAN_ENTITY_ID, False) async def test_set_speed(hass):
self.hass.block_till_done() """Test setting the speed of the device."""
assert not self.get_entity().attributes.get('oscillating') assert STATE_OFF == get_entity(hass).state
def test_is_on(self): common.async_set_speed(hass, FAN_ENTITY_ID, fan.SPEED_LOW)
"""Test is on service call.""" await hass.async_block_till_done()
assert not fan.is_on(self.hass, FAN_ENTITY_ID) assert fan.SPEED_LOW == \
get_entity(hass).attributes.get('speed')
common.turn_on(self.hass, FAN_ENTITY_ID)
self.hass.block_till_done() async def test_oscillate(hass):
assert fan.is_on(self.hass, FAN_ENTITY_ID) """Test oscillating the fan."""
assert not get_entity(hass).attributes.get('oscillating')
common.async_oscillate(hass, FAN_ENTITY_ID, True)
await hass.async_block_till_done()
assert get_entity(hass).attributes.get('oscillating')
common.async_oscillate(hass, FAN_ENTITY_ID, False)
await hass.async_block_till_done()
assert not get_entity(hass).attributes.get('oscillating')
async def test_is_on(hass):
"""Test is on service call."""
assert not fan.is_on(hass, FAN_ENTITY_ID)
common.async_turn_on(hass, FAN_ENTITY_ID)
await hass.async_block_till_done()
assert fan.is_on(hass, FAN_ENTITY_ID)

File diff suppressed because it is too large Load Diff