2017-04-16 09:06:57 +00:00
|
|
|
"""The tests for mqtt camera component."""
|
2019-01-29 00:21:38 +00:00
|
|
|
from unittest.mock import ANY
|
2017-04-18 15:55:51 +00:00
|
|
|
|
2019-01-29 00:21:38 +00:00
|
|
|
from homeassistant.components import camera, mqtt
|
|
|
|
from homeassistant.components.mqtt.discovery import async_start
|
2017-04-16 09:06:57 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
|
2019-01-29 00:21:38 +00:00
|
|
|
from tests.common import (
|
2019-07-31 19:25:30 +00:00
|
|
|
MockConfigEntry,
|
|
|
|
async_fire_mqtt_message,
|
|
|
|
async_mock_mqtt_component,
|
|
|
|
mock_registry,
|
|
|
|
)
|
2017-06-04 01:51:29 +00:00
|
|
|
|
|
|
|
|
2019-04-19 21:08:02 +00:00
|
|
|
async def test_run_camera_setup(hass, aiohttp_client):
|
2017-06-04 01:51:29 +00:00
|
|
|
"""Test that it fetches the given payload."""
|
2019-07-31 19:25:30 +00:00
|
|
|
topic = "test/camera"
|
2019-04-19 21:08:02 +00:00
|
|
|
await async_mock_mqtt_component(hass)
|
2019-07-31 19:25:30 +00:00
|
|
|
await async_setup_component(
|
|
|
|
hass,
|
|
|
|
"camera",
|
|
|
|
{"camera": {"platform": "mqtt", "topic": topic, "name": "Test Camera"}},
|
|
|
|
)
|
2017-06-04 01:51:29 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
url = hass.states.get("camera.test_camera").attributes["entity_picture"]
|
2017-06-04 01:51:29 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, topic, "beer")
|
2017-06-04 01:51:29 +00:00
|
|
|
|
2019-04-19 21:08:02 +00:00
|
|
|
client = await aiohttp_client(hass.http.app)
|
|
|
|
resp = await client.get(url)
|
2017-06-04 01:51:29 +00:00
|
|
|
assert resp.status == 200
|
2019-04-19 21:08:02 +00:00
|
|
|
body = await resp.text()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert body == "beer"
|
2018-09-21 11:09:54 +00:00
|
|
|
|
|
|
|
|
2019-04-19 21:08:02 +00:00
|
|
|
async def test_unique_id(hass):
|
2018-09-21 11:09:54 +00:00
|
|
|
"""Test unique id option only creates one camera per unique_id."""
|
2019-04-19 21:08:02 +00:00
|
|
|
await async_mock_mqtt_component(hass)
|
2019-07-31 19:25:30 +00:00
|
|
|
await async_setup_component(
|
|
|
|
hass,
|
|
|
|
"camera",
|
|
|
|
{
|
|
|
|
"camera": [
|
|
|
|
{
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "Test Camera 1",
|
|
|
|
"topic": "test-topic",
|
|
|
|
"unique_id": "TOTALLY_UNIQUE",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "Test Camera 2",
|
|
|
|
"topic": "test-topic",
|
|
|
|
"unique_id": "TOTALLY_UNIQUE",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
async_fire_mqtt_message(hass, "test-topic", "payload")
|
2018-09-21 11:09:54 +00:00
|
|
|
assert len(hass.states.async_all()) == 1
|
2019-01-29 00:21:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discovery_removal_camera(hass, mqtt_mock, caplog):
|
|
|
|
"""Test removal of discovered camera."""
|
|
|
|
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
2019-07-31 19:25:30 +00:00
|
|
|
await async_start(hass, "homeassistant", {}, entry)
|
2019-01-29 00:21:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
data = '{ "name": "Beer",' ' "topic": "test_topic"}'
|
2019-01-29 00:21:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "homeassistant/camera/bla/config", data)
|
2019-01-29 00:21:38 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("camera.beer")
|
2019-01-29 00:21:38 +00:00
|
|
|
assert state is not None
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.name == "Beer"
|
2019-01-29 00:21:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "homeassistant/camera/bla/config", "")
|
2019-01-29 00:21:38 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("camera.beer")
|
2019-01-29 00:21:38 +00:00
|
|
|
assert state is None
|
|
|
|
|
|
|
|
|
|
|
|
async def test_discovery_update_camera(hass, mqtt_mock, caplog):
|
|
|
|
"""Test update of discovered camera."""
|
|
|
|
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
2019-07-31 19:25:30 +00:00
|
|
|
await async_start(hass, "homeassistant", {}, entry)
|
2019-01-29 00:21:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
data1 = '{ "name": "Beer",' ' "topic": "test_topic"}'
|
|
|
|
data2 = '{ "name": "Milk",' ' "topic": "test_topic"}'
|
2019-01-29 00:21:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "homeassistant/camera/bla/config", data1)
|
2019-01-29 00:21:38 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("camera.beer")
|
2019-01-29 00:21:38 +00:00
|
|
|
assert state is not None
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.name == "Beer"
|
2019-01-29 00:21:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "homeassistant/camera/bla/config", data2)
|
2019-01-29 00:21:38 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("camera.beer")
|
2019-01-29 00:21:38 +00:00
|
|
|
assert state is not None
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.name == "Milk"
|
|
|
|
state = hass.states.get("camera.milk")
|
2019-01-29 00:21:38 +00:00
|
|
|
assert state is None
|
|
|
|
|
|
|
|
|
|
|
|
async def test_discovery_broken(hass, mqtt_mock, caplog):
|
|
|
|
"""Test handling of bad discovery message."""
|
|
|
|
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
2019-07-31 19:25:30 +00:00
|
|
|
await async_start(hass, "homeassistant", {}, entry)
|
2019-01-29 00:21:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
data1 = '{ "name": "Beer" }'
|
|
|
|
data2 = '{ "name": "Milk",' ' "topic": "test_topic"}'
|
2019-01-29 00:21:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "homeassistant/camera/bla/config", data1)
|
2019-01-29 00:21:38 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("camera.beer")
|
2019-01-29 00:21:38 +00:00
|
|
|
assert state is None
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "homeassistant/camera/bla/config", data2)
|
2019-01-29 00:21:38 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("camera.milk")
|
2019-01-29 00:21:38 +00:00
|
|
|
assert state is not None
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.name == "Milk"
|
|
|
|
state = hass.states.get("camera.beer")
|
2019-01-29 00:21:38 +00:00
|
|
|
assert state is None
|
|
|
|
|
|
|
|
|
|
|
|
async def test_entity_id_update(hass, mqtt_mock):
|
|
|
|
"""Test MQTT subscriptions are managed when entity_id is updated."""
|
|
|
|
registry = mock_registry(hass, {})
|
|
|
|
mock_mqtt = await async_mock_mqtt_component(hass)
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
camera.DOMAIN,
|
|
|
|
{
|
|
|
|
camera.DOMAIN: [
|
|
|
|
{
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "beer",
|
|
|
|
"topic": "test-topic",
|
|
|
|
"unique_id": "TOTALLY_UNIQUE",
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
state = hass.states.get("camera.beer")
|
2019-01-29 00:21:38 +00:00
|
|
|
assert state is not None
|
|
|
|
assert mock_mqtt.async_subscribe.call_count == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_mqtt.async_subscribe.assert_any_call("test-topic", ANY, 0, None)
|
2019-01-29 00:21:38 +00:00
|
|
|
mock_mqtt.async_subscribe.reset_mock()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
registry.async_update_entity("camera.beer", new_entity_id="camera.milk")
|
2019-01-29 00:21:38 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("camera.beer")
|
2019-01-29 00:21:38 +00:00
|
|
|
assert state is None
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("camera.milk")
|
2019-01-29 00:21:38 +00:00
|
|
|
assert state is not None
|
|
|
|
assert mock_mqtt.async_subscribe.call_count == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_mqtt.async_subscribe.assert_any_call("test-topic", ANY, 0, None)
|