init home-assistant

This commit is contained in:
2026-11-19 15:15:32 -05:00
parent 2e81d9d300
commit feec6ef9fb
19230 changed files with 3335526 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
"""Tests for the Image Upload integration."""
import pathlib
TEST_IMAGE = pathlib.Path(__file__).parent / "logo.png"

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

View File

@@ -0,0 +1,95 @@
"""Test that we can upload images."""
import pathlib
import tempfile
from unittest.mock import patch
from aiohttp import ClientSession, ClientWebSocketResponse
from freezegun.api import FrozenDateTimeFactory
from homeassistant.components.websocket_api import TYPE_RESULT
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
from . import TEST_IMAGE
from tests.typing import ClientSessionGenerator, WebSocketGenerator
async def test_upload_image(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
hass_client: ClientSessionGenerator,
hass_ws_client: WebSocketGenerator,
) -> None:
"""Test we can upload an image."""
now = dt_util.utcnow()
freezer.move_to(now)
with (
tempfile.TemporaryDirectory() as tempdir,
patch.object(hass.config, "path", return_value=tempdir),
):
assert await async_setup_component(hass, "image_upload", {})
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"]
test_image_bytes = TEST_IMAGE.read_bytes()
assert (item_folder / "original").read_bytes() == test_image_bytes
# fetch original image
res = await client.get(f"/api/image/serve/{item['id']}/original")
assert res.status == 200
fetched_image_bytes = await res.read()
assert fetched_image_bytes == test_image_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"] == 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"] == TYPE_RESULT
assert msg["success"]
# Ensure removed from disk
assert not item_folder.is_dir()

View File

@@ -0,0 +1,90 @@
"""Test image_upload media source."""
import tempfile
from unittest.mock import patch
from aiohttp import ClientSession
import pytest
from homeassistant.components import media_source
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from . import TEST_IMAGE
from tests.typing import ClientSessionGenerator
@pytest.fixture(autouse=True)
async def setup_media_source(hass: HomeAssistant) -> None:
"""Set up media source."""
assert await async_setup_component(hass, "media_source", {})
async def __upload_test_image(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
) -> str:
with (
tempfile.TemporaryDirectory() as tempdir,
patch.object(hass.config, "path", return_value=tempdir),
):
assert await async_setup_component(hass, "image_upload", {})
client: ClientSession = await hass_client()
file = await hass.async_add_executor_job(TEST_IMAGE.open, "rb")
res = await client.post("/api/image/upload", data={"file": file})
hass.async_add_executor_job(file.close)
assert res.status == 200
item = await res.json()
assert item["content_type"] == "image/png"
assert item["filesize"] == 38847
return item["id"]
async def test_browsing(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
) -> None:
"""Test browsing image media source."""
image_id = await __upload_test_image(hass, hass_client)
item = await media_source.async_browse_media(hass, "media-source://image_upload")
assert item is not None
assert item.title == "Image Upload"
assert len(item.children) == 1
assert item.children[0].media_content_type == "image/png"
assert item.children[0].identifier == image_id
assert item.children[0].thumbnail == f"/api/image/serve/{image_id}/256x256"
with pytest.raises(
media_source.BrowseError,
match="Unknown item",
):
await media_source.async_browse_media(
hass, "media-source://image_upload/invalid_path"
)
async def test_resolving(
hass: HomeAssistant, hass_client: ClientSessionGenerator
) -> None:
"""Test resolving."""
image_id = await __upload_test_image(hass, hass_client)
item = await media_source.async_resolve_media(
hass, f"media-source://image_upload/{image_id}", None
)
assert item is not None
assert item.url == f"/api/image/serve/{image_id}/original"
assert item.mime_type == "image/png"
invalid_id = "aabbccddeeff"
with pytest.raises(
media_source.Unresolvable,
match=f"Could not resolve media item: {invalid_id}",
):
await media_source.async_resolve_media(
hass, f"media-source://image_upload/{invalid_id}", None
)