1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-25 05:26:47 +00:00

Rewrite demo unittest tests to pytest style test functions (#41845)

This commit is contained in:
CurrentThread
2020-10-19 23:29:44 +02:00
committed by GitHub
parent 7e3aa6ddce
commit e129ea4db7
3 changed files with 258 additions and 261 deletions

View File

@@ -1,57 +1,63 @@
"""The tests for the demo remote component."""
# pylint: disable=protected-access
import unittest
import pytest
import homeassistant.components.remote as remote
from homeassistant.const import STATE_OFF, STATE_ON
from homeassistant.setup import setup_component
from tests.common import get_test_home_assistant
from tests.components.remote import common
from homeassistant.components.remote import ATTR_COMMAND
from homeassistant.const import (
ATTR_ENTITY_ID,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
STATE_OFF,
STATE_ON,
)
from homeassistant.setup import async_setup_component
ENTITY_ID = "remote.remote_one"
SERVICE_SEND_COMMAND = "send_command"
class TestDemoRemote(unittest.TestCase):
"""Test the demo remote."""
@pytest.fixture(autouse=True)
async def setup_component(hass):
"""Initialize components."""
assert await async_setup_component(
hass, remote.DOMAIN, {"remote": {"platform": "demo"}}
)
await hass.async_block_till_done()
# pylint: disable=invalid-name
def setUp(self):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
assert setup_component(
self.hass, remote.DOMAIN, {"remote": {"platform": "demo"}}
)
self.hass.block_till_done()
self.addCleanup(self.tear_down_cleanup)
async def test_methods(hass):
"""Test if services call the entity methods as expected."""
await hass.services.async_call(
remote.DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: ENTITY_ID}
)
await hass.async_block_till_done()
state = hass.states.get(ENTITY_ID)
assert state.state == STATE_ON
def tear_down_cleanup(self):
"""Stop down everything that was started."""
self.hass.stop()
await hass.services.async_call(
remote.DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: ENTITY_ID}
)
await hass.async_block_till_done()
state = hass.states.get(ENTITY_ID)
assert state.state == STATE_OFF
def test_methods(self):
"""Test if services call the entity methods as expected."""
common.turn_on(self.hass, entity_id=ENTITY_ID)
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_ID)
assert state.state == STATE_ON
await hass.services.async_call(
remote.DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: ENTITY_ID}
)
await hass.async_block_till_done()
state = hass.states.get(ENTITY_ID)
assert state.state == STATE_ON
common.turn_off(self.hass, entity_id=ENTITY_ID)
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_ID)
assert state.state == STATE_OFF
data = {
ATTR_ENTITY_ID: ENTITY_ID,
ATTR_COMMAND: ["test"],
}
common.turn_on(self.hass, entity_id=ENTITY_ID)
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_ID)
assert state.state == STATE_ON
common.send_command(self.hass, "test", entity_id=ENTITY_ID)
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_ID)
assert state.attributes == {
"friendly_name": "Remote One",
"last_command_sent": "test",
"supported_features": 0,
}
await hass.services.async_call(remote.DOMAIN, SERVICE_SEND_COMMAND, data)
await hass.async_block_till_done()
state = hass.states.get(ENTITY_ID)
assert state.attributes == {
"friendly_name": "Remote One",
"last_command_sent": "test",
"supported_features": 0,
}