Skip to content

Commit

Permalink
Do not suggest datapoint id used by other entities
Browse files Browse the repository at this point in the history
  • Loading branch information
postlund committed Nov 6, 2020
1 parent 597826f commit 124b819
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
12 changes: 9 additions & 3 deletions custom_components/localtuya/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ 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):
def platform_schema(platform, dps_strings, allow_id=True, yaml=False, dps_in_use=None):
"""Generate input validation schema for a platform."""
schema = {}
if yaml:
Expand All @@ -125,7 +125,7 @@ def platform_schema(platform, dps_strings, allow_id=True, yaml=False):
return vol.Schema(schema)

return schema_defaults(
vol.Schema(schema), dps_strings, **suggest(platform, dps_strings)
vol.Schema(schema), dps_strings, **suggest(platform, dps_strings, dps_in_use)
)


Expand Down Expand Up @@ -207,6 +207,10 @@ def __init__(self):
self.selected_device = None
self.entities = []

def async_dps_in_use(self):
"""Return datapoints used as ID for entities."""
return [entity[CONF_ID] for entity in self.entities]

async def async_step_user(self, user_input=None):
"""Handle initial step."""
errors = {}
Expand Down Expand Up @@ -305,7 +309,9 @@ 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),
data_schema=platform_schema(
self.platform, self.dps_strings, dps_in_use=self.async_dps_in_use()
),
errors=errors,
description_placeholders={"platform": self.platform},
)
Expand Down
12 changes: 9 additions & 3 deletions custom_components/localtuya/suggestions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""Module used to suggest datapoints for a platform."""
from homeassistant.const import CONF_ID

from importlib import import_module


def _suggest_defaults(suggestions, dps_strings):
def _suggest_defaults(suggestions, dps_strings, dps_in_use):
"""Return datapoint suggestions for options."""

def _match(suggestion):
Expand All @@ -14,18 +16,22 @@ def _match(suggestion):
output = {}
for conf, conf_suggestion in suggestions.items():
for suggestion in conf_suggestion:
# Don't suggest an ID that is already in use
if conf == CONF_ID and suggestion in dps_in_use:
continue

match = _match(suggestion)
if match:
output[conf] = match
break
return output


def suggest(platform, dps_strings):
def suggest(platform, dps_strings, dps_in_use=None):
"""Suggest datapoints for a platform."""
integration_module = ".".join(__name__.split(".")[:-1])
module = import_module("." + platform, integration_module)

if hasattr(module, "DP_SUGGESTIONS"):
return _suggest_defaults(module.DP_SUGGESTIONS, dps_strings)
return _suggest_defaults(module.DP_SUGGESTIONS, dps_strings, dps_in_use or [])
return {}

0 comments on commit 124b819

Please sign in to comment.