Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pr/1256 label d psdescriptions #1

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions custom_components/localtuya/cloud_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,18 @@ async def async_get_devices_list(self):
# _LOGGER.debug("DEV_LIST: %s", self.device_list)

return "ok"

async def async_get_device_specifications(self, device_id):
"""Obtain the DP ID mappings for a device."""
resp = await self.async_make_request(
"GET", url=f"/v1.1/devices/{device_id}/specifications"
)

if not resp.ok:
return {}, "Request failed, status " + str(resp.status)

r_json = resp.json()
if not r_json["success"]:
return {}, f"Error {r_json['code']}: {r_json['msg']}"

return r_json["result"], "ok"
25 changes: 22 additions & 3 deletions custom_components/localtuya/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,15 @@ def schema_defaults(schema, dps_list=None, **defaults):
return copy


def dps_string_list(dps_data):
def dps_string_list(dps_data, cloud_dp_codes):
"""Return list of friendly DPS values."""
return [f"{id} (value: {value})" for id, value in dps_data.items()]
strs = []
for dp, value in dps_data.items():
if dp in cloud_dp_codes:
strs.append(f"{dp} (code: {cloud_dp_codes[dp]}, value: {value})")
else:
strs.append(f"{dp} (value: {value})")
return strs


def gen_dps_strings():
Expand Down Expand Up @@ -299,7 +305,20 @@ async def validate_input(hass: core.HomeAssistant, data):

_LOGGER.debug("Total DPS: %s", detected_dps)

return dps_string_list(detected_dps)
# Get DP descriptions from the cloud, if the device is there.
cloud_dp_codes = {}
if data[CONF_DEVICE_ID] in hass.data[DOMAIN][DATA_CLOUD].device_list:
cloud_device_specs, res = await hass.data[DOMAIN][
DATA_CLOUD
].async_get_device_specifications(data[CONF_DEVICE_ID])
if res != "ok":
_LOGGER.error("Cloud DP specification request failed: %s", res)
else:
for category in ("functions", "status"):
cloud_dp_codes.update(
{str(e["dp_id"]): e["code"] for e in cloud_device_specs[category]}
)
return dps_string_list(detected_dps, cloud_dp_codes)


async def attempt_cloud_connection(hass, user_input):
Expand Down
Loading