Skip to content

Commit

Permalink
Add tests for image entity service
Browse files Browse the repository at this point in the history
  • Loading branch information
NickM-27 committed Feb 9, 2024
1 parent 4076a20 commit ae0cdba
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
6 changes: 3 additions & 3 deletions homeassistant/components/image/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,9 @@ async def async_handle_snapshot_service(
)

async with asyncio.timeout(IMAGE_TIMEOUT):
image = await image.async_image()
image_data = await image.async_image()

if image is None:
if image_data is None:
return

def _write_image(to_file: str, image_data: bytes) -> None:
Expand All @@ -340,6 +340,6 @@ def _write_image(to_file: str, image_data: bytes) -> None:
img_file.write(image_data)

try:
await hass.async_add_executor_job(_write_image, snapshot_file, image)
await hass.async_add_executor_job(_write_image, snapshot_file, image_data)
except OSError as err:
_LOGGER.error("Can't write image to file: %s", err)
2 changes: 1 addition & 1 deletion homeassistant/components/image/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ snapshot:
required: true
example: "/tmp/snapshot_{{ entity_id.name }}.jpg"
selector:
text:
text:
12 changes: 12 additions & 0 deletions homeassistant/components/image/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,17 @@
"_": {
"name": "[%key:component::image::title%]"
}
},
"services": {
"snapshot": {
"name": "Take snapshot",
"description": "Takes a snapshot from an image.",
"fields": {
"filename": {
"name": "Filename",
"description": "Template of a filename. Variable available is `entity_id`."
}
}
}
}
}
47 changes: 46 additions & 1 deletion tests/components/image/test_init.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""The tests for the image component."""
from http import HTTPStatus
import ssl
from unittest.mock import MagicMock, patch
from unittest.mock import MagicMock, mock_open, patch

from aiohttp import hdrs
import httpx
Expand All @@ -10,7 +10,9 @@

from homeassistant.components import image
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.setup import async_setup_component

from .conftest import (
Expand Down Expand Up @@ -287,3 +289,46 @@ async def test_fetch_image_url_wrong_content_type(

resp = await client.get("/api/image_proxy/image.test")
assert resp.status == HTTPStatus.INTERNAL_SERVER_ERROR


async def test_snapshot_service(hass: HomeAssistant) -> None:
"""Test snapshot service."""
mopen = mock_open()

with patch("homeassistant.components.image.open", mopen, create=True), patch(
"homeassistant.components.image.os.makedirs",
), patch.object(hass.config, "is_allowed_path", return_value=True):
await hass.services.async_call(
image.DOMAIN,
image.SERVICE_SNAPSHOT,
{
ATTR_ENTITY_ID: "image.demo_image",
image.ATTR_FILENAME: "/test/snapshot.jpg",
},
blocking=True,
)

mock_write = mopen().write

assert len(mock_write.mock_calls) == 1
assert mock_write.mock_calls[0][1][0] == b"Test"


async def test_snapshot_service_not_allowed_path(
hass: HomeAssistant
) -> None:
"""Test snapshot service with a not allowed path."""
mopen = mock_open()

with patch("homeassistant.components.image.open", mopen, create=True), patch(
"homeassistant.components.image.os.makedirs",
), pytest.raises(HomeAssistantError, match="/test/snapshot.jpg"):
await hass.services.async_call(
image.DOMAIN,
image.SERVICE_SNAPSHOT,
{
ATTR_ENTITY_ID: "image.demo_image",
image.ATTR_FILENAME: "/test/snapshot.jpg",
},
blocking=True,
)

0 comments on commit ae0cdba

Please sign in to comment.