diff --git a/custom_components/localtuya/core/ha_entities/__init__.py b/custom_components/localtuya/core/ha_entities/__init__.py index ff92e350..046b2526 100644 --- a/custom_components/localtuya/core/ha_entities/__init__.py +++ b/custom_components/localtuya/core/ha_entities/__init__.py @@ -96,7 +96,7 @@ def gen_localtuya_entities(localtuya_data: dict, tuya_category: str) -> list[dic localtuya_entity_configs = ent_data.entity_configs # Conditions contains_any: list[str] = ent_data.contains_any - local_entity = {} + entity = {} # used_dp = 0 for k, code in localtuya_conf.items(): @@ -114,21 +114,28 @@ def gen_localtuya_entities(localtuya_data: dict, tuya_category: str) -> list[dic for dp_data in detected_dps: dp_data: str = dp_data.lower() + # Same method we use in config_flow to get dp. + dp_id = dp_data.split(" ")[0] + + if k in entity: + # if the k already configured break the loop!. + _LOGGER.debug(f"{k} Already configured with: {entity[k]}.") + break if contains_any is not None: if not any(cond in dp_data for cond in contains_any): continue if code and code.lower() in dp_data.split(): - # Same method we use in config_flow to get dp. - local_entity[k] = dp_data.split(" ")[0] + _LOGGER.debug(f"Added!!!!: {dp_data}: code: {code}") + entity[k] = dp_id # Pull dp values from cloud. still unsure to apply this to all. # This is due to the fact that some local values may not same with the values provided from cloud. # For now, this is applied only to numbers values. for k, v in localtuya_entity_configs.items(): if isinstance(v, CLOUD_VALUE): - config_dp = local_entity.get(v.dp_config) + config_dp = entity.get(v.dp_config) dp_values = get_dp_values(config_dp, dps_data, v) or {} # special case for lights @@ -136,23 +143,23 @@ def gen_localtuya_entities(localtuya_data: dict, tuya_category: str) -> list[dic # value = dp_values.get(v.value_key) # dp_values[v.value_key] = convert_to_kelvin(value) - local_entity[k] = dp_values.get(v.value_key, v.default_value) + entity[k] = dp_values.get(v.value_key, v.default_value) else: - local_entity[k] = v + entity[k] = v - if local_entity: + if entity: # Entity most contains ID - if not local_entity.get(CONF_ID): + if not entity.get(CONF_ID): continue # Workaround to Prevent duplicated id. - if local_entity[CONF_ID] in entities: - _LOGGER.debug(f"{device_name}: Duplicated ID: {local_entity}") + if entity[CONF_ID] in entities: + _LOGGER.debug(f"{device_name}: Duplicated ID: {entity}") continue - local_entity.update(main_confs) - local_entity[CONF_PLATFORM] = platform - entities[local_entity.get(CONF_ID)] = local_entity - _LOGGER.debug(f"{device_name}: Entity configured: {local_entity}") + entity.update(main_confs) + entity[CONF_PLATFORM] = platform + entities[entity.get(CONF_ID)] = entity + _LOGGER.debug(f"{device_name}: Entity configured: {entity}") # sort entites by id sorted_ids = sorted(entities, key=int) @@ -197,13 +204,14 @@ def get_dp_values(dp: str, dps_data: dict, req_info: CLOUD_VALUE = None) -> dict if dp_values and dp_type == DPType.INTEGER: # We only need the scaling factor, other values will be scaled from via later on. # dp_values["min"] = scale(dp_values.get("min"), val_scale) - pref_type = req_info.prefer_type or int + valid_type = req_info.prefer_type and req_info.prefer_type in (str, float, int) + pref_type = req_info.prefer_type if valid_type else int val_scale = dp_values.get("scale", 1) dp_values["min"] = pref_type(dp_values.get("min")) dp_values["max"] = pref_type(dp_values.get("max")) dp_values["step"] = pref_type(dp_values.get("step")) - pref_type = req_info.prefer_type or float + pref_type = req_info.prefer_type if valid_type else float dp_values["scale"] = pref_type(scale(1, val_scale, float)) return dp_values