mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 23:27:37 +00:00
Prepare MQTT platform tests part2 (#90105)
* Tests button * Tests camera * Tests climate
This commit is contained in:
parent
87475e8ff6
commit
8fd88d6703
@ -13,7 +13,6 @@ from homeassistant.const import (
|
|||||||
Platform,
|
Platform,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.setup import async_setup_component
|
|
||||||
|
|
||||||
from .test_common import (
|
from .test_common import (
|
||||||
help_test_availability_when_connection_lost,
|
help_test_availability_when_connection_lost,
|
||||||
@ -57,13 +56,9 @@ def button_platform_only():
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.freeze_time("2021-11-08 13:31:44+00:00")
|
@pytest.mark.freeze_time("2021-11-08 13:31:44+00:00")
|
||||||
async def test_sending_mqtt_commands(
|
@pytest.mark.parametrize(
|
||||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
|
"hass_config",
|
||||||
) -> None:
|
[
|
||||||
"""Test the sending MQTT commands."""
|
|
||||||
assert await async_setup_component(
|
|
||||||
hass,
|
|
||||||
mqtt.DOMAIN,
|
|
||||||
{
|
{
|
||||||
mqtt.DOMAIN: {
|
mqtt.DOMAIN: {
|
||||||
button.DOMAIN: {
|
button.DOMAIN: {
|
||||||
@ -74,10 +69,14 @@ async def test_sending_mqtt_commands(
|
|||||||
"qos": "2",
|
"qos": "2",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
)
|
],
|
||||||
await hass.async_block_till_done()
|
)
|
||||||
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
|
async def test_sending_mqtt_commands(
|
||||||
|
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
|
||||||
|
) -> None:
|
||||||
|
"""Test the sending MQTT commands."""
|
||||||
|
mqtt_mock = await mqtt_mock_entry_no_yaml_config()
|
||||||
|
|
||||||
state = hass.states.get("button.test_button")
|
state = hass.states.get("button.test_button")
|
||||||
assert state.state == STATE_UNKNOWN
|
assert state.state == STATE_UNKNOWN
|
||||||
@ -98,13 +97,9 @@ async def test_sending_mqtt_commands(
|
|||||||
assert state.state == "2021-11-08T13:31:44+00:00"
|
assert state.state == "2021-11-08T13:31:44+00:00"
|
||||||
|
|
||||||
|
|
||||||
async def test_command_template(
|
@pytest.mark.parametrize(
|
||||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
|
"hass_config",
|
||||||
) -> None:
|
[
|
||||||
"""Test the sending of MQTT commands through a command template."""
|
|
||||||
assert await async_setup_component(
|
|
||||||
hass,
|
|
||||||
mqtt.DOMAIN,
|
|
||||||
{
|
{
|
||||||
mqtt.DOMAIN: {
|
mqtt.DOMAIN: {
|
||||||
button.DOMAIN: {
|
button.DOMAIN: {
|
||||||
@ -114,10 +109,14 @@ async def test_command_template(
|
|||||||
"payload_press": "milky_way_press",
|
"payload_press": "milky_way_press",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
)
|
],
|
||||||
await hass.async_block_till_done()
|
)
|
||||||
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
|
async def test_command_template(
|
||||||
|
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
|
||||||
|
) -> None:
|
||||||
|
"""Test the sending of MQTT commands through a command template."""
|
||||||
|
mqtt_mock = await mqtt_mock_entry_no_yaml_config()
|
||||||
|
|
||||||
state = hass.states.get("button.test")
|
state = hass.states.get("button.test")
|
||||||
assert state.state == STATE_UNKNOWN
|
assert state.state == STATE_UNKNOWN
|
||||||
@ -436,11 +435,9 @@ async def test_entity_debug_info_message(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def test_invalid_device_class(hass: HomeAssistant) -> None:
|
@pytest.mark.parametrize(
|
||||||
"""Test device_class option with invalid value."""
|
"hass_config",
|
||||||
assert not await async_setup_component(
|
[
|
||||||
hass,
|
|
||||||
mqtt.DOMAIN,
|
|
||||||
{
|
{
|
||||||
mqtt.DOMAIN: {
|
mqtt.DOMAIN: {
|
||||||
button.DOMAIN: {
|
button.DOMAIN: {
|
||||||
@ -449,17 +446,20 @@ async def test_invalid_device_class(hass: HomeAssistant) -> None:
|
|||||||
"device_class": "foobarnotreal",
|
"device_class": "foobarnotreal",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
)
|
],
|
||||||
|
)
|
||||||
|
async def test_invalid_device_class(
|
||||||
async def test_valid_device_class(
|
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
|
||||||
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test device_class option with valid values."""
|
"""Test device_class option with invalid value."""
|
||||||
assert await async_setup_component(
|
with pytest.raises(AssertionError):
|
||||||
hass,
|
await mqtt_mock_entry_no_yaml_config()
|
||||||
mqtt.DOMAIN,
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"hass_config",
|
||||||
|
[
|
||||||
{
|
{
|
||||||
mqtt.DOMAIN: {
|
mqtt.DOMAIN: {
|
||||||
button.DOMAIN: [
|
button.DOMAIN: [
|
||||||
@ -479,10 +479,14 @@ async def test_valid_device_class(
|
|||||||
},
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
)
|
],
|
||||||
await hass.async_block_till_done()
|
)
|
||||||
await mqtt_mock_entry_with_yaml_config()
|
async def test_valid_device_class(
|
||||||
|
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
|
||||||
|
) -> None:
|
||||||
|
"""Test device_class option with valid values."""
|
||||||
|
await mqtt_mock_entry_no_yaml_config()
|
||||||
|
|
||||||
state = hass.states.get("button.test_1")
|
state = hass.states.get("button.test_1")
|
||||||
assert state.attributes["device_class"] == button.ButtonDeviceClass.UPDATE
|
assert state.attributes["device_class"] == button.ButtonDeviceClass.UPDATE
|
||||||
|
@ -10,7 +10,6 @@ from homeassistant.components import camera, mqtt
|
|||||||
from homeassistant.components.mqtt.camera import MQTT_CAMERA_ATTRIBUTES_BLOCKED
|
from homeassistant.components.mqtt.camera import MQTT_CAMERA_ATTRIBUTES_BLOCKED
|
||||||
from homeassistant.const import Platform
|
from homeassistant.const import Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.setup import async_setup_component
|
|
||||||
|
|
||||||
from .test_common import (
|
from .test_common import (
|
||||||
help_test_availability_when_connection_lost,
|
help_test_availability_when_connection_lost,
|
||||||
@ -56,20 +55,18 @@ def camera_platform_only():
|
|||||||
yield
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"hass_config",
|
||||||
|
[{mqtt.DOMAIN: {camera.DOMAIN: {"topic": "test/camera", "name": "Test Camera"}}}],
|
||||||
|
)
|
||||||
async def test_run_camera_setup(
|
async def test_run_camera_setup(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
hass_client_no_auth: ClientSessionGenerator,
|
hass_client_no_auth: ClientSessionGenerator,
|
||||||
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
|
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test that it fetches the given payload."""
|
"""Test that it fetches the given payload."""
|
||||||
topic = "test/camera"
|
topic = "test/camera"
|
||||||
await async_setup_component(
|
await mqtt_mock_entry_no_yaml_config()
|
||||||
hass,
|
|
||||||
mqtt.DOMAIN,
|
|
||||||
{mqtt.DOMAIN: {camera.DOMAIN: {"topic": topic, "name": "Test Camera"}}},
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
await mqtt_mock_entry_with_yaml_config()
|
|
||||||
|
|
||||||
url = hass.states.get("camera.test_camera").attributes["entity_picture"]
|
url = hass.states.get("camera.test_camera").attributes["entity_picture"]
|
||||||
|
|
||||||
@ -82,28 +79,28 @@ async def test_run_camera_setup(
|
|||||||
assert body == "beer"
|
assert body == "beer"
|
||||||
|
|
||||||
|
|
||||||
async def test_run_camera_b64_encoded(
|
@pytest.mark.parametrize(
|
||||||
hass: HomeAssistant,
|
"hass_config",
|
||||||
hass_client_no_auth: ClientSessionGenerator,
|
[
|
||||||
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
|
|
||||||
) -> None:
|
|
||||||
"""Test that it fetches the given encoded payload."""
|
|
||||||
topic = "test/camera"
|
|
||||||
await async_setup_component(
|
|
||||||
hass,
|
|
||||||
mqtt.DOMAIN,
|
|
||||||
{
|
{
|
||||||
mqtt.DOMAIN: {
|
mqtt.DOMAIN: {
|
||||||
camera.DOMAIN: {
|
camera.DOMAIN: {
|
||||||
"topic": topic,
|
"topic": "test/camera",
|
||||||
"name": "Test Camera",
|
"name": "Test Camera",
|
||||||
"image_encoding": "b64",
|
"image_encoding": "b64",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
)
|
],
|
||||||
await hass.async_block_till_done()
|
)
|
||||||
await mqtt_mock_entry_with_yaml_config()
|
async def test_run_camera_b64_encoded(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
hass_client_no_auth: ClientSessionGenerator,
|
||||||
|
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
|
||||||
|
) -> None:
|
||||||
|
"""Test that it fetches the given encoded payload."""
|
||||||
|
topic = "test/camera"
|
||||||
|
await mqtt_mock_entry_no_yaml_config()
|
||||||
|
|
||||||
url = hass.states.get("camera.test_camera").attributes["entity_picture"]
|
url = hass.states.get("camera.test_camera").attributes["entity_picture"]
|
||||||
|
|
||||||
@ -116,31 +113,31 @@ async def test_run_camera_b64_encoded(
|
|||||||
assert body == "grass"
|
assert body == "grass"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"hass_config",
|
||||||
|
[
|
||||||
|
{
|
||||||
|
mqtt.DOMAIN: {
|
||||||
|
"camera": {
|
||||||
|
"topic": "test/camera",
|
||||||
|
"name": "Test Camera",
|
||||||
|
"encoding": "utf-8",
|
||||||
|
"image_encoding": "b64",
|
||||||
|
"availability": {"topic": "test/camera_availability"},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
)
|
||||||
async def test_camera_b64_encoded_with_availability(
|
async def test_camera_b64_encoded_with_availability(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
hass_client_no_auth: ClientSessionGenerator,
|
hass_client_no_auth: ClientSessionGenerator,
|
||||||
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
|
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test availability works if b64 encoding is turned on."""
|
"""Test availability works if b64 encoding is turned on."""
|
||||||
topic = "test/camera"
|
topic = "test/camera"
|
||||||
topic_availability = "test/camera_availability"
|
topic_availability = "test/camera_availability"
|
||||||
await async_setup_component(
|
await mqtt_mock_entry_no_yaml_config()
|
||||||
hass,
|
|
||||||
mqtt.DOMAIN,
|
|
||||||
{
|
|
||||||
mqtt.DOMAIN: {
|
|
||||||
"camera": {
|
|
||||||
"topic": topic,
|
|
||||||
"name": "Test Camera",
|
|
||||||
"encoding": "utf-8",
|
|
||||||
"image_encoding": "b64",
|
|
||||||
"availability": {"topic": topic_availability},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
await mqtt_mock_entry_with_yaml_config()
|
|
||||||
|
|
||||||
# Make sure we are available
|
# Make sure we are available
|
||||||
async_fire_mqtt_message(hass, topic_availability, "online")
|
async_fire_mqtt_message(hass, topic_availability, "online")
|
||||||
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user