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

Fix local ctrl error #271

Merged
merged 8 commits into from
Dec 20, 2024
Merged
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
21 changes: 18 additions & 3 deletions custom_components/xiaomi_home/miot/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,19 @@
Common utilities.
"""
import json
from os import path
import random
from typing import Optional
import hashlib
from paho.mqtt.client import MQTTMatcher
import yaml

MIOT_ROOT_PATH: str = path.dirname(path.abspath(__file__))


def gen_absolute_path(relative_path: str) -> str:
"""Generate an absolute path."""
return path.join(MIOT_ROOT_PATH, relative_path)


def calc_group_id(uid: str, home_id: str) -> str:
Expand All @@ -64,6 +73,12 @@ def load_json_file(json_file: str) -> dict:
return json.load(f)


def load_yaml_file(yaml_file: str) -> dict:
"""Load a YAML file."""
with open(yaml_file, 'r', encoding='utf-8') as f:
return yaml.load(f, Loader=yaml.FullLoader)


def randomize_int(value: int, ratio: float) -> int:
"""Randomize an integer value."""
return int(value * (1 - ratio + random.random()*2*ratio))
Expand All @@ -74,12 +89,12 @@ class MIoTMatcher(MQTTMatcher):

def iter_all_nodes(self) -> any:
"""Return an iterator on all nodes with their paths and contents."""
def rec(node, path):
def rec(node, path_):
# pylint: disable=protected-access
if node._content:
yield ('/'.join(path), node._content)
yield ('/'.join(path_), node._content)
for part, child in node._children.items():
yield from rec(child, path + [part])
yield from rec(child, path_ + [part])
return rec(self._root, [])

def get(self, topic: str) -> Optional[any]:
Expand Down
Loading
Loading