Skip to content

Commit

Permalink
fixing defaults and optionals on selectors for config flow (#237)
Browse files Browse the repository at this point in the history
* fixing defaults and optionals on selectors for config flow

* fixing defaults and optionals on selectors for config flow

* removing commented and debug code

* removed duplicate NullableEntitySelector class code block

* fixing defaults not showing up if you had a config previously

* fixing defaults not showing up if you had a config previously
  • Loading branch information
jseidl committed Jul 20, 2022
1 parent d167492 commit e170693
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 26 deletions.
4 changes: 3 additions & 1 deletion custom_components/magic_areas/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,9 @@ async def load_entities(self) -> None:
continue

# Skip excluded entities
if entity["entity_id"] in self.config.get(CONF_EXCLUDE_ENTITIES):
if entity["entity_id"] in self.config.get(
CONF_EXCLUDE_ENTITIES
):
continue

entity_list.append(entity["entity_id"])
Expand Down
63 changes: 38 additions & 25 deletions custom_components/magic_areas/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
from homeassistant.components.media_player import DOMAIN as MEDIA_PLAYER_DOMAIN
from homeassistant.const import CONF_NAME
from homeassistant.core import callback
from homeassistant.helpers.selector import selector
from homeassistant.helpers.selector import (
EntitySelector,
EntitySelectorConfig,
selector,
)

from custom_components.magic_areas.const import (
ALL_BINARY_SENSOR_DEVICE_CLASSES,
Expand Down Expand Up @@ -93,6 +97,18 @@
EMPTY_ENTRY = [""]


class NullableEntitySelector(EntitySelector):
def __call__(self, data):
"""Validate the passed selection, if passed."""

_LOGGER.warn(f"Null data {data}")

if not data:
return data

return super().__call__(data)


class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Magic Areas."""

Expand Down Expand Up @@ -148,15 +164,12 @@ def _build_selector_select(self, options=[], multiple=False):
)

def _build_selector_entity_simple(
self, options=None, multiple=False, force_include=False
self, options=[], multiple=False, force_include=False
):

selector_opts = {"entity": {"multiple": multiple}}

if options is not None:
selector_opts["entity"]["include_entities"] = options

return selector(selector_opts)
return NullableEntitySelector(
EntitySelectorConfig(include_entities=options, multiple=multiple)
)

def _build_selector_number(
self, min=0, max=9999, mode="box", unit_of_measurement="seconds"
Expand Down Expand Up @@ -191,17 +204,18 @@ def _build_options_schema(
schema = {
vol.Optional(
name,
description={"suggested_value": saved_options.get(name)},
description={
"suggested_value": saved_options.get(name)
if saved_options.get(name)
else default
},
default=default,
): dynamic_validators.get(name, validation)
): selectors[name]
if name in selectors.keys()
else dynamic_validators.get(name, validation)
for name, default, validation in options
}

# Apply selector overrides
if selectors:
for conf_key, selector_value in selectors.items():
schema[conf_key] = selector_value

_LOGGER.debug(f"Built schema: {schema}")

if raw:
Expand Down Expand Up @@ -301,7 +315,9 @@ async def async_step_area_config(self, user_input=None):
CONF_TYPE: self._build_selector_select(
sorted([AREA_TYPE_INTERIOR, AREA_TYPE_EXTERIOR])
),
CONF_INCLUDE_ENTITIES: self._build_selector_entity_simple(multiple=True),
CONF_INCLUDE_ENTITIES: self._build_selector_entity_simple(
self.all_entities, multiple=True
),
CONF_EXCLUDE_ENTITIES: self._build_selector_entity_simple(
self.all_area_entities, multiple=True
),
Expand Down Expand Up @@ -368,17 +384,14 @@ async def async_step_secondary_states(self, user_input=None):
CONF_ACCENT_ENTITY: vol.In(EMPTY_ENTRY + self.all_entities),
},
selectors={
# CONF_DARK_ENTITY: self._build_selector_entity_simple(),
# CONF_SLEEP_ENTITY: self._build_selector_entity_simple(),
# CONF_ACCENT_ENTITY: self._build_selector_entity_simple(),
CONF_DARK_ENTITY: self._build_selector_select(
EMPTY_ENTRY + self.all_entities
CONF_DARK_ENTITY: self._build_selector_entity_simple(
self.all_entities
),
CONF_SLEEP_ENTITY: self._build_selector_select(
EMPTY_ENTRY + self.all_entities
CONF_SLEEP_ENTITY: self._build_selector_entity_simple(
self.all_entities
),
CONF_ACCENT_ENTITY: self._build_selector_select(
EMPTY_ENTRY + self.all_entities
CONF_ACCENT_ENTITY: self._build_selector_entity_simple(
self.all_entities
),
CONF_SLEEP_TIMEOUT: self._build_selector_number(),
CONF_EXTENDED_TIME: self._build_selector_number(),
Expand Down

0 comments on commit e170693

Please sign in to comment.