Skip to content

Commit

Permalink
feat: adding exclude domain and device class
Browse files Browse the repository at this point in the history
  • Loading branch information
Lebe1ge committed Dec 30, 2024
1 parent 2c03084 commit b26af90
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 89 deletions.
12 changes: 8 additions & 4 deletions custom_components/linus_dashboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from .const import (
CONF_ALARM_ENTITY,
CONF_ALARM_ENTITY_ID,
CONF_EXCLUDED_DEVICE_CLASSES,
CONF_EXCLUDED_DOMAINS,
CONF_WEATHER_ENTITY,
CONF_WEATHER_ENTITY_ID,
)
Expand Down Expand Up @@ -124,10 +126,12 @@ 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)
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),
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_EXCLUDED_DOMAINS: config_entries[0].options.get(CONF_EXCLUDED_DOMAINS),
CONF_EXCLUDED_DEVICE_CLASSES: config_entries[0].options.get(
CONF_EXCLUDED_DEVICE_CLASSES
),
}

connection.send_message(websocket_api.result_message(msg["id"], config))
99 changes: 53 additions & 46 deletions custom_components/linus_dashboard/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,26 @@
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.selector import EntitySelector, EntitySelectorConfig

from .const import CONF_ALARM_ENTITY, CONF_WEATHER_ENTITY, DOMAIN
from homeassistant.components.sensor.const import SensorDeviceClass
from homeassistant.components.binary_sensor import BinarySensorDeviceClass
from homeassistant.helpers.selector import (
EntitySelector,
EntitySelectorConfig,
SelectSelector,
SelectSelectorConfig,
)

from .const import (
CONF_ALARM_ENTITY,
CONF_WEATHER_ENTITY,
CONF_EXCLUDED_DOMAINS,
CONF_EXCLUDED_DEVICE_CLASSES,
DOMAIN,
NAME,
)
from homeassistant.const import (
Platform,
)


class NullableEntitySelector(EntitySelector):
Expand All @@ -26,56 +43,24 @@ class LinusDashboardConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):

VERSION = 1

def __init__(self) -> None:
"""Initialize the config flow."""
self._config = {}

async def async_step_user(
self, user_input: dict | None = None
) -> config_entries.ConfigFlowResult:
"""Handle the initial step."""
if user_input is not None:
# Sauvegarde et création de l'entrée de configuration
self._config.update(user_input)
return self.async_create_entry(
title="Linus Dashboard",
data=self._config,
)

# Création du schéma pour le formulaire
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=vol.Schema(schema),
errors={},
)
"""Handle the initial step of the user flow."""
if user_input is not None or self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")
return self.async_create_entry(title=NAME, data={})

@staticmethod
@callback
def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
) -> config_entries.OptionsFlow:
"""Define the options flow."""
return LinusDashboardOptionsFlowHandler(config_entry)
"""Get the options flow for this handler."""
return LinusDashboardEditFlow(config_entry)


class LinusDashboardOptionsFlowHandler(config_entries.OptionsFlow):
class LinusDashboardEditFlow(config_entries.OptionsFlow):
"""Handle options for the Linus Dashboard component."""

def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
Expand All @@ -89,26 +74,48 @@ async def async_step_init(
if user_input is not None:
return self.async_create_entry(title="", data=user_input)

# Récupération des plateforms disponibles depuis Home Assistant
domain_list = list(Platform)

# Récupération des classes de dispositifs disponibles depuis Home Assistant
device_class_list = list(SensorDeviceClass) + list(BinarySensorDeviceClass)

# Création du schéma pour le formulaire d'options
schema = {
vol.Optional(
CONF_ALARM_ENTITY,
default=self.config_entry.options.get(CONF_ALARM_ENTITY)
or self.config_entry.data.get(CONF_ALARM_ENTITY),
default=self.config_entry.options.get(CONF_ALARM_ENTITY),
): NullableEntitySelector(
EntitySelectorConfig(
domain=ALARM_DOMAIN,
),
),
vol.Optional(
CONF_WEATHER_ENTITY,
default=self.config_entry.options.get(CONF_WEATHER_ENTITY)
or self.config_entry.data.get(CONF_WEATHER_ENTITY),
default=self.config_entry.options.get(CONF_WEATHER_ENTITY),
): NullableEntitySelector(
EntitySelectorConfig(
domain=WEATHER_DOMAIN,
),
),
vol.Optional(
CONF_EXCLUDED_DOMAINS,
default=self.config_entry.options.get(CONF_EXCLUDED_DOMAINS, []),
): SelectSelector(
SelectSelectorConfig(
options=domain_list,
multiple=True,
),
),
vol.Optional(
CONF_EXCLUDED_DEVICE_CLASSES,
default=self.config_entry.options.get(CONF_EXCLUDED_DEVICE_CLASSES, []),
): SelectSelector(
SelectSelectorConfig(
options=device_class_list,
multiple=True,
),
),
}

return self.async_show_form(
Expand Down
2 changes: 2 additions & 0 deletions custom_components/linus_dashboard/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
CONF_ALARM_ENTITY_ID = "alarm_entity_id"
CONF_WEATHER_ENTITY = "weather_entity"
CONF_WEATHER_ENTITY_ID = "weather_entity_id"
CONF_EXCLUDED_DOMAINS = "excluded_domains"
CONF_EXCLUDED_DEVICE_CLASSES = "excluded_device_classes"
14 changes: 9 additions & 5 deletions custom_components/linus_dashboard/translations/en.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
{
"config": {
"abort": {
"single_instance_allowed": "Only a single configuration of Linus Dashboard is allowed."
},
"step": {
"user": {
"title": "Linus Dashboard Settings",
"description": "",
"data": {
"alarm_entity": "Select alarm entity",
"weather_entity": "Select weather entity"
"weather_entity": "Select weather entity",
"excluded_domains": "Select domains to exclude",
"excluded_device_classes": "Select device classes to exclude"
}
}
},
"abort": {
"single_instance_allowed": "Only a single configuration of Linus Dashboard is allowed."
}
},
"options": {
Expand All @@ -21,7 +23,9 @@
"description": "",
"data": {
"alarm_entity": "Select alarm entity",
"weather_entity": "Select weather entity"
"weather_entity": "Select weather entity",
"excluded_domains": "Select domains to exclude",
"excluded_device_classes": "Select device classes to exclude"
}
}
}
Expand Down
14 changes: 3 additions & 11 deletions custom_components/linus_dashboard/translations/fr.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
{
"config": {
"step": {
"user": {
"title": "Paramètres du tableau de bord Linus",
"description": "",
"data": {
"alarm_entity": "Sélectionnez l'entité d'alarme",
"weather_entity": "Sélectionnez l'entité météo"
}
}
},
"abort": {
"single_instance_allowed": "Une seule configuration du tableau de bord Linus est autorisée."
}
Expand All @@ -21,7 +11,9 @@
"description": "",
"data": {
"alarm_entity": "Sélectionnez l'entité d'alarme",
"weather_entity": "Sélectionnez l'entité météo"
"weather_entity": "Sélectionnez l'entité météo",
"excluded_domains": "Sélectionnez les domaines à exclure",
"excluded_device_classes": "Sélectionnez les classes de dispositifs à exclure"
}
}
}
Expand Down
Loading

0 comments on commit b26af90

Please sign in to comment.