-
Notifications
You must be signed in to change notification settings - Fork 17
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
6 changed files
with
209 additions
and
19 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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant | ||
|
||
DOMAIN = 'gyverlamp' | ||
|
||
|
||
async def async_setup(hass, hass_config): | ||
# used only with GUI setup | ||
hass.data[DOMAIN] = {} | ||
return True | ||
|
||
|
||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): | ||
# migrate data (after first setup) to options | ||
if entry.data: | ||
hass.config_entries.async_update_entry(entry, data={}, | ||
options=entry.data) | ||
|
||
# add options handler | ||
entry.add_update_listener(async_update_options) | ||
|
||
# forward to light setup | ||
coro = hass.config_entries.async_forward_entry_setup(entry, 'light') | ||
hass.async_create_task(coro) | ||
|
||
return True | ||
|
||
|
||
async def async_update_options(hass: HomeAssistant, entry: ConfigEntry): | ||
# update entity config | ||
hass.data[DOMAIN][entry.entry_id].update_config(entry.options) | ||
|
||
|
||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): | ||
return await hass.config_entries.async_forward_entry_unload(entry, 'light') |
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import re | ||
|
||
import homeassistant.helpers.config_validation as cv | ||
import voluptuous as vol | ||
from homeassistant.config_entries import ConfigFlow, OptionsFlow, ConfigEntry | ||
from homeassistant.const import CONF_HOST | ||
from homeassistant.core import callback | ||
|
||
from . import DOMAIN | ||
from .light import CONF_EFFECTS, EFFECTS | ||
|
||
|
||
def parse_effects(data: str) -> list: | ||
return re.split(r'\s*,\s*', data.strip()) | ||
|
||
|
||
class ConfigFlowHandler(ConfigFlow, domain=DOMAIN): | ||
VERSION = 1 | ||
|
||
async def async_step_user(self, user_input=None): | ||
if user_input is not None: | ||
host = user_input[CONF_HOST] | ||
user_input[CONF_EFFECTS] = parse_effects(user_input[CONF_EFFECTS]) | ||
return self.async_create_entry(title=host, data=user_input) | ||
|
||
effects = ','.join(EFFECTS) | ||
return self.async_show_form( | ||
step_id='user', | ||
data_schema=vol.Schema({ | ||
vol.Required(CONF_HOST): cv.string, | ||
vol.Optional(CONF_EFFECTS, default=effects): cv.string | ||
}) | ||
) | ||
|
||
@staticmethod | ||
@callback | ||
def async_get_options_flow(config_entry): | ||
return OptionsFlowHandler(config_entry) | ||
|
||
|
||
class OptionsFlowHandler(OptionsFlow): | ||
def __init__(self, config_entry: ConfigEntry): | ||
self.config_entry = config_entry | ||
|
||
async def async_step_init(self, user_input=None): | ||
host = self.config_entry.options[CONF_HOST] | ||
effects = ','.join(self.config_entry.options[CONF_EFFECTS]) | ||
return self.async_show_form( | ||
step_id='user', | ||
data_schema=vol.Schema({ | ||
vol.Required(CONF_HOST, default=host): cv.string, | ||
vol.Optional(CONF_EFFECTS, default=effects): cv.string | ||
}) | ||
) | ||
|
||
async def async_step_user(self, user_input: dict = None): | ||
user_input[CONF_EFFECTS] = parse_effects(user_input[CONF_EFFECTS]) | ||
return self.async_create_entry(title='', data=user_input) |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"config": { | ||
"step": { | ||
"user": { | ||
"data": { | ||
"host": "Host", | ||
"effects": "Effects" | ||
} | ||
} | ||
} | ||
}, | ||
"options": { | ||
"step": { | ||
"user": { | ||
"data": { | ||
"host": "Host", | ||
"effects": "Effects" | ||
} | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"config": { | ||
"step": { | ||
"user": { | ||
"data": { | ||
"host": "Хост", | ||
"effects": "Эффекты" | ||
} | ||
} | ||
} | ||
}, | ||
"options": { | ||
"step": { | ||
"user": { | ||
"data": { | ||
"host": "Хост", | ||
"effects": "Эффекты" | ||
} | ||
} | ||
} | ||
} | ||
} |