-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
109 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,48 @@ | ||
import voluptuous as vol | ||
from homeassistant import config_entries | ||
from homeassistant.core import callback | ||
import voluptuous as vol | ||
from homeassistant.helpers.selector import selector | ||
from .const import DOMAIN | ||
|
||
# Parametri di configurazione | ||
CONF_BRIGHTNESS_ON_SWITCH = "brightness_on_switch" | ||
CONF_TURN_OFF_PHYSICAL = "turn_off_physical" | ||
CONF_PHYSICAL_LIGHT = "physical_light" | ||
CONF_SMART_LIGHT = "smart_light" | ||
|
||
class LightPairingConfigFlow(config_entries.ConfigFlow, domain="light_pairing"): | ||
"""Gestisce il flusso di configurazione per il pairing delle luci.""" | ||
|
||
class LightPairConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): | ||
VERSION = 1 | ||
|
||
async def async_step_user(self, user_input=None): | ||
"""Gestisce lo step iniziale.""" | ||
errors = {} | ||
|
||
"""Gestisce il flusso di configurazione iniziale.""" | ||
if user_input is not None: | ||
# Gestisce i dati inseriti dall'utente | ||
return self.async_create_entry(title=user_input["name"], data=user_input) | ||
|
||
schema = vol.Schema({ | ||
vol.Required("name"): str, | ||
vol.Required(CONF_PHYSICAL_LIGHT): selector({ | ||
"entity": {"domain": "light"} | ||
}), | ||
vol.Required(CONF_SMART_LIGHT): selector({ | ||
"entity": {"domain": "light"} | ||
}), | ||
vol.Optional(CONF_BRIGHTNESS_ON_SWITCH, default=100): vol.All(vol.Coerce(int), vol.Range(min=0, max=100)), | ||
vol.Optional(CONF_TURN_OFF_PHYSICAL, default=False): bool | ||
}) | ||
|
||
return self.async_show_form( | ||
step_id="user", | ||
data_schema=schema, | ||
errors=errors | ||
data_schema=vol.Schema({ | ||
vol.Required("physical_light"): selector({"entity": {"domain": ["switch", "light"]}}), | ||
vol.Required("smart_light"): selector({"entity": {"domain": "light"}}), | ||
vol.Required("name"): str, | ||
vol.Optional("brightness_on_switch", default=80): vol.All(vol.Coerce(int), vol.Range(min=0, max=100)), | ||
vol.Optional("turn_off_physical", default=True): bool | ||
}) | ||
) | ||
|
||
@staticmethod | ||
@callback | ||
def async_get_options_flow(config_entry): | ||
"""Ritorna il flusso di opzioni per la riconfigurazione.""" | ||
return LightPairingOptionsFlowHandler(config_entry) | ||
|
||
|
||
class LightPairingOptionsFlowHandler(config_entries.OptionsFlow): | ||
"""Gestisce il flusso di opzioni per riconfigurare i parametri.""" | ||
|
||
def __init__(self, config_entry): | ||
"""Inizializza il flusso di opzioni.""" | ||
self.config_entry = config_entry | ||
|
||
async def async_step_init(self, user_input=None): | ||
"""Inizia il flusso di riconfigurazione.""" | ||
return await self.async_step_reconfigure() | ||
|
||
async def async_step_reconfigure(self, user_input=None): | ||
"""Gestisce la riconfigurazione dei parametri.""" | ||
"""Gestisce il flusso di riconfigurazione.""" | ||
if user_input is not None: | ||
# Aggiorna i parametri nel config entry | ||
self.hass.config_entries.async_update_entry( | ||
self.config_entry, | ||
options={**self.config_entry.options, **user_input} | ||
) | ||
return self.async_create_entry(title="", data=None) | ||
|
||
# Preleva i parametri attuali dalle opzioni o dai dati di configurazione iniziale | ||
current_brightness_on_switch = self.config_entry.options.get( | ||
CONF_BRIGHTNESS_ON_SWITCH, | ||
self.config_entry.data.get(CONF_BRIGHTNESS_ON_SWITCH, 100) | ||
) | ||
current_turn_off_physical = self.config_entry.options.get( | ||
CONF_TURN_OFF_PHYSICAL, | ||
self.config_entry.data.get(CONF_TURN_OFF_PHYSICAL, False) | ||
) | ||
# Aggiorna la configurazione esistente con i nuovi valori | ||
return self.async_create_entry(title=user_input["name"], data=user_input) | ||
|
||
# Schema per riconfigurare i due parametri | ||
schema = vol.Schema({ | ||
vol.Optional(CONF_BRIGHTNESS_ON_SWITCH, default=current_brightness_on_switch): vol.All(vol.Coerce(int), vol.Range(min=0, max=100)), | ||
vol.Optional(CONF_TURN_OFF_PHYSICAL, default=current_turn_off_physical): bool | ||
}) | ||
current_config = self._get_current_config() | ||
|
||
return self.async_show_form( | ||
step_id="reconfigure", | ||
data_schema=schema | ||
data_schema=vol.Schema({ | ||
vol.Required("physical_light", default=current_config.get("physical_light")): selector({"entity": {"domain": ["switch", "light"]}}), | ||
vol.Required("smart_light", default=current_config.get("smart_light")): selector({"entity": {"domain": "light"}}), | ||
vol.Required("name", default=current_config.get("name")): str, | ||
vol.Optional("brightness_on_switch", default=current_config.get("brightness_on_switch", 100)): vol.All(vol.Coerce(int), vol.Range(min=0, max=100)), | ||
vol.Optional("turn_off_physical", default=current_config.get("turn_off_physical", True)): bool | ||
}) | ||
) | ||
|
||
def _get_current_config(self): | ||
"""Ottiene la configurazione attuale per l'entità da modificare.""" | ||
entry = self.hass.config_entries.async_get_entry(self.context["entry_id"]) | ||
return entry.data if entry else {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,26 @@ | ||
{ | ||
"config": { | ||
"step": { | ||
"user": { | ||
"title": "Setup a new entity pair", | ||
"data": { | ||
"physical_light": "Select your phisical light entity (switch or light)", | ||
"smart_light": "Select your smart light (light)", | ||
"name": "New Light name" | ||
} | ||
} | ||
"config": { | ||
"step": { | ||
"user": { | ||
"title": "Configure a new light pair", | ||
"data": { | ||
"physical_light": "Select the physical light (switch or light)", | ||
"smart_light": "Select the smart light (light)", | ||
"name": "New light name", | ||
"brightness_on_switch": "Brightness percentage when physical switch is turned on (0-100)", | ||
"turn_off_physical": "Turn off physical switch when virtual light is turned off (yes or no)" | ||
} | ||
}, | ||
"reconfigure": { | ||
"title": "Reconfigure light pair", | ||
"data": { | ||
"physical_light": "Select the physical light (switch or light)", | ||
"smart_light": "Select the smart light (light)", | ||
"name": "Light name", | ||
"brightness_on_switch": "Brightness percentage when physical switch is turned on (0-100)", | ||
"turn_off_physical": "Turn off physical switch when virtual light is turned off (yes or no)" | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,26 @@ | ||
{ | ||
"config": { | ||
"step": { | ||
"user": { | ||
"title": "Configura l'abbinamento delle luci", | ||
"data": { | ||
"physical_light": "Seleziona la luce o interruttore fisico", | ||
"smart_light": "Seleziona la luce smart", | ||
"name": "Nome della nuova luce" | ||
} | ||
} | ||
"config": { | ||
"step": { | ||
"user": { | ||
"title": "Configura una nuova coppia di luci", | ||
"data": { | ||
"physical_light": "Seleziona la luce fisica (interruttore o luce)", | ||
"smart_light": "Seleziona la luce smart (luce)", | ||
"name": "Nome della nuova luce", | ||
"brightness_on_switch": "Percentuale di luminosità quando l'interruttore fisico è acceso (0-100)", | ||
"turn_off_physical": "Spegni l'interruttore fisico quando la luce virtuale viene spenta (sì o no)" | ||
} | ||
}, | ||
"reconfigure": { | ||
"title": "Riconfigura la coppia di luci", | ||
"data": { | ||
"physical_light": "Seleziona la luce fisica (interruttore o luce)", | ||
"smart_light": "Seleziona la luce smart (luce)", | ||
"name": "Nome della luce", | ||
"brightness_on_switch": "Percentuale di luminosità quando l'interruttore fisico è acceso (0-100)", | ||
"turn_off_physical": "Spegni l'interruttore fisico quando la luce virtuale viene spenta (sì o no)" | ||
} | ||
} | ||
} | ||
} | ||
} |