Skip to content

Commit

Permalink
Add checkbox to suggest platform configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
postlund committed Nov 6, 2020
1 parent 124b819 commit 205e3f6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
25 changes: 19 additions & 6 deletions custom_components/localtuya/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
_LOGGER = logging.getLogger(__name__)

PLATFORM_TO_ADD = "platform_to_add"
SUGGEST_DPS = "suggest_dps"
NO_ADDITIONAL_PLATFORMS = "no_additional_platforms"
DISCOVERED_DEVICE = "discovered_device"

Expand Down Expand Up @@ -65,7 +66,10 @@
)

PICK_ENTITY_SCHEMA = vol.Schema(
{vol.Required(PLATFORM_TO_ADD, default=PLATFORMS[0]): vol.In(PLATFORMS)}
{
vol.Required(PLATFORM_TO_ADD, default=PLATFORMS[0]): vol.In(PLATFORMS),
vol.Required(SUGGEST_DPS, default=True): bool,
}
)


Expand Down Expand Up @@ -107,7 +111,9 @@ def gen_dps_strings():
return [f"{dp} (value: ?)" for dp in range(1, 256)]


def platform_schema(platform, dps_strings, allow_id=True, yaml=False, dps_in_use=None):
def platform_schema(
platform, dps_strings, allow_id=True, yaml=False, dps_in_use=None, suggest_dps=False
):
"""Generate input validation schema for a platform."""
schema = {}
if yaml:
Expand All @@ -124,9 +130,11 @@ def platform_schema(platform, dps_strings, allow_id=True, yaml=False, dps_in_use
if yaml:
return vol.Schema(schema)

return schema_defaults(
vol.Schema(schema), dps_strings, **suggest(platform, dps_strings, dps_in_use)
)
suggestions = {}
if suggest_dps:
suggestions = suggest(platform, dps_strings, dps_in_use)

return schema_defaults(vol.Schema(schema), dps_strings, **suggestions)


def strip_dps_values(user_input, dps_strings):
Expand Down Expand Up @@ -203,6 +211,7 @@ def __init__(self):
self.basic_info = None
self.dps_strings = []
self.platform = None
self.suggest_dps = None
self.devices = {}
self.selected_device = None
self.entities = []
Expand Down Expand Up @@ -281,6 +290,7 @@ async def async_step_pick_entity_type(self, user_input=None):
)

self.platform = user_input[PLATFORM_TO_ADD]
self.suggest_dps = user_input[SUGGEST_DPS]
return await self.async_step_add_entity()

# Add a checkbox that allows bailing out from config flow iff at least one
Expand Down Expand Up @@ -310,7 +320,10 @@ async def async_step_add_entity(self, user_input=None):
return self.async_show_form(
step_id="add_entity",
data_schema=platform_schema(
self.platform, self.dps_strings, dps_in_use=self.async_dps_in_use()
self.platform,
self.dps_strings,
dps_in_use=self.async_dps_in_use(),
suggest_dps=self.suggest_dps,
),
errors=errors,
description_placeholders={"platform": self.platform},
Expand Down
1 change: 1 addition & 0 deletions custom_components/localtuya/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"description": "Please pick the type of entity you want to add.",
"data": {
"platform_to_add": "Platform",
"suggest_dps": "Suggest platform configuration",
"no_additional_platforms": "Do not add any more entities"
}
},
Expand Down

0 comments on commit 205e3f6

Please sign in to comment.