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 17, 2020
1 parent e328347 commit e33c410
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 @@ -108,7 +108,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 @@ -126,7 +126,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 @@ -208,6 +208,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 the initial step."""
errors = {}
Expand Down Expand Up @@ -318,7 +322,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 e33c410

Please sign in to comment.