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 17, 2020
1 parent e33c410 commit 13d1649
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 @@ -31,6 +31,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 @@ -66,7 +67,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 @@ -108,7 +112,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 @@ -125,9 +131,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 @@ -204,6 +212,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 @@ -294,6 +303,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 @@ -323,7 +333,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 @@ -35,6 +35,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 13d1649

Please sign in to comment.