mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 11:33:11 -04:00
Make Collection Image a helper (#195918)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Paulus Schoutsen
Claude Opus 5.5
parent
89f0e702f4
commit
5d6edaeb98
@@ -8,18 +8,26 @@ from homeassistant.components.image import DOMAIN as IMAGE_DOMAIN
|
||||
from homeassistant.components.media_player import BrowseError, MediaClass
|
||||
from homeassistant.components.media_source import URI_SCHEME, async_browse_media
|
||||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
||||
from homeassistant.const import CONF_NAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.selector import MediaSelector
|
||||
from homeassistant.helpers.selector import MediaSelector, TextSelector
|
||||
|
||||
from .const import CONF_MEDIA, DOMAIN
|
||||
|
||||
IMAGE_MEDIA_URI = f"{URI_SCHEME}{IMAGE_DOMAIN}"
|
||||
|
||||
MEDIA_SELECTOR = MediaSelector({"accept": ["directory"], "multiple": True})
|
||||
|
||||
STEP_USER_DATA_SCHEMA = probatio.Schema(
|
||||
{
|
||||
probatio.Required(CONF_MEDIA): MediaSelector(
|
||||
{"accept": ["directory"], "multiple": True}
|
||||
),
|
||||
probatio.Required(CONF_NAME): TextSelector(),
|
||||
probatio.Required(CONF_MEDIA): MEDIA_SELECTOR,
|
||||
}
|
||||
)
|
||||
|
||||
STEP_RECONFIGURE_DATA_SCHEMA = probatio.Schema(
|
||||
{
|
||||
probatio.Required(CONF_MEDIA): MEDIA_SELECTOR,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -27,12 +35,11 @@ STEP_USER_DATA_SCHEMA = probatio.Schema(
|
||||
async def _async_validate_media(
|
||||
hass: HomeAssistant,
|
||||
user_input: dict[str, Any],
|
||||
) -> tuple[str | None, dict[str, str], dict[str, str]]:
|
||||
"""Validate selected directories and return title and form errors."""
|
||||
) -> tuple[dict[str, str], dict[str, str]]:
|
||||
"""Validate selected directories and return form errors."""
|
||||
errors: dict[str, str] = {}
|
||||
placeholders: dict[str, str] = {}
|
||||
found_pictures = False
|
||||
title = "Unnamed collection"
|
||||
|
||||
for user_media in user_input[CONF_MEDIA]:
|
||||
if user_media["media_content_id"] == IMAGE_MEDIA_URI:
|
||||
@@ -50,19 +57,15 @@ async def _async_validate_media(
|
||||
placeholders["error"] = str(err)
|
||||
break
|
||||
|
||||
if (
|
||||
not found_pictures
|
||||
and browse.children
|
||||
and any(item.media_class == MediaClass.IMAGE for item in browse.children)
|
||||
if browse.children and any(
|
||||
item.media_class == MediaClass.IMAGE for item in browse.children
|
||||
):
|
||||
found_pictures = True
|
||||
if browse.title:
|
||||
title = f"{browse.title} collection"
|
||||
|
||||
if not errors and not found_pictures:
|
||||
errors[CONF_MEDIA] = "selected_media_no_images"
|
||||
|
||||
return (title if not errors else None), errors, placeholders
|
||||
return errors, placeholders
|
||||
|
||||
|
||||
class CollectionImageConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
@@ -76,11 +79,11 @@ class CollectionImageConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
placeholders: dict[str, str] = {}
|
||||
entry = self._get_reconfigure_entry()
|
||||
if user_input is not None:
|
||||
title, errors, placeholders = await _async_validate_media(
|
||||
errors, placeholders = await _async_validate_media(
|
||||
self.hass,
|
||||
user_input,
|
||||
)
|
||||
if title is not None:
|
||||
if not errors:
|
||||
return self.async_update_reload_and_abort(
|
||||
entry, data_updates=user_input
|
||||
)
|
||||
@@ -88,7 +91,7 @@ class CollectionImageConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
return self.async_show_form(
|
||||
step_id="reconfigure",
|
||||
data_schema=self.add_suggested_values_to_schema(
|
||||
STEP_USER_DATA_SCHEMA,
|
||||
STEP_RECONFIGURE_DATA_SCHEMA,
|
||||
user_input
|
||||
or {CONF_MEDIA: probatio.EnsureList()(entry.data[CONF_MEDIA])},
|
||||
),
|
||||
@@ -106,14 +109,14 @@ class CollectionImageConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
placeholders: dict[str, str] = {}
|
||||
|
||||
if user_input is not None:
|
||||
title, errors, placeholders = await _async_validate_media(
|
||||
errors, placeholders = await _async_validate_media(
|
||||
self.hass,
|
||||
user_input,
|
||||
)
|
||||
if title is not None:
|
||||
if not errors:
|
||||
return self.async_create_entry(
|
||||
title=title,
|
||||
data=user_input,
|
||||
title=user_input[CONF_NAME],
|
||||
data={CONF_MEDIA: user_input[CONF_MEDIA]},
|
||||
)
|
||||
|
||||
return self.async_show_form(
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
"codeowners": ["@karwosts"],
|
||||
"config_flow": true,
|
||||
"documentation": "https://www.home-assistant.io/integrations/collection_image",
|
||||
"integration_type": "service",
|
||||
"integration_type": "helper",
|
||||
"iot_class": "calculated",
|
||||
"quality_scale": "silver"
|
||||
}
|
||||
|
||||
@@ -17,10 +17,12 @@
|
||||
},
|
||||
"user": {
|
||||
"data": {
|
||||
"media": "Media"
|
||||
"media": "Media",
|
||||
"name": "[%key:common::config_flow::data::name%]"
|
||||
},
|
||||
"data_description": {
|
||||
"media": "The media where images will be retrieved from."
|
||||
"media": "The media where images will be retrieved from.",
|
||||
"name": "The name of the image entity."
|
||||
},
|
||||
"description": "The Collection Image integration creates an image entity which renders an image from the selected media.",
|
||||
"submit": "Create"
|
||||
|
||||
Generated
+1
-1
@@ -5,6 +5,7 @@ To update, run python3 -m script.hassfest
|
||||
|
||||
FLOWS = {
|
||||
"helper": [
|
||||
"collection_image",
|
||||
"compensation",
|
||||
"derivative",
|
||||
"filter",
|
||||
@@ -151,7 +152,6 @@ FLOWS = {
|
||||
"cloudflare_r2",
|
||||
"co2signal",
|
||||
"coinbase",
|
||||
"collection_image",
|
||||
"color_extractor",
|
||||
"comelit",
|
||||
"compit",
|
||||
|
||||
@@ -2490,15 +2490,6 @@
|
||||
"config_flow": true,
|
||||
"iot_class": "cloud_polling"
|
||||
},
|
||||
"collection_image": {
|
||||
"integration_type": "service",
|
||||
"categories": [
|
||||
"helpers",
|
||||
"productivity_information.documents_photos"
|
||||
],
|
||||
"config_flow": true,
|
||||
"iot_class": "calculated"
|
||||
},
|
||||
"colmo": {
|
||||
"name": "Colmo",
|
||||
"integration_type": "virtual",
|
||||
@@ -17864,6 +17855,15 @@
|
||||
}
|
||||
},
|
||||
"helper": {
|
||||
"collection_image": {
|
||||
"integration_type": "helper",
|
||||
"categories": [
|
||||
"helpers",
|
||||
"productivity_information.documents_photos"
|
||||
],
|
||||
"config_flow": true,
|
||||
"iot_class": "calculated"
|
||||
},
|
||||
"compensation": {
|
||||
"name": "Compensation",
|
||||
"integration_type": "helper",
|
||||
|
||||
@@ -8,6 +8,7 @@ from pytest_homeassistant.common import MockConfigEntry
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.collection_image.config_flow import IMAGE_MEDIA_URI
|
||||
from homeassistant.components.collection_image.const import CONF_MEDIA, DOMAIN
|
||||
from homeassistant.const import CONF_NAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
from homeassistant.util import slugify
|
||||
@@ -22,21 +23,20 @@ from .helpers import data_from_uri
|
||||
|
||||
TEST_TIME = "2026-09-12T07:12:00+00:00"
|
||||
TEST_TIME_NEXT = "2026-09-12T07:30:00+00:00"
|
||||
TEST_NAME = "My collection"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("uris", "expected_title"),
|
||||
"uris",
|
||||
[
|
||||
([MOCK_MEDIA_DIR_URI_1], "My pictures collection"),
|
||||
([MOCK_MEDIA_DIR_URI_1, MOCK_MEDIA_DIR_URI_2], "My pictures collection"),
|
||||
([MOCK_MEDIA_DIR_URI_2, MOCK_MEDIA_DIR_URI_1], "Three pictures collection"),
|
||||
[MOCK_MEDIA_DIR_URI_1],
|
||||
[MOCK_MEDIA_DIR_URI_1, MOCK_MEDIA_DIR_URI_2],
|
||||
[MOCK_MEDIA_DIR_URI_2, MOCK_MEDIA_DIR_URI_1],
|
||||
],
|
||||
)
|
||||
@pytest.mark.usefixtures("mock_media_source")
|
||||
@freeze_time(TEST_TIME)
|
||||
async def test_config_flow(
|
||||
hass: HomeAssistant, uris: list[str], expected_title: str
|
||||
) -> None:
|
||||
async def test_config_flow(hass: HomeAssistant, uris: list[str]) -> None:
|
||||
"""Test the config flow."""
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
@@ -47,15 +47,17 @@ async def test_config_flow(
|
||||
|
||||
data = data_from_uri(uris)
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(result["flow_id"], data)
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_NAME: TEST_NAME, **data}
|
||||
)
|
||||
|
||||
assert result.get("type") is FlowResultType.CREATE_ENTRY
|
||||
assert result.get("title") == expected_title
|
||||
assert result.get("title") == TEST_NAME
|
||||
assert result.get("data") == data
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(f"image.{slugify(expected_title)}")
|
||||
state = hass.states.get(f"image.{slugify(TEST_NAME)}")
|
||||
assert state and state.state == TEST_TIME
|
||||
|
||||
|
||||
@@ -110,7 +112,9 @@ async def test_config_flow_error(
|
||||
assert result.get("errors") == {}
|
||||
|
||||
data = data_from_uri(uris)
|
||||
result = await hass.config_entries.flow.async_configure(result["flow_id"], data)
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_NAME: TEST_NAME, **data}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result.get("type") is FlowResultType.FORM
|
||||
@@ -134,17 +138,18 @@ async def test_config_flow_error(
|
||||
|
||||
# Try again successfully to ensure we can recover from errors
|
||||
data = data_from_uri([MOCK_MEDIA_DIR_URI_1])
|
||||
expected_title = "My pictures collection"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(result["flow_id"], data)
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_NAME: TEST_NAME, **data}
|
||||
)
|
||||
|
||||
assert result.get("type") is FlowResultType.CREATE_ENTRY
|
||||
assert result.get("title") == expected_title
|
||||
assert result.get("title") == TEST_NAME
|
||||
assert result.get("data") == data
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(f"image.{slugify(expected_title)}")
|
||||
state = hass.states.get(f"image.{slugify(TEST_NAME)}")
|
||||
assert state and state.state == TEST_TIME
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user