From 801d5fb2a3d38405506d2b99423052e9db31e6f4 Mon Sep 17 00:00:00 2001 From: Juicy Date: Thu, 5 Dec 2024 15:55:38 +0000 Subject: [PATCH] fix(config_flow): allow undefined entities --- custom_components/linus_dashboard/__init__.py | 6 +- .../linus_dashboard/config_flow.py | 61 +++++++++++++------ .../linus_dashboard/translations/en.json | 6 +- .../linus_dashboard/translations/fr.json | 6 +- 4 files changed, 51 insertions(+), 28 deletions(-) diff --git a/custom_components/linus_dashboard/__init__.py b/custom_components/linus_dashboard/__init__.py index 4b1bdf6..b5a8ff1 100644 --- a/custom_components/linus_dashboard/__init__.py +++ b/custom_components/linus_dashboard/__init__.py @@ -124,8 +124,10 @@ async def websocket_get_entities( """Handle request for getting entities.""" config_entries = hass.config_entries.async_entries(DOMAIN) config = { - CONF_ALARM_ENTITY_ID: config_entries[0].options.get(CONF_ALARM_ENTITY), - CONF_WEATHER_ENTITY_ID: config_entries[0].options.get(CONF_WEATHER_ENTITY), + CONF_ALARM_ENTITY_ID: config_entries[0].options.get(CONF_ALARM_ENTITY) + or config_entries[0].data.get(CONF_ALARM_ENTITY), + CONF_WEATHER_ENTITY_ID: config_entries[0].options.get(CONF_WEATHER_ENTITY) + or config_entries[0].data.get(CONF_WEATHER_ENTITY), } connection.send_message(websocket_api.result_message(msg["id"], config)) diff --git a/custom_components/linus_dashboard/config_flow.py b/custom_components/linus_dashboard/config_flow.py index 974575b..0f517e2 100644 --- a/custom_components/linus_dashboard/config_flow.py +++ b/custom_components/linus_dashboard/config_flow.py @@ -2,12 +2,25 @@ import voluptuous as vol from homeassistant import config_entries +from homeassistant.components.alarm_control_panel.const import DOMAIN as ALARM_DOMAIN +from homeassistant.components.weather.const import DOMAIN as WEATHER_DOMAIN from homeassistant.core import callback -from homeassistant.helpers import selector +from homeassistant.helpers.selector import EntitySelector, EntitySelectorConfig from .const import CONF_ALARM_ENTITY, CONF_WEATHER_ENTITY, DOMAIN +class NullableEntitySelector(EntitySelector): + """Entity selector that supports null values.""" + + def __call__(self, data: str | None) -> str | None: + """Validate the passed selection, if passed.""" + if data in (None, ""): + return data + + return super().__call__(data) + + class LinusDashboardConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): """Handle a config flow for the Linus Dashboard component.""" @@ -29,21 +42,27 @@ async def async_step_user( data=self._config, ) - # Récupérer les entités disponibles dans Home Assistant - alarm_entities = list(self.hass.states.async_entity_ids("alarm_control_panel")) - weather_entities = list(self.hass.states.async_entity_ids("weather")) - # Création du schéma pour le formulaire - schema = vol.Schema( - { - vol.Required(CONF_ALARM_ENTITY): vol.In(alarm_entities), - vol.Required(CONF_WEATHER_ENTITY): vol.In(weather_entities), - } - ) + schema = { + vol.Optional( + CONF_ALARM_ENTITY, + ): NullableEntitySelector( + EntitySelectorConfig( + domain=ALARM_DOMAIN, + ), + ), + vol.Optional( + CONF_WEATHER_ENTITY, + ): NullableEntitySelector( + EntitySelectorConfig( + domain=WEATHER_DOMAIN, + ), + ), + } return self.async_show_form( step_id="user", - data_schema=schema, + data_schema=vol.Schema(schema), errors={}, ) @@ -74,18 +93,20 @@ async def async_step_init( schema = { vol.Optional( CONF_ALARM_ENTITY, - default=self.config_entry.options.get(CONF_ALARM_ENTITY, ""), - ): selector.EntitySelector( - selector.EntitySelectorConfig( - domain="alarm_control_panel", + default=self.config_entry.options.get(CONF_ALARM_ENTITY) + or self.config_entry.data.get(CONF_ALARM_ENTITY), + ): NullableEntitySelector( + EntitySelectorConfig( + domain=ALARM_DOMAIN, ), ), vol.Optional( CONF_WEATHER_ENTITY, - default=self.config_entry.options.get(CONF_WEATHER_ENTITY, ""), - ): selector.EntitySelector( - selector.EntitySelectorConfig( - domain="weather", + default=self.config_entry.options.get(CONF_WEATHER_ENTITY) + or self.config_entry.data.get(CONF_WEATHER_ENTITY), + ): NullableEntitySelector( + EntitySelectorConfig( + domain=WEATHER_DOMAIN, ), ), } diff --git a/custom_components/linus_dashboard/translations/en.json b/custom_components/linus_dashboard/translations/en.json index 685905b..13a5f0a 100644 --- a/custom_components/linus_dashboard/translations/en.json +++ b/custom_components/linus_dashboard/translations/en.json @@ -1,7 +1,7 @@ { "config": { "step": { - "init": { + "user": { "title": "Linus Dashboard Settings", "description": "", "data": { @@ -20,8 +20,8 @@ "title": "Linus Dashboard Settings", "description": "", "data": { - "alarm_entity": "Alarm entity", - "weather_entity": "Weather entity" + "alarm_entity": "Select alarm entity", + "weather_entity": "Select weather entity" } } } diff --git a/custom_components/linus_dashboard/translations/fr.json b/custom_components/linus_dashboard/translations/fr.json index f9df78e..572baf8 100644 --- a/custom_components/linus_dashboard/translations/fr.json +++ b/custom_components/linus_dashboard/translations/fr.json @@ -1,7 +1,7 @@ { "config": { "step": { - "init": { + "user": { "title": "Paramètres du tableau de bord Linus", "description": "", "data": { @@ -20,8 +20,8 @@ "title": "Paramètres du tableau de bord Linus", "description": "", "data": { - "alarm_entity": "Entité d'alarme", - "weather_entity": "Entité météo" + "alarm_entity": "Sélectionnez l'entité d'alarme", + "weather_entity": "Sélectionnez l'entité météo" } } }