Add config flow to color extractor (#100862)
This commit is contained in:
parent
0f38cd5794
commit
fd53e116bb
9 changed files with 170 additions and 5 deletions
|
@ -13,6 +13,7 @@ from homeassistant.components.light import (
|
|||
DOMAIN as LIGHT_DOMAIN,
|
||||
LIGHT_TURN_ON_SCHEMA,
|
||||
)
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
||||
from homeassistant.const import SERVICE_TURN_ON as LIGHT_SERVICE_TURN_ON
|
||||
from homeassistant.core import HomeAssistant, ServiceCall
|
||||
from homeassistant.helpers import aiohttp_client
|
||||
|
@ -58,8 +59,20 @@ def _get_color(file_handler) -> tuple:
|
|||
return color
|
||||
|
||||
|
||||
async def async_setup(hass: HomeAssistant, hass_config: ConfigType) -> bool:
|
||||
"""Set up services for color_extractor integration."""
|
||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
"""Set up the Color extractor component."""
|
||||
|
||||
hass.async_create_task(
|
||||
hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_IMPORT}, data={}
|
||||
)
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Load a config entry."""
|
||||
|
||||
async def async_handle_service(service_call: ServiceCall) -> None:
|
||||
"""Decide which color_extractor method to call based on service."""
|
||||
|
|
64
homeassistant/components/color_extractor/config_flow.py
Normal file
64
homeassistant/components/color_extractor/config_flow.py
Normal file
|
@ -0,0 +1,64 @@
|
|||
"""Config flow to configure the Color extractor integration."""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.config_entries import ConfigFlow
|
||||
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN
|
||||
from homeassistant.data_entry_flow import FlowResult, FlowResultType
|
||||
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
||||
|
||||
from .const import DEFAULT_NAME, DOMAIN
|
||||
|
||||
|
||||
class ColorExtractorConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
"""Config flow for Color extractor."""
|
||||
|
||||
VERSION = 1
|
||||
|
||||
async def async_step_user(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> FlowResult:
|
||||
"""Handle a flow initialized by the user."""
|
||||
if self._async_current_entries():
|
||||
return self.async_abort(reason="single_instance_allowed")
|
||||
|
||||
if user_input is not None:
|
||||
return self.async_create_entry(title=DEFAULT_NAME, data={})
|
||||
|
||||
return self.async_show_form(step_id="user")
|
||||
|
||||
async def async_step_import(self, user_input: dict[str, Any]) -> FlowResult:
|
||||
"""Handle import from configuration.yaml."""
|
||||
result = await self.async_step_user(user_input)
|
||||
if result["type"] == FlowResultType.CREATE_ENTRY:
|
||||
async_create_issue(
|
||||
self.hass,
|
||||
HOMEASSISTANT_DOMAIN,
|
||||
f"deprecated_yaml_{DOMAIN}",
|
||||
breaks_in_ha_version="2024.4.0",
|
||||
is_fixable=False,
|
||||
issue_domain=DOMAIN,
|
||||
severity=IssueSeverity.WARNING,
|
||||
translation_key="deprecated_yaml",
|
||||
translation_placeholders={
|
||||
"domain": DOMAIN,
|
||||
"integration_title": "Color extractor",
|
||||
},
|
||||
)
|
||||
else:
|
||||
async_create_issue(
|
||||
self.hass,
|
||||
DOMAIN,
|
||||
"deprecated_yaml",
|
||||
breaks_in_ha_version="2024.4.0",
|
||||
is_fixable=False,
|
||||
issue_domain=DOMAIN,
|
||||
severity=IssueSeverity.WARNING,
|
||||
translation_key="deprecated_yaml",
|
||||
translation_placeholders={
|
||||
"domain": DOMAIN,
|
||||
"integration_title": "Color extractor",
|
||||
},
|
||||
)
|
||||
return result
|
|
@ -3,5 +3,6 @@ ATTR_PATH = "color_extract_path"
|
|||
ATTR_URL = "color_extract_url"
|
||||
|
||||
DOMAIN = "color_extractor"
|
||||
DEFAULT_NAME = "Color extractor"
|
||||
|
||||
SERVICE_TURN_ON = "turn_on"
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
"domain": "color_extractor",
|
||||
"name": "ColorExtractor",
|
||||
"codeowners": ["@GenericStudent"],
|
||||
"config_flow": false,
|
||||
"config_flow": true,
|
||||
"documentation": "https://www.home-assistant.io/integrations/color_extractor",
|
||||
"requirements": ["colorthief==0.2.1"]
|
||||
}
|
||||
|
|
|
@ -1,4 +1,20 @@
|
|||
{
|
||||
"config": {
|
||||
"step": {
|
||||
"user": {
|
||||
"description": "[%key:common::config_flow::description::confirm_setup%]"
|
||||
}
|
||||
},
|
||||
"abort": {
|
||||
"single_instance_allowed": "[%key:common::config_flow::abort::single_instance_allowed%]"
|
||||
}
|
||||
},
|
||||
"issues": {
|
||||
"deprecated_yaml": {
|
||||
"title": "The {integration_title} YAML configuration is being removed",
|
||||
"description": "Configuring {integration_title} using YAML is being removed.\n\nYour configuration is already imported.\n\nRemove the `{domain}` configuration from your configuration.yaml file and restart Home Assistant to fix this issue."
|
||||
}
|
||||
},
|
||||
"services": {
|
||||
"turn_on": {
|
||||
"name": "[%key:common::action::turn_on%]",
|
||||
|
|
|
@ -83,6 +83,7 @@ FLOWS = {
|
|||
"cloudflare",
|
||||
"co2signal",
|
||||
"coinbase",
|
||||
"color_extractor",
|
||||
"comelit",
|
||||
"control4",
|
||||
"coolmaster",
|
||||
|
|
|
@ -876,7 +876,7 @@
|
|||
"color_extractor": {
|
||||
"name": "ColorExtractor",
|
||||
"integration_type": "hub",
|
||||
"config_flow": false
|
||||
"config_flow": true
|
||||
},
|
||||
"comed": {
|
||||
"name": "Commonwealth Edison (ComEd)",
|
||||
|
|
70
tests/components/color_extractor/test_config_flow.py
Normal file
70
tests/components/color_extractor/test_config_flow.py
Normal file
|
@ -0,0 +1,70 @@
|
|||
"""Tests for the Color extractor config flow."""
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.color_extractor.const import DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_full_user_flow(hass: HomeAssistant) -> None:
|
||||
"""Test the full user configuration flow."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
assert result.get("type") == FlowResultType.FORM
|
||||
assert result.get("step_id") == "user"
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.color_extractor.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={},
|
||||
)
|
||||
|
||||
assert result.get("type") == FlowResultType.CREATE_ENTRY
|
||||
assert result.get("title") == "Color extractor"
|
||||
assert result.get("data") == {}
|
||||
assert result.get("options") == {}
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize("source", [SOURCE_USER, SOURCE_IMPORT])
|
||||
async def test_single_instance_allowed(
|
||||
hass: HomeAssistant,
|
||||
source: str,
|
||||
) -> None:
|
||||
"""Test we abort if already setup."""
|
||||
mock_config_entry = MockConfigEntry(domain=DOMAIN)
|
||||
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": source}, data={}
|
||||
)
|
||||
|
||||
assert result.get("type") == FlowResultType.ABORT
|
||||
assert result.get("reason") == "single_instance_allowed"
|
||||
|
||||
|
||||
async def test_import_flow(
|
||||
hass: HomeAssistant,
|
||||
) -> None:
|
||||
"""Test the import configuration flow."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_IMPORT},
|
||||
data={},
|
||||
)
|
||||
|
||||
assert result.get("type") == FlowResultType.CREATE_ENTRY
|
||||
assert result.get("title") == "Color extractor"
|
||||
assert result.get("data") == {}
|
||||
assert result.get("options") == {}
|
|
@ -63,7 +63,7 @@ def _close_enough(actual_rgb, testing_rgb):
|
|||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
async def setup_light(hass):
|
||||
async def setup_light(hass: HomeAssistant):
|
||||
"""Configure our light component to work against for testing."""
|
||||
assert await async_setup_component(
|
||||
hass, LIGHT_DOMAIN, {LIGHT_DOMAIN: {"platform": "demo"}}
|
||||
|
|
Loading…
Add table
Reference in a new issue