Skip to content

Commit

Permalink
fix(config_flow): allow undefined entities
Browse files Browse the repository at this point in the history
  • Loading branch information
Lebe1ge committed Dec 5, 2024
1 parent c6ebb6d commit 801d5fb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 28 deletions.
6 changes: 4 additions & 2 deletions custom_components/linus_dashboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
61 changes: 41 additions & 20 deletions custom_components/linus_dashboard/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand All @@ -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={},
)

Expand Down Expand Up @@ -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,
),
),
}
Expand Down
6 changes: 3 additions & 3 deletions custom_components/linus_dashboard/translations/en.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"config": {
"step": {
"init": {
"user": {
"title": "Linus Dashboard Settings",
"description": "",
"data": {
Expand All @@ -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"
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions custom_components/linus_dashboard/translations/fr.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"config": {
"step": {
"init": {
"user": {
"title": "Paramètres du tableau de bord Linus",
"description": "",
"data": {
Expand All @@ -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"
}
}
}
Expand Down

0 comments on commit 801d5fb

Please sign in to comment.