Skip to content

Commit

Permalink
Add zigbee info (dev)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Dec 1, 2020
1 parent d019273 commit 6959178
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 7 deletions.
77 changes: 71 additions & 6 deletions custom_components/xiaomi_gateway3/core/gateway3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
import socket
import time
from telnetlib import Telnet
from threading import Thread
from typing import Optional, Union

Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 10 additions & 1 deletion custom_components/xiaomi_gateway3/core/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions custom_components/xiaomi_gateway3/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 6959178

Please sign in to comment.