From 69591788b043055dcf7858733857007c7ce0a706 Mon Sep 17 00:00:00 2001 From: Alexey Khit Date: Tue, 1 Dec 2020 23:22:41 +0300 Subject: [PATCH] Add zigbee info (dev) --- .../xiaomi_gateway3/core/gateway3.py | 77 +++++++++++++++++-- .../xiaomi_gateway3/core/shell.py | 11 ++- custom_components/xiaomi_gateway3/remote.py | 3 + 3 files changed, 84 insertions(+), 7 deletions(-) diff --git a/custom_components/xiaomi_gateway3/core/gateway3.py b/custom_components/xiaomi_gateway3/core/gateway3.py index ad9ced04..b3a79b74 100644 --- a/custom_components/xiaomi_gateway3/core/gateway3.py +++ b/custom_components/xiaomi_gateway3/core/gateway3.py @@ -3,6 +3,7 @@ import re import socket import time +from telnetlib import Telnet from threading import Thread from typing import Optional, Union @@ -63,6 +64,7 @@ def __init__(self, host: str, token: str, config: dict, ble: bool = True, self._debug = config['debug'] if 'debug' in config else '' self._disable_buzzer = config.get('buzzer') is False + self._config = config self.default_devices = config['devices'] self.devices = {} @@ -176,12 +178,15 @@ def _prepeare_gateway(self, with_devices: bool = False): self.debug("Stop socat") shell.stop_socat() - if "Lumi_Z3GatewayHost_MQTT" not in ps: - self.debug("Run Lumi Zigbee") - shell.run_lumi_zigbee() - # elif "Lumi_Z3GatewayHost_MQTT -n 1 -b 115200 -v" not in ps: - # self.debug("Run public Zigbee console") - # shell.run_public_zb_console() + if 'zigbee_console' in self._config: + if "Lumi_Z3GatewayHost_MQTT -n 1 -b 115200 -v" not in ps: + self.debug("Run public Zigbee console") + shell.run_public_zb_console() + + else: + if "Lumi_Z3GatewayHost_MQTT" not in ps: + self.debug("Run Lumi Zigbee") + shell.run_lumi_zigbee() if with_devices: self.debug("Get devices") @@ -765,6 +770,66 @@ def get_device(self, mac: str) -> Optional[dict]: return device return None + def get_gateway_info(self): + telnet = Telnet(self.host, 4901) + telnet.read_until(b'Lumi_Z3GatewayHost') + + telnet.write(b"option print-rx-msgs disable\r\n") + telnet.read_until(b'Lumi_Z3GatewayHost') + + telnet.write(b"plugin device-table print\r\n") + raw = telnet.read_until(b'Lumi_Z3GatewayHost').decode() + m1 = re.findall(r'\d+ ([A-F0-9]{4}): ([A-F0-9]{16}) 0 JOINED (\d+)', + raw) + + telnet.write(b"plugin stack-diagnostics child-table\r\n") + raw = telnet.read_until(b'Lumi_Z3GatewayHost').decode() + m2 = re.findall(r'\(>\)([A-F0-9]{16})', raw) + + telnet.write(b"plugin stack-diagnostics neighbor-table\r\n") + raw = telnet.read_until(b'Lumi_Z3GatewayHost').decode() + m3 = re.findall(r'\(>\)([A-F0-9]{16})', raw) + + telnet.write(b"plugin concentrator print-table\r\n") + raw = telnet.read_until(b'Lumi_Z3GatewayHost').decode() + m4 = re.findall(r': (.{16,}) -> 0x0000', raw) + m4 = [i.replace('0x', '').split(' -> ') for i in m4] + m4 = {i[0]: i[1:] for i in m4} + + md = ('nwk|eid64|ago|type|parent|name\n' + '---|-----|---|----|------|----') + + for i in m1: + nid = i[0] + eid64 = i[1] + last_seen = i[2] + if eid64 in m2: + type_ = 'device' + elif eid64 in m3: + type_ = 'router' + else: + type_ = '-' + + parent = m4[nid][0] if nid in m4 else '-' + + did = 'lumi.' + re.sub(r'^0*', '', eid64).lower() + device = self.devices.get(did) + name = device['device_name'] if device else '-' + + try: + md += '\n' + '|'.join([ + nid, + eid64, + last_seen, + type_, + parent, + name + ]) + except: + print() + + return md + def is_gw3(host: str, token: str) -> Optional[str]: try: diff --git a/custom_components/xiaomi_gateway3/core/shell.py b/custom_components/xiaomi_gateway3/core/shell.py index 7b74a5b2..c62ea5da 100644 --- a/custom_components/xiaomi_gateway3/core/shell.py +++ b/custom_components/xiaomi_gateway3/core/shell.py @@ -91,8 +91,17 @@ def redirect_miio2mqtt(self, pattern: str, new_version=False): def run_public_zb_console(self): self.exec("killall daemon_app.sh; killall Lumi_Z3GatewayHost_MQTT") + # run Gateway with open console port self.exec("Lumi_Z3GatewayHost_MQTT -n 1 -b 115200 -v -p '/dev/ttyS2' " - "-d '/data/silicon_zigbee_host/' &") + "-d '/data/silicon_zigbee_host/' > /dev/null &") + + # connect to console to start zigbee chip + self.write(b"nc localhost 4901\r\n") + time.sleep(1) + # exit console (ctrl+c) + self.write(b"\x03") + self.read_until(b"\r\n# ") + self.exec("daemon_app.sh &") def read_file(self, filename: str, as_base64=False): diff --git a/custom_components/xiaomi_gateway3/remote.py b/custom_components/xiaomi_gateway3/remote.py index dc7ac95c..531f0aac 100644 --- a/custom_components/xiaomi_gateway3/remote.py +++ b/custom_components/xiaomi_gateway3/remote.py @@ -103,3 +103,6 @@ async def async_send_command(self, command, **kwargs): self.gw.send(self.device, {'channel': int(args[1])}) elif cmd == 'publishstate': self.gw.send_mqtt('publishstate') + elif cmd == 'info': + raw = self.gw.get_gateway_info() + persistent_notification.async_create(self.hass, raw)