2016-10-18 21:16:20 +00:00
|
|
|
"""The tests for the Pilight sensor platform."""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.components import pilight
|
2019-12-09 11:10:38 +00:00
|
|
|
import homeassistant.components.sensor as sensor
|
|
|
|
from homeassistant.setup import setup_component
|
2016-10-18 21:16:20 +00:00
|
|
|
|
2019-12-09 11:10:38 +00:00
|
|
|
from tests.common import assert_setup_component, get_test_home_assistant, mock_component
|
2016-10-18 21:16:20 +00:00
|
|
|
|
|
|
|
HASS = None
|
|
|
|
|
|
|
|
|
|
|
|
def fire_pilight_message(protocol, data):
|
2016-10-31 12:18:47 +00:00
|
|
|
"""Fire the fake Pilight message."""
|
|
|
|
message = {pilight.CONF_PROTOCOL: protocol}
|
2016-10-18 21:16:20 +00:00
|
|
|
message.update(data)
|
|
|
|
HASS.bus.fire(pilight.EVENT, message)
|
|
|
|
|
|
|
|
|
2016-10-30 21:18:53 +00:00
|
|
|
# pylint: disable=invalid-name
|
|
|
|
def setup_function():
|
2016-10-18 21:16:20 +00:00
|
|
|
"""Initialize a Home Assistant server."""
|
|
|
|
global HASS
|
|
|
|
|
|
|
|
HASS = get_test_home_assistant()
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_component(HASS, "pilight")
|
2016-10-18 21:16:20 +00:00
|
|
|
|
|
|
|
|
2016-10-30 21:18:53 +00:00
|
|
|
# pylint: disable=invalid-name
|
|
|
|
def teardown_function():
|
2016-10-18 21:16:20 +00:00
|
|
|
"""Stop the Home Assistant server."""
|
|
|
|
HASS.stop()
|
|
|
|
|
|
|
|
|
|
|
|
def test_sensor_value_from_code():
|
|
|
|
"""Test the setting of value via pilight."""
|
|
|
|
with assert_setup_component(1):
|
2019-07-31 19:25:30 +00:00
|
|
|
setup_component(
|
|
|
|
HASS,
|
|
|
|
sensor.DOMAIN,
|
|
|
|
{
|
|
|
|
sensor.DOMAIN: {
|
|
|
|
"platform": "pilight",
|
|
|
|
"name": "test",
|
|
|
|
"variable": "test",
|
|
|
|
"payload": {"protocol": "test-protocol"},
|
|
|
|
"unit_of_measurement": "fav unit",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2020-06-01 05:18:30 +00:00
|
|
|
HASS.block_till_done()
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
state = HASS.states.get("sensor.test")
|
|
|
|
assert state.state == "unknown"
|
|
|
|
|
|
|
|
unit_of_measurement = state.attributes.get("unit_of_measurement")
|
|
|
|
assert unit_of_measurement == "fav unit"
|
2016-10-18 21:16:20 +00:00
|
|
|
|
|
|
|
# Set value from data with correct payload
|
2019-07-31 19:25:30 +00:00
|
|
|
fire_pilight_message(protocol="test-protocol", data={"test": 42})
|
2016-10-18 21:16:20 +00:00
|
|
|
HASS.block_till_done()
|
2019-07-31 19:25:30 +00:00
|
|
|
state = HASS.states.get("sensor.test")
|
|
|
|
assert state.state == "42"
|
2016-10-18 21:16:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_disregard_wrong_payload():
|
|
|
|
"""Test omitting setting of value with wrong payload."""
|
|
|
|
with assert_setup_component(1):
|
2019-07-31 19:25:30 +00:00
|
|
|
setup_component(
|
|
|
|
HASS,
|
|
|
|
sensor.DOMAIN,
|
|
|
|
{
|
|
|
|
sensor.DOMAIN: {
|
|
|
|
"platform": "pilight",
|
|
|
|
"name": "test_2",
|
|
|
|
"variable": "test",
|
|
|
|
"payload": {"uuid": "1-2-3-4", "protocol": "test-protocol_2"},
|
2016-10-31 12:18:47 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
)
|
2020-06-01 05:18:30 +00:00
|
|
|
HASS.block_till_done()
|
2016-10-18 21:16:20 +00:00
|
|
|
|
|
|
|
# Try set value from data with incorrect payload
|
2019-07-31 19:25:30 +00:00
|
|
|
fire_pilight_message(
|
|
|
|
protocol="test-protocol_2", data={"test": "data", "uuid": "0-0-0-0"}
|
|
|
|
)
|
2016-10-18 21:16:20 +00:00
|
|
|
HASS.block_till_done()
|
2019-07-31 19:25:30 +00:00
|
|
|
state = HASS.states.get("sensor.test_2")
|
|
|
|
assert state.state == "unknown"
|
2016-10-18 21:16:20 +00:00
|
|
|
|
|
|
|
# Try set value from data with partially matched payload
|
2019-07-31 19:25:30 +00:00
|
|
|
fire_pilight_message(
|
|
|
|
protocol="wrong-protocol", data={"test": "data", "uuid": "1-2-3-4"}
|
|
|
|
)
|
2016-10-18 21:16:20 +00:00
|
|
|
HASS.block_till_done()
|
2019-07-31 19:25:30 +00:00
|
|
|
state = HASS.states.get("sensor.test_2")
|
|
|
|
assert state.state == "unknown"
|
2016-10-18 21:16:20 +00:00
|
|
|
|
|
|
|
# Try set value from data with fully matched payload
|
2019-07-31 19:25:30 +00:00
|
|
|
fire_pilight_message(
|
|
|
|
protocol="test-protocol_2",
|
|
|
|
data={"test": "data", "uuid": "1-2-3-4", "other_payload": 3.141},
|
|
|
|
)
|
2016-10-18 21:16:20 +00:00
|
|
|
HASS.block_till_done()
|
2019-07-31 19:25:30 +00:00
|
|
|
state = HASS.states.get("sensor.test_2")
|
|
|
|
assert state.state == "data"
|
2016-10-18 21:16:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_variable_missing(caplog):
|
|
|
|
"""Check if error message when variable missing."""
|
|
|
|
caplog.set_level(logging.ERROR)
|
|
|
|
with assert_setup_component(1):
|
2019-07-31 19:25:30 +00:00
|
|
|
setup_component(
|
|
|
|
HASS,
|
|
|
|
sensor.DOMAIN,
|
|
|
|
{
|
|
|
|
sensor.DOMAIN: {
|
|
|
|
"platform": "pilight",
|
|
|
|
"name": "test_3",
|
|
|
|
"variable": "test",
|
|
|
|
"payload": {"protocol": "test-protocol"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2020-06-01 05:18:30 +00:00
|
|
|
HASS.block_till_done()
|
2016-10-18 21:16:20 +00:00
|
|
|
|
|
|
|
# Create code without sensor variable
|
2019-07-31 19:25:30 +00:00
|
|
|
fire_pilight_message(
|
|
|
|
protocol="test-protocol", data={"uuid": "1-2-3-4", "other_variable": 3.141}
|
|
|
|
)
|
2016-10-18 21:16:20 +00:00
|
|
|
HASS.block_till_done()
|
|
|
|
|
|
|
|
logs = caplog.text
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "No variable test in received code" in logs
|