Skip to content

Commit

Permalink
Merge dps_data on cloud to auto configure
Browse files Browse the repository at this point in the history
  • Loading branch information
xZetsubou authored Nov 10, 2023
1 parent c7dbb6f commit de0e888
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 2 additions & 0 deletions custom_components/localtuya/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,8 @@ def unique_id(self) -> str:
@property
def available(self) -> bool:
"""Return if device is available or not."""
if (platform := self._config.get(CONF_PLATFORM)) and platform == "button":
return True
return str(self._dp_id) in self._status

@property
Expand Down
16 changes: 12 additions & 4 deletions custom_components/localtuya/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,22 @@ def schema_defaults(schema, dps_list=None, **defaults):
return copy


def dps_string_list(dps_data, cloud_dp_codes):
def dps_string_list(dps_data: dict[str, dict], cloud_dp_codes: dict[str, dict]) -> list:
"""Return list of friendly DPS values."""
strs = []

# Merge DPS found on cloud with local dps strings.
# for dp, func in cloud_dp_codes.items():
# if dp not in dps_data:
# dps_data[dp] = f"{func.get('value')}, cloud pull"

for dp, value in dps_data.items():
if (dp_data := cloud_dp_codes.get(dp)) and (code := dp_data.get("code")):
strs.append(f"{dp} ( code: {code} , value: {value} )")
else:
strs.append(f"{dp} ( value: {value} )")
return strs

return sorted(strs, key=lambda i: int(i.split()[0]))


def gen_dps_strings():
Expand Down Expand Up @@ -892,13 +899,14 @@ async def async_step_auto_configure_device(self, user_input=None):
dev_id = self.selected_device
category = None
node_id = self.nodeID
device_data = dev_id in self.cloud_data.device_list
device_data = self.cloud_data.device_list.get(dev_id)
if device_data:
category = self.cloud_data.device_list[dev_id].get("category", "")

localtuya_data = {
CONF_FRIENDLY_NAME: self.device_data.get(CONF_FRIENDLY_NAME),
"device_cloud_data": device_data,
CONF_DPS_STRINGS: self.dps_strings,
CONF_FRIENDLY_NAME: self.device_data.get(CONF_FRIENDLY_NAME),
}

dev_data = generate_tuya_device(localtuya_data, category)
Expand Down
3 changes: 3 additions & 0 deletions custom_components/localtuya/core/tuya_devices/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ def generate_tuya_device(localtuya_data: dict, tuya_category: str) -> dict | lis

if not tuya_category or not detected_dps:
return
if dps_data := device_cloud_data.get("dps_data"):
detected_dps = merge_local_cloud_dps(detected_dps, dps_data)

entities = {}

for platform, tuya_data in DATA_PLATFORMS.items():
Expand Down

0 comments on commit de0e888

Please sign in to comment.