Files
homeassistant-core/tests/components/push/test_camera.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

94 lines
2.7 KiB
Python
Raw Normal View History

2018-07-04 06:44:47 +01:00
"""The tests for generic camera component."""
2018-07-04 06:44:47 +01:00
from datetime import timedelta
from http import HTTPStatus
import io
2018-07-04 06:44:47 +01:00
from homeassistant.config import async_process_ha_core_config
from homeassistant.core import HomeAssistant
2018-07-04 06:44:47 +01:00
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
2020-06-29 11:39:24 -05:00
from tests.common import async_fire_time_changed
from tests.typing import ClientSessionGenerator
2020-06-29 11:39:24 -05:00
2018-07-04 06:44:47 +01:00
async def test_bad_posting(
hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator
) -> None:
2018-07-04 06:44:47 +01:00
"""Test that posting to wrong api endpoint fails."""
await async_process_ha_core_config(
2020-08-27 13:56:20 +02:00
hass,
{"external_url": "http://example.com"},
)
2018-07-04 06:44:47 +01:00
await async_setup_component(
hass,
"camera",
{
"camera": {
"platform": "push",
"name": "config_test",
"webhook_id": "camera.config_test",
2019-07-31 12:25:30 -07:00
}
2018-07-04 06:44:47 +01:00
},
)
await hass.async_block_till_done()
assert hass.states.get("camera.config_test") is not None
client = await hass_client_no_auth()
2018-07-04 06:44:47 +01:00
# missing file
2019-01-16 23:23:46 +01:00
async with client.post("/api/webhook/camera.config_test") as resp:
assert resp.status == HTTPStatus.OK # webhooks always return OK
camera_state = hass.states.get("camera.config_test")
assert camera_state.state == "idle" # no file supplied we are still idle
2018-07-04 06:44:47 +01:00
async def test_posting_url(
hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator
) -> None:
2018-07-04 06:44:47 +01:00
"""Test that posting to api endpoint works."""
await async_process_ha_core_config(
2020-08-27 13:56:20 +02:00
hass,
{"external_url": "http://example.com"},
)
2018-07-04 06:44:47 +01:00
await async_setup_component(
hass,
"camera",
{
"camera": {
"platform": "push",
"name": "config_test",
"webhook_id": "camera.config_test",
2019-07-31 12:25:30 -07:00
}
2018-07-04 06:44:47 +01:00
},
)
await hass.async_block_till_done()
2018-07-04 06:44:47 +01:00
client = await hass_client_no_auth()
2018-07-04 06:44:47 +01:00
files = {"image": io.BytesIO(b"fake")}
# initial state
camera_state = hass.states.get("camera.config_test")
assert camera_state.state == "idle"
# post image
resp = await client.post("/api/webhook/camera.config_test", data=files)
assert resp.status == HTTPStatus.OK
2018-07-04 06:44:47 +01:00
# state recording
camera_state = hass.states.get("camera.config_test")
assert camera_state.state == "recording"
# await timeout
shifted_time = dt_util.utcnow() + timedelta(seconds=15)
2020-06-29 11:39:24 -05:00
async_fire_time_changed(hass, shifted_time)
2018-07-04 06:44:47 +01:00
await hass.async_block_till_done()
# back to initial state
camera_state = hass.states.get("camera.config_test")
assert camera_state.state == "idle"