Skip to content

Commit

Permalink
Fix integration errors list
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Apr 9, 2024
1 parent 2652194 commit a54bcb8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 56 deletions.
26 changes: 0 additions & 26 deletions custom_components/sonoff/core/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,3 @@
"staMac",
"timers",
)


def source_hash() -> str:
if source_hash.__doc__:
return source_hash.__doc__

try:
import hashlib
import os

m = hashlib.md5()
path = os.path.dirname(os.path.dirname(__file__))
for root, dirs, files in os.walk(path):
dirs.sort()
for file in sorted(files):
if not file.endswith(".py"):
continue
path = os.path.join(root, file)
with open(path, "rb") as f:
m.update(f.read())

source_hash.__doc__ = m.hexdigest()[:7]
return source_hash.__doc__

except Exception as e:
return f"{type(e).__name__}: {e}"
38 changes: 38 additions & 0 deletions custom_components/sonoff/core/xutils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from homeassistant.core import HomeAssistant


def source_hash() -> str:
if source_hash.__doc__:
return source_hash.__doc__

try:
import hashlib
import os

m = hashlib.md5()
path = os.path.dirname(os.path.dirname(__file__))
for root, dirs, files in os.walk(path):
dirs.sort()
for file in sorted(files):
if not file.endswith(".py"):
continue
path = os.path.join(root, file)
with open(path, "rb") as f:
m.update(f.read())

source_hash.__doc__ = m.hexdigest()[:7]
return source_hash.__doc__

except Exception as e:
return repr(e)


def system_log_records(hass: HomeAssistant, domain: str) -> list | str:
try:
return [
entry.to_dict()
for key, entry in hass.data["system_log"].records.items()
if domain in str(key)
]
except Exception as e:
return str(e)
54 changes: 24 additions & 30 deletions custom_components/sonoff/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntry

from .core.const import DOMAIN, PRIVATE_KEYS, source_hash
from .core.const import DOMAIN, PRIVATE_KEYS
from .core.ewelink import XRegistry
from .core import xutils


async def async_get_config_entry_diagnostics(hass: HomeAssistant, entry: ConfigEntry):
Expand All @@ -21,49 +22,42 @@ async def async_get_config_entry_diagnostics(hass: HomeAssistant, entry: ConfigE
else:
config = None
except Exception as e:
config = f"{type(e).__name__}: {e}"
config = repr(e)

options = {k: len(v) if k == "homes" else v for k, v in entry.options.items()}

registry: XRegistry = hass.data[DOMAIN][entry.entry_id]
try:
devices = {
did: {
"uiid": device["extra"]["uiid"],
"params": {
k: "***" if k in PRIVATE_KEYS else v
for k, v in device["params"].items()
},
"model": device.get("productModel"),
"online": device.get("online"),
"local": device.get("local"),
"localtype": device.get("localtype"),
"host": device.get("host"),
}
if "params" in device
else {
"localtype": device.get("localtype"),
}
did: (
{
"uiid": device["extra"]["uiid"],
"params": {
k: "***" if k in PRIVATE_KEYS else v
for k, v in device["params"].items()
},
"model": device.get("productModel"),
"online": device.get("online"),
"local": device.get("local"),
"localtype": device.get("localtype"),
"host": device.get("host"),
}
if "params" in device
else {
"localtype": device.get("localtype"),
}
)
for did, device in registry.devices.items()
}
except Exception as e:
devices = f"{type(e).__name__}: {e}"

try:
errors = [
entry.to_dict()
for key, entry in hass.data["system_log"].records.items()
if DOMAIN in key
]
except Exception as e:
errors = f"{type(e).__name__}: {e}"
devices = repr(e)

return {
"version": source_hash(),
"version": xutils.source_hash(),
"cloud_auth": registry.cloud.auth is not None,
"config": config,
"options": options,
"errors": errors,
"errors": xutils.system_log_records(hass, DOMAIN),
"devices": devices,
}

Expand Down

0 comments on commit a54bcb8

Please sign in to comment.