mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Use Object selector for AndroidTV detection rules option (#84412)
This commit is contained in:
parent
4c8e3aa7c4
commit
5058272818
@ -1,7 +1,6 @@
|
|||||||
"""Config flow to configure the Android TV integration."""
|
"""Config flow to configure the Android TV integration."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import json
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
from typing import Any
|
from typing import Any
|
||||||
@ -19,6 +18,7 @@ from homeassistant.core import callback
|
|||||||
from homeassistant.data_entry_flow import FlowResult
|
from homeassistant.data_entry_flow import FlowResult
|
||||||
from homeassistant.helpers import config_validation as cv
|
from homeassistant.helpers import config_validation as cv
|
||||||
from homeassistant.helpers.selector import (
|
from homeassistant.helpers.selector import (
|
||||||
|
ObjectSelector,
|
||||||
SelectOptionDict,
|
SelectOptionDict,
|
||||||
SelectSelector,
|
SelectSelector,
|
||||||
SelectSelectorConfig,
|
SelectSelectorConfig,
|
||||||
@ -332,8 +332,8 @@ class OptionsFlowHandler(OptionsFlowWithConfigEntry):
|
|||||||
if rule_id:
|
if rule_id:
|
||||||
if user_input.get(CONF_RULE_DELETE, False):
|
if user_input.get(CONF_RULE_DELETE, False):
|
||||||
self._state_det_rules.pop(rule_id)
|
self._state_det_rules.pop(rule_id)
|
||||||
elif str_det_rule := user_input.get(CONF_RULE_VALUES):
|
elif det_rule := user_input.get(CONF_RULE_VALUES):
|
||||||
state_det_rule = _validate_state_det_rules(str_det_rule)
|
state_det_rule = _validate_state_det_rules(det_rule)
|
||||||
if state_det_rule is None:
|
if state_det_rule is None:
|
||||||
return self._async_rules_form(
|
return self._async_rules_form(
|
||||||
rule_id=self._conf_rule_id or RULES_NEW_ID,
|
rule_id=self._conf_rule_id or RULES_NEW_ID,
|
||||||
@ -349,10 +349,11 @@ class OptionsFlowHandler(OptionsFlowWithConfigEntry):
|
|||||||
self, rule_id: str, default_id: str = "", errors: dict[str, str] | None = None
|
self, rule_id: str, default_id: str = "", errors: dict[str, str] | None = None
|
||||||
) -> FlowResult:
|
) -> FlowResult:
|
||||||
"""Return configuration form for detection rules."""
|
"""Return configuration form for detection rules."""
|
||||||
state_det_rule = self._state_det_rules.get(rule_id)
|
rule_schema = {
|
||||||
str_det_rule = json.dumps(state_det_rule) if state_det_rule else ""
|
vol.Optional(
|
||||||
|
CONF_RULE_VALUES, default=self._state_det_rules.get(rule_id)
|
||||||
rule_schema = {vol.Optional(CONF_RULE_VALUES, default=str_det_rule): str}
|
): ObjectSelector()
|
||||||
|
}
|
||||||
if rule_id == RULES_NEW_ID:
|
if rule_id == RULES_NEW_ID:
|
||||||
data_schema = vol.Schema(
|
data_schema = vol.Schema(
|
||||||
{vol.Optional(CONF_RULE_ID, default=default_id): str, **rule_schema}
|
{vol.Optional(CONF_RULE_ID, default=default_id): str, **rule_schema}
|
||||||
@ -372,14 +373,9 @@ class OptionsFlowHandler(OptionsFlowWithConfigEntry):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _validate_state_det_rules(state_det_rules: str) -> list[Any] | None:
|
def _validate_state_det_rules(state_det_rules: Any) -> list[Any] | None:
|
||||||
"""Validate a string that contain state detection rules and return a dict."""
|
"""Validate a string that contain state detection rules and return a dict."""
|
||||||
try:
|
json_rules = state_det_rules
|
||||||
json_rules = json.loads(state_det_rules)
|
|
||||||
except ValueError:
|
|
||||||
_LOGGER.warning("Error loading state detection rules")
|
|
||||||
return None
|
|
||||||
|
|
||||||
if not isinstance(json_rules, list):
|
if not isinstance(json_rules, list):
|
||||||
json_rules = [json_rules]
|
json_rules = [json_rules]
|
||||||
|
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
"""Tests for the AndroidTV config flow."""
|
"""Tests for the AndroidTV config flow."""
|
||||||
import json
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@ -410,7 +409,7 @@ async def test_options_flow(hass):
|
|||||||
result = await hass.config_entries.options.async_configure(
|
result = await hass.config_entries.options.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input={
|
user_input={
|
||||||
CONF_RULE_VALUES: json.dumps({"a": "b"}),
|
CONF_RULE_VALUES: {"a": "b"},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
||||||
@ -421,7 +420,7 @@ async def test_options_flow(hass):
|
|||||||
result = await hass.config_entries.options.async_configure(
|
result = await hass.config_entries.options.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input={
|
user_input={
|
||||||
CONF_RULE_VALUES: json.dumps(["standby"]),
|
CONF_RULE_VALUES: ["standby"],
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
||||||
@ -442,7 +441,7 @@ async def test_options_flow(hass):
|
|||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input={
|
user_input={
|
||||||
CONF_RULE_ID: "rule2",
|
CONF_RULE_ID: "rule2",
|
||||||
CONF_RULE_VALUES: json.dumps(VALID_DETECT_RULE),
|
CONF_RULE_VALUES: VALID_DETECT_RULE,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
||||||
|
Loading…
x
Reference in New Issue
Block a user