Use the infrared consumer config flow in Alpha Bidet Infrared (#195082)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Abílio Costa
2026-11-30 17:24:03 +00:00
committed by GitHub
co-authored by Claude Opus 5.5
parent de580a04b2
commit ff69ceadc4
4 changed files with 26 additions and 84 deletions
@@ -4,16 +4,10 @@ from typing import Any, override
import probatio
from homeassistant.components.infrared import (
DOMAIN as INFRARED_DOMAIN,
async_get_emitters,
)
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.components.infrared import InfraredConsumerConfigFlow
from homeassistant.const import CONF_MODEL
from homeassistant.core import callback
from homeassistant.helpers.selector import (
EntitySelector,
EntitySelectorConfig,
SelectSelector,
SelectSelectorConfig,
SelectSelectorMode,
@@ -22,78 +16,30 @@ from homeassistant.helpers.selector import (
from .const import CONF_INFRARED_EMITTER_ENTITY_ID, DOMAIN, AlphaBidetModel
@callback
def _emitter_schema(emitter_entity_ids: list[str]) -> probatio.Schema:
"""Return the infrared emitter selection schema."""
return probatio.Schema(
{
probatio.Required(CONF_INFRARED_EMITTER_ENTITY_ID): EntitySelector(
EntitySelectorConfig(
domain=INFRARED_DOMAIN, include_entities=emitter_entity_ids
)
),
}
)
class AlphaBidetIrConfigFlow(ConfigFlow, domain=DOMAIN):
class AlphaBidetIrConfigFlow(InfraredConsumerConfigFlow, domain=DOMAIN):
"""Handle config flow for Alpha Bidet IR."""
VERSION = 1
INFRARED_EMITTER_KEY = CONF_INFRARED_EMITTER_ENTITY_ID
@override
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle the initial step - select IR emitter and bidet model."""
emitter_entity_ids = async_get_emitters(self.hass)
if not emitter_entity_ids:
return self.async_abort(reason="no_emitters")
if user_input is not None:
await self.async_set_unique_id(user_input[CONF_INFRARED_EMITTER_ENTITY_ID])
self._abort_if_unique_id_configured()
model = AlphaBidetModel(user_input[CONF_MODEL])
return self.async_create_entry(
title=f"Alpha Bidet {model.value}", data=user_input
)
return self.async_show_form(
step_id="user",
data_schema=_emitter_schema(emitter_entity_ids).extend(
{
probatio.Required(CONF_MODEL): SelectSelector(
SelectSelectorConfig(
options=[model.value for model in AlphaBidetModel],
mode=SelectSelectorMode.DROPDOWN,
)
),
}
),
@callback
def _async_infrared_schema(self) -> probatio.Schema:
"""Return the emitter and bidet model selection schema."""
return probatio.Schema(
{
**self._async_infrared_entity_fields(),
probatio.Required(CONF_MODEL): SelectSelector(
SelectSelectorConfig(
options=[model.value for model in AlphaBidetModel],
mode=SelectSelectorMode.DROPDOWN,
)
),
}
)
async def async_step_reconfigure(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle reconfiguration of the infrared emitter."""
entry = self._get_reconfigure_entry()
emitter_entity_ids = async_get_emitters(self.hass)
if not emitter_entity_ids:
return self.async_abort(reason="no_emitters")
if user_input is not None:
emitter_entity_id = user_input[CONF_INFRARED_EMITTER_ENTITY_ID]
self._async_abort_entries_match(
{CONF_INFRARED_EMITTER_ENTITY_ID: emitter_entity_id}
)
return self.async_update_reload_and_abort(
entry, unique_id=emitter_entity_id, data_updates=user_input
)
return self.async_show_form(
step_id="reconfigure",
data_schema=self.add_suggested_values_to_schema(
_emitter_schema(emitter_entity_ids), entry.data
),
)
@override
async def _async_infrared_entry_title(self, user_input: dict[str, Any]) -> str:
"""Return the title of a new entry."""
model = AlphaBidetModel(user_input[CONF_MODEL])
return f"Alpha Bidet {model.value}"
@@ -2,7 +2,7 @@
"config": {
"abort": {
"already_configured": "This infrared emitter is already configured to control a bidet.",
"no_emitters": "[%key:component::infrared::common::no_infrared_emitters%]"
"no_infrared_emitters": "[%key:component::infrared::common::no_infrared_emitters%]"
},
"step": {
"reconfigure": {
@@ -30,7 +30,6 @@ def mock_config_entry() -> MockConfigEntry:
CONF_INFRARED_EMITTER_ENTITY_ID: MOCK_INFRARED_EMITTER_ENTITY_ID,
CONF_MODEL: AlphaBidetModel.JX2.value,
},
unique_id=MOCK_INFRARED_EMITTER_ENTITY_ID,
)
@@ -54,7 +54,7 @@ async def test_user_flow_success(hass: HomeAssistant) -> None:
CONF_INFRARED_EMITTER_ENTITY_ID: MOCK_INFRARED_EMITTER_ENTITY_ID,
CONF_MODEL: AlphaBidetModel.JX2.value,
}
assert result["result"].unique_id == MOCK_INFRARED_EMITTER_ENTITY_ID
assert result["result"].unique_id is None
@pytest.mark.usefixtures("mock_infrared_emitter_entity")
@@ -90,7 +90,7 @@ async def test_user_flow_no_emitters(hass: HomeAssistant) -> None:
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "no_emitters"
assert result["reason"] == "no_infrared_emitters"
@pytest.mark.usefixtures("mock_infrared_emitter_entity")
@@ -123,7 +123,7 @@ async def test_reconfigure_flow_suggests_current_emitter(
async def test_reconfigure_flow(
hass: HomeAssistant, init_integration: MockConfigEntry, emitter_entity_id: str
) -> None:
"""Test reconfiguring updates the emitter and unique ID and reloads the entry."""
"""Test reconfiguring updates the emitter and reloads the entry."""
result = await init_integration.start_reconfigure_flow(hass)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
@@ -137,7 +137,6 @@ async def test_reconfigure_flow(
CONF_INFRARED_EMITTER_ENTITY_ID: emitter_entity_id,
CONF_MODEL: AlphaBidetModel.JX2.value,
}
assert init_integration.unique_id == emitter_entity_id
assert init_integration.state is ConfigEntryState.LOADED
@@ -153,7 +152,6 @@ async def test_reconfigure_flow_already_configured(
CONF_INFRARED_EMITTER_ENTITY_ID: SECOND_EMITTER_ENTITY_ID,
CONF_MODEL: AlphaBidetModel.JX2.value,
},
unique_id=SECOND_EMITTER_ENTITY_ID,
).add_to_hass(hass)
result = await mock_config_entry.start_reconfigure_flow(hass)
@@ -168,7 +166,6 @@ async def test_reconfigure_flow_already_configured(
mock_config_entry.data[CONF_INFRARED_EMITTER_ENTITY_ID]
== MOCK_INFRARED_EMITTER_ENTITY_ID
)
assert mock_config_entry.unique_id == MOCK_INFRARED_EMITTER_ENTITY_ID
@pytest.mark.usefixtures("init_infrared")
@@ -181,4 +178,4 @@ async def test_reconfigure_flow_no_emitters(
result = await mock_config_entry.start_reconfigure_flow(hass)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "no_emitters"
assert result["reason"] == "no_infrared_emitters"