Rewrite the pilight/test_sensor.py tests to use async pytest functions (#42418)

This commit is contained in:
Elena Rogleva 2020-11-10 12:11:56 +02:00 committed by GitHub
parent 48e954e038
commit c029d29f11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,42 +1,34 @@
"""The tests for the Pilight sensor platform.""" """The tests for the Pilight sensor platform."""
import logging import logging
import pytest
from homeassistant.components import pilight from homeassistant.components import pilight
import homeassistant.components.sensor as sensor import homeassistant.components.sensor as sensor
from homeassistant.setup import setup_component from homeassistant.setup import async_setup_component
from tests.common import assert_setup_component, get_test_home_assistant, mock_component from tests.common import assert_setup_component, mock_component
HASS = None
def fire_pilight_message(protocol, data): @pytest.fixture(autouse=True)
def setup_comp(hass):
"""Initialize components."""
mock_component(hass, "pilight")
def fire_pilight_message(hass, protocol, data):
"""Fire the fake Pilight message.""" """Fire the fake Pilight message."""
message = {pilight.CONF_PROTOCOL: protocol} message = {pilight.CONF_PROTOCOL: protocol}
message.update(data) message.update(data)
HASS.bus.fire(pilight.EVENT, message)
hass.bus.async_fire(pilight.EVENT, message)
# pylint: disable=invalid-name async def test_sensor_value_from_code(hass):
def setup_function():
"""Initialize a Home Assistant server."""
global HASS
HASS = get_test_home_assistant()
mock_component(HASS, "pilight")
# pylint: disable=invalid-name
def teardown_function():
"""Stop the Home Assistant server."""
HASS.stop()
def test_sensor_value_from_code():
"""Test the setting of value via pilight.""" """Test the setting of value via pilight."""
with assert_setup_component(1): with assert_setup_component(1):
setup_component( assert await async_setup_component(
HASS, hass,
sensor.DOMAIN, sensor.DOMAIN,
{ {
sensor.DOMAIN: { sensor.DOMAIN: {
@ -48,26 +40,26 @@ def test_sensor_value_from_code():
} }
}, },
) )
HASS.block_till_done() await hass.async_block_till_done()
state = HASS.states.get("sensor.test") state = hass.states.get("sensor.test")
assert state.state == "unknown" assert state.state == "unknown"
unit_of_measurement = state.attributes.get("unit_of_measurement") unit_of_measurement = state.attributes.get("unit_of_measurement")
assert unit_of_measurement == "fav unit" assert unit_of_measurement == "fav unit"
# Set value from data with correct payload # Set value from data with correct payload
fire_pilight_message(protocol="test-protocol", data={"test": 42}) fire_pilight_message(hass, protocol="test-protocol", data={"test": 42})
HASS.block_till_done() await hass.async_block_till_done()
state = HASS.states.get("sensor.test") state = hass.states.get("sensor.test")
assert state.state == "42" assert state.state == "42"
def test_disregard_wrong_payload(): async def test_disregard_wrong_payload(hass):
"""Test omitting setting of value with wrong payload.""" """Test omitting setting of value with wrong payload."""
with assert_setup_component(1): with assert_setup_component(1):
setup_component( assert await async_setup_component(
HASS, hass,
sensor.DOMAIN, sensor.DOMAIN,
{ {
sensor.DOMAIN: { sensor.DOMAIN: {
@ -78,40 +70,41 @@ def test_disregard_wrong_payload():
} }
}, },
) )
HASS.block_till_done() await hass.async_block_till_done()
# Try set value from data with incorrect payload # Try set value from data with incorrect payload
fire_pilight_message( fire_pilight_message(
protocol="test-protocol_2", data={"test": "data", "uuid": "0-0-0-0"} hass, protocol="test-protocol_2", data={"test": "data", "uuid": "0-0-0-0"}
) )
HASS.block_till_done() await hass.async_block_till_done()
state = HASS.states.get("sensor.test_2") state = hass.states.get("sensor.test_2")
assert state.state == "unknown" assert state.state == "unknown"
# Try set value from data with partially matched payload # Try set value from data with partially matched payload
fire_pilight_message( fire_pilight_message(
protocol="wrong-protocol", data={"test": "data", "uuid": "1-2-3-4"} hass, protocol="wrong-protocol", data={"test": "data", "uuid": "1-2-3-4"}
) )
HASS.block_till_done() await hass.async_block_till_done()
state = HASS.states.get("sensor.test_2") state = hass.states.get("sensor.test_2")
assert state.state == "unknown" assert state.state == "unknown"
# Try set value from data with fully matched payload # Try set value from data with fully matched payload
fire_pilight_message( fire_pilight_message(
hass,
protocol="test-protocol_2", protocol="test-protocol_2",
data={"test": "data", "uuid": "1-2-3-4", "other_payload": 3.141}, data={"test": "data", "uuid": "1-2-3-4", "other_payload": 3.141},
) )
HASS.block_till_done() await hass.async_block_till_done()
state = HASS.states.get("sensor.test_2") state = hass.states.get("sensor.test_2")
assert state.state == "data" assert state.state == "data"
def test_variable_missing(caplog): async def test_variable_missing(hass, caplog):
"""Check if error message when variable missing.""" """Check if error message when variable missing."""
caplog.set_level(logging.ERROR) caplog.set_level(logging.ERROR)
with assert_setup_component(1): with assert_setup_component(1):
setup_component( assert await async_setup_component(
HASS, hass,
sensor.DOMAIN, sensor.DOMAIN,
{ {
sensor.DOMAIN: { sensor.DOMAIN: {
@ -122,13 +115,15 @@ def test_variable_missing(caplog):
} }
}, },
) )
HASS.block_till_done() await hass.async_block_till_done()
# Create code without sensor variable # Create code without sensor variable
fire_pilight_message( fire_pilight_message(
protocol="test-protocol", data={"uuid": "1-2-3-4", "other_variable": 3.141} hass,
protocol="test-protocol",
data={"uuid": "1-2-3-4", "other_variable": 3.141},
) )
HASS.block_till_done() await hass.async_block_till_done()
logs = caplog.text logs = caplog.text