Use Object selector for AndroidTV detection rules option (#84412)

This commit is contained in:
ollo69 2022-12-27 15:08:23 +01:00 committed by GitHub
parent 4c8e3aa7c4
commit 5058272818
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 18 deletions

View File

@ -1,7 +1,6 @@
"""Config flow to configure the Android TV integration."""
from __future__ import annotations
import json
import logging
import os
from typing import Any
@ -19,6 +18,7 @@ from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.selector import (
ObjectSelector,
SelectOptionDict,
SelectSelector,
SelectSelectorConfig,
@ -332,8 +332,8 @@ class OptionsFlowHandler(OptionsFlowWithConfigEntry):
if rule_id:
if user_input.get(CONF_RULE_DELETE, False):
self._state_det_rules.pop(rule_id)
elif str_det_rule := user_input.get(CONF_RULE_VALUES):
state_det_rule = _validate_state_det_rules(str_det_rule)
elif det_rule := user_input.get(CONF_RULE_VALUES):
state_det_rule = _validate_state_det_rules(det_rule)
if state_det_rule is None:
return self._async_rules_form(
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
) -> FlowResult:
"""Return configuration form for detection rules."""
state_det_rule = self._state_det_rules.get(rule_id)
str_det_rule = json.dumps(state_det_rule) if state_det_rule else ""
rule_schema = {vol.Optional(CONF_RULE_VALUES, default=str_det_rule): str}
rule_schema = {
vol.Optional(
CONF_RULE_VALUES, default=self._state_det_rules.get(rule_id)
): ObjectSelector()
}
if rule_id == RULES_NEW_ID:
data_schema = vol.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."""
try:
json_rules = json.loads(state_det_rules)
except ValueError:
_LOGGER.warning("Error loading state detection rules")
return None
json_rules = state_det_rules
if not isinstance(json_rules, list):
json_rules = [json_rules]

View File

@ -1,5 +1,4 @@
"""Tests for the AndroidTV config flow."""
import json
from unittest.mock import patch
import pytest
@ -410,7 +409,7 @@ async def test_options_flow(hass):
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
CONF_RULE_VALUES: json.dumps({"a": "b"}),
CONF_RULE_VALUES: {"a": "b"},
},
)
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["flow_id"],
user_input={
CONF_RULE_VALUES: json.dumps(["standby"]),
CONF_RULE_VALUES: ["standby"],
},
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
@ -442,7 +441,7 @@ async def test_options_flow(hass):
result["flow_id"],
user_input={
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