Update fan/demo tests to async (#18109)

* Update fan/demo tests to async

* Use async_create_task
pull/18135/head
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 (
ATTR_ENTITY_ID, SERVICE_TURN_ON, SERVICE_TURN_OFF)
from homeassistant.loader import bind_hass
from homeassistant.core import callback
@callback
@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."""
data = {
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
}
hass.services.call(DOMAIN, SERVICE_TURN_ON, data)
hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_TURN_ON, data))
@callback
@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."""
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
def oscillate(hass, entity_id: str = None,
should_oscillate: bool = True) -> None:
def async_oscillate(hass, entity_id: str = None,
should_oscillate: bool = True) -> None:
"""Set oscillation on all or specified fan."""
data = {
key: value for key, value in [
@ -43,11 +49,13 @@ def oscillate(hass, entity_id: str = 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
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."""
data = {
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
}
hass.services.call(DOMAIN, SERVICE_SET_SPEED, data)
hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_SET_SPEED, data))
@callback
@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."""
data = {
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
}
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."""
import pytest
import unittest
from homeassistant.setup import setup_component
from homeassistant.setup import async_setup_component
from homeassistant.components import fan
from homeassistant.const import STATE_OFF, STATE_ON
from tests.common import get_test_home_assistant
from tests.components.fan import common
FAN_ENTITY_ID = 'fan.living_room_fan'
class TestDemoFan(unittest.TestCase):
"""Test the fan demo platform."""
def get_entity(hass):
"""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):
"""Initialize unit test data."""
self.hass = get_test_home_assistant()
assert setup_component(self.hass, fan.DOMAIN, {'fan': {
@pytest.fixture(autouse=True)
def setup_comp(hass):
"""Initialize components."""
hass.loop.run_until_complete(async_setup_component(hass, fan.DOMAIN, {
'fan': {
'platform': 'demo',
}})
self.hass.block_till_done()
}
}))
def tearDown(self):
"""Tear down unit test data."""
self.hass.stop()
def test_turn_on(self):
"""Test turning on the device."""
assert STATE_OFF == self.get_entity().state
async def test_turn_on(hass):
"""Test turning on the device."""
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.async_turn_on(hass, FAN_ENTITY_ID)
await hass.async_block_till_done()
assert STATE_OFF != get_entity(hass).state
common.turn_on(self.hass, FAN_ENTITY_ID, fan.SPEED_HIGH)
self.hass.block_till_done()
assert STATE_ON == self.get_entity().state
assert fan.SPEED_HIGH == \
self.get_entity().attributes[fan.ATTR_SPEED]
common.async_turn_on(hass, FAN_ENTITY_ID, fan.SPEED_HIGH)
await hass.async_block_till_done()
assert STATE_ON == get_entity(hass).state
assert fan.SPEED_HIGH == \
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)
self.hass.block_till_done()
assert STATE_OFF != self.get_entity().state
async def test_turn_off(hass):
"""Test turning off the device."""
assert STATE_OFF == get_entity(hass).state
common.turn_off(self.hass, FAN_ENTITY_ID)
self.hass.block_till_done()
assert STATE_OFF == self.get_entity().state
common.async_turn_on(hass, FAN_ENTITY_ID)
await hass.async_block_till_done()
assert STATE_OFF != get_entity(hass).state
def test_turn_off_without_entity_id(self):
"""Test turning off all fans."""
assert STATE_OFF == self.get_entity().state
common.async_turn_off(hass, FAN_ENTITY_ID)
await hass.async_block_till_done()
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)
self.hass.block_till_done()
assert STATE_OFF == self.get_entity().state
async def test_turn_off_without_entity_id(hass):
"""Test turning off all fans."""
assert STATE_OFF == get_entity(hass).state
def test_set_direction(self):
"""Test setting the direction of the device."""
assert STATE_OFF == self.get_entity().state
common.async_turn_on(hass, FAN_ENTITY_ID)
await hass.async_block_till_done()
assert STATE_OFF != get_entity(hass).state
common.set_direction(self.hass, FAN_ENTITY_ID, fan.DIRECTION_REVERSE)
self.hass.block_till_done()
assert fan.DIRECTION_REVERSE == \
self.get_entity().attributes.get('direction')
common.async_turn_off(hass)
await hass.async_block_till_done()
assert STATE_OFF == get_entity(hass).state
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)
self.hass.block_till_done()
assert fan.SPEED_LOW == \
self.get_entity().attributes.get('speed')
async def test_set_direction(hass):
"""Test setting the direction of the device."""
assert STATE_OFF == get_entity(hass).state
def test_oscillate(self):
"""Test oscillating the fan."""
assert not self.get_entity().attributes.get('oscillating')
common.async_set_direction(hass, FAN_ENTITY_ID, fan.DIRECTION_REVERSE)
await hass.async_block_till_done()
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)
self.hass.block_till_done()
assert not self.get_entity().attributes.get('oscillating')
async def test_set_speed(hass):
"""Test setting the speed of the device."""
assert STATE_OFF == get_entity(hass).state
def test_is_on(self):
"""Test is on service call."""
assert not fan.is_on(self.hass, FAN_ENTITY_ID)
common.async_set_speed(hass, FAN_ENTITY_ID, fan.SPEED_LOW)
await hass.async_block_till_done()
assert fan.SPEED_LOW == \
get_entity(hass).attributes.get('speed')
common.turn_on(self.hass, FAN_ENTITY_ID)
self.hass.block_till_done()
assert fan.is_on(self.hass, FAN_ENTITY_ID)
async def test_oscillate(hass):
"""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