Add image integration (#38969)

This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2020-08-19 11:33:04 +02:00
1 parent 111c2006c8
commit 24a16ff8fe
20 files changed
+356 -41

No files matched your search

+1
View File
@@ -0,0 +1 @@
"""Tests for the Image integration."""
Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

+76
View File
@@ -0,0 +1,76 @@
"""Test that we can upload images."""
import pathlib
import tempfile
from aiohttp import ClientSession, ClientWebSocketResponse
from homeassistant.components.websocket_api import const as ws_const
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as util_dt
from tests.async_mock import patch
async def test_upload_image(hass, hass_client, hass_ws_client):
"""Test we can upload an image."""
now = util_dt.utcnow()
test_image = pathlib.Path(__file__).parent / "logo.png"
with tempfile.TemporaryDirectory() as tempdir, patch.object(
hass.config, "path", return_value=tempdir
), patch("homeassistant.util.dt.utcnow", return_value=now):
assert await async_setup_component(hass, "image", {})
ws_client: ClientWebSocketResponse = await hass_ws_client()
client: ClientSession = await hass_client()
with test_image.open("rb") as fp:
res = await client.post("/api/image/upload", data={"file": fp})
assert res.status == 200
item = await res.json()
assert item["content_type"] == "image/png"
assert item["filesize"] == 38847
assert item["name"] == "logo.png"
assert item["uploaded_at"] == now.isoformat()
tempdir = pathlib.Path(tempdir)
item_folder: pathlib.Path = tempdir / item["id"]
assert (item_folder / "original").read_bytes() == test_image.read_bytes()
# fetch non-existing image
res = await client.get("/api/image/serve/non-existing/256x256")
assert res.status == 404
# fetch invalid sizes
for inv_size in ("256", "256x25A", "100x100", "25Ax256"):
res = await client.get(f"/api/image/serve/{item['id']}/{inv_size}")
assert res.status == 400
# fetch resized version
res = await client.get(f"/api/image/serve/{item['id']}/256x256")
assert res.status == 200
assert (item_folder / "256x256").is_file()
# List item
await ws_client.send_json({"id": 6, "type": "image/list"})
msg = await ws_client.receive_json()
assert msg["id"] == 6
assert msg["type"] == ws_const.TYPE_RESULT
assert msg["success"]
assert msg["result"] == [item]
# Delete item
await ws_client.send_json(
{"id": 7, "type": "image/delete", "image_id": item["id"]}
)
msg = await ws_client.receive_json()
assert msg["id"] == 7
assert msg["type"] == ws_const.TYPE_RESULT
assert msg["success"]
# Ensure removed from disk
assert not item_folder.is_dir()
+17 -19
View File
@@ -11,6 +11,7 @@ from homeassistant.components.device_tracker import (
)
from homeassistant.components.person import ATTR_SOURCE, ATTR_USER_ID, DOMAIN
from homeassistant.const import (
ATTR_ENTITY_PICTURE,
ATTR_GPS_ACCURACY,
ATTR_ID,
ATTR_LATITUDE,
@@ -24,7 +25,7 @@ from homeassistant.helpers import collection, entity_registry
from homeassistant.setup import async_setup_component
from tests.async_mock import patch
from tests.common import assert_setup_component, mock_component, mock_restore_cache
from tests.common import mock_component, mock_restore_cache
DEVICE_TRACKER = "device_tracker.test_tracker"
DEVICE_TRACKER_2 = "device_tracker.test_tracker_2"
@@ -67,8 +68,7 @@ def storage_setup(hass, hass_storage, hass_admin_user):
async def test_minimal_setup(hass):
"""Test minimal config with only name."""
config = {DOMAIN: {"id": "1234", "name": "test person"}}
with assert_setup_component(1):
assert await async_setup_component(hass, DOMAIN, config)
assert await async_setup_component(hass, DOMAIN, config)
state = hass.states.get("person.test_person")
assert state.state == STATE_UNKNOWN
@@ -76,6 +76,7 @@ async def test_minimal_setup(hass):
assert state.attributes.get(ATTR_LONGITUDE) is None
assert state.attributes.get(ATTR_SOURCE) is None
assert state.attributes.get(ATTR_USER_ID) is None
assert state.attributes.get(ATTR_ENTITY_PICTURE) is None
async def test_setup_no_id(hass):
@@ -94,8 +95,7 @@ async def test_setup_user_id(hass, hass_admin_user):
"""Test config with user id."""
user_id = hass_admin_user.id
config = {DOMAIN: {"id": "1234", "name": "test person", "user_id": user_id}}
with assert_setup_component(1):
assert await async_setup_component(hass, DOMAIN, config)
assert await async_setup_component(hass, DOMAIN, config)
state = hass.states.get("person.test_person")
assert state.state == STATE_UNKNOWN
@@ -115,8 +115,7 @@ async def test_valid_invalid_user_ids(hass, hass_admin_user):
{"id": "5678", "name": "test bad user", "user_id": "bad_user_id"},
]
}
with assert_setup_component(2):
assert await async_setup_component(hass, DOMAIN, config)
assert await async_setup_component(hass, DOMAIN, config)
state = hass.states.get("person.test_valid_user")
assert state.state == STATE_UNKNOWN
@@ -141,8 +140,7 @@ async def test_setup_tracker(hass, hass_admin_user):
"device_trackers": DEVICE_TRACKER,
}
}
with assert_setup_component(1):
assert await async_setup_component(hass, DOMAIN, config)
assert await async_setup_component(hass, DOMAIN, config)
state = hass.states.get("person.tracked_person")
assert state.state == STATE_UNKNOWN
@@ -198,8 +196,7 @@ async def test_setup_two_trackers(hass, hass_admin_user):
"device_trackers": [DEVICE_TRACKER, DEVICE_TRACKER_2],
}
}
with assert_setup_component(1):
assert await async_setup_component(hass, DOMAIN, config)
assert await async_setup_component(hass, DOMAIN, config)
state = hass.states.get("person.tracked_person")
assert state.state == STATE_UNKNOWN
@@ -285,8 +282,7 @@ async def test_ignore_unavailable_states(hass, hass_admin_user):
"device_trackers": [DEVICE_TRACKER, DEVICE_TRACKER_2],
}
}
with assert_setup_component(1):
assert await async_setup_component(hass, DOMAIN, config)
assert await async_setup_component(hass, DOMAIN, config)
state = hass.states.get("person.tracked_person")
assert state.state == STATE_UNKNOWN
@@ -337,10 +333,10 @@ async def test_restore_home_state(hass, hass_admin_user):
"name": "tracked person",
"user_id": user_id,
"device_trackers": DEVICE_TRACKER,
"picture": "/bla",
}
}
with assert_setup_component(1):
assert await async_setup_component(hass, DOMAIN, config)
assert await async_setup_component(hass, DOMAIN, config)
state = hass.states.get("person.tracked_person")
assert state.state == "home"
@@ -350,6 +346,7 @@ async def test_restore_home_state(hass, hass_admin_user):
# When restoring state the entity_id of the person will be used as source.
assert state.attributes.get(ATTR_SOURCE) == "person.tracked_person"
assert state.attributes.get(ATTR_USER_ID) == user_id
assert state.attributes.get(ATTR_ENTITY_PICTURE) == "/bla"
async def test_duplicate_ids(hass, hass_admin_user):
@@ -360,8 +357,7 @@ async def test_duplicate_ids(hass, hass_admin_user):
{"id": "1234", "name": "test user 2"},
]
}
with assert_setup_component(2):
assert await async_setup_component(hass, DOMAIN, config)
assert await async_setup_component(hass, DOMAIN, config)
assert len(hass.states.async_entity_ids("person")) == 1
assert hass.states.get("person.test_user_1") is not None
@@ -371,8 +367,7 @@ async def test_duplicate_ids(hass, hass_admin_user):
async def test_create_person_during_run(hass):
"""Test that person is updated if created while hass is running."""
config = {DOMAIN: {}}
with assert_setup_component(0):
assert await async_setup_component(hass, DOMAIN, config)
assert await async_setup_component(hass, DOMAIN, config)
hass.states.async_set(DEVICE_TRACKER, "home")
await hass.async_block_till_done()
@@ -465,6 +460,7 @@ async def test_ws_create(hass, hass_ws_client, storage_setup, hass_read_only_use
"name": "Hello",
"device_trackers": [DEVICE_TRACKER],
"user_id": hass_read_only_user.id,
"picture": "/bla",
}
)
resp = await client.receive_json()
@@ -529,6 +525,7 @@ async def test_ws_update(hass, hass_ws_client, storage_setup):
"name": "Updated Name",
"device_trackers": [DEVICE_TRACKER_2],
"user_id": None,
"picture": "/bla",
}
)
resp = await client.receive_json()
@@ -542,6 +539,7 @@ async def test_ws_update(hass, hass_ws_client, storage_setup):
assert persons[0]["name"] == "Updated Name"
assert persons[0]["device_trackers"] == [DEVICE_TRACKER_2]
assert persons[0]["user_id"] is None
assert persons[0]["picture"] == "/bla"
state = hass.states.get("person.tracked_person")
assert state.name == "Updated Name"