Skip to content

Commit

Permalink
Fetch software version and schema ID from Cloudcutter profile
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba2k2 committed Sep 7, 2023
1 parent 5d59669 commit c1b781e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
14 changes: 14 additions & 0 deletions ltctplugin/upk2esphome/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,18 @@ def cloudcutter_get_device(slug: str) -> dict:
f"Status code: {r.status_code}"
)
device = r.json()
for i, profile in enumerate(device["profiles"]):
device["profiles"][i] = cloudcutter_get_profile(profile["slug"])
return device


def cloudcutter_get_profile(slug: str) -> dict:
url = f"https://tuya-cloudcutter.github.io/api/profiles/{slug}.json"
with requests.get(url) as r:
if r.status_code != 200:
raise RuntimeError(
f"Couldn't download Cloudcutter profile '{slug}'.\n"
f"Status code: {r.status_code}"
)
profile = r.json()
return profile
30 changes: 25 additions & 5 deletions upk2esphome/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def build(data: dict, extras: dict) -> "ConfigData":
def is_tuya_mcu(self) -> bool:
match self.type:
case ConfigData.Type.CLOUDCUTTER:
slug = self.profile_slug
slug = self.profile_slug or ""
return slug.startswith("bk7231") and "common" in slug
case ConfigData.Type.STORAGE:
return "baud_cfg" in self.data or "uart_adapt_params" in self.data
Expand Down Expand Up @@ -63,12 +63,21 @@ def category(self) -> str | None:
return self.extras.get("category", {})

@property
def profile_slug(self) -> str:
def profile(self) -> dict | None:
profiles = self.data.get("profiles", [])
profile = profiles and profiles[0] or {}
if isinstance(profile, dict):
return profile
return None

@property
def profile_slug(self) -> str | None:
profiles = self.data.get("profiles", [])
profile = profiles and profiles[0] or {}
if isinstance(profile, str):
return profile.lower()
return profile.get("slug", "").lower()
slug = profile.get("slug", None)
return slug and slug.lower()

@property
def chip_name(
Expand All @@ -83,6 +92,13 @@ def chip_name(
return "BK7231N"
case _:
return "?"
profile = self.profile
if profile and "firmware" in profile:
chip = profile["firmware"].get("chip", None)
if chip == "BK7231T":
return "BK7231T"
if chip == "BK7231N":
return "BK7231N"
slug = self.profile_slug
if slug:
if "bk7231t" in slug:
Expand All @@ -100,7 +116,11 @@ def chip_name(

@property
def schema_id(self) -> str | None:
return self.data.get("gw_di", {}).get("s_id", None)
match self.type:
case ConfigData.Type.CLOUDCUTTER:
return list(self.data.get("schemas", {}) or [None])[0]
case ConfigData.Type.STORAGE:
return self.data.get("gw_di", {}).get("s_id", None)

@property
def data_device(self) -> dict | None:
Expand All @@ -111,7 +131,7 @@ def data_device(self) -> dict | None:
firmwareKey=key if key.startswith("key") else None,
productKey=key if not key.startswith("key") else None,
factoryPin=None,
softwareVer=None,
softwareVer=self.profile.get("firmware", {}).get("version", None),
)
case ConfigData.Type.STORAGE:
gw_di = self.data.get("gw_di", {})
Expand Down

0 comments on commit c1b781e

Please sign in to comment.