Skip to content

Commit 0014f31

Browse files
author
niceboy
committed
performance improve
1 parent f0d4da0 commit 0014f31

File tree

9 files changed

+95
-79
lines changed

9 files changed

+95
-79
lines changed

custom_components/aqara_gateway/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
4545
# migrate data (also after first setup) to options
4646
if entry.data:
4747
hass.config_entries.async_update_entry(entry, data={},
48-
options=entry.data)
48+
options=entry.data)
4949

5050
await _setup_logger(hass)
5151

@@ -194,7 +194,7 @@ def __init__(self, gateway: Gateway, device: dict, attr: str):
194194

195195
self._unique_id = f"{self.device['mac']}_{self._attr}"
196196
self._name = (self.device['device_name'] + ' ' +
197-
self._attr.replace('_', ' ').title())
197+
self._attr.replace('_', ' ').title())
198198

199199
self.entity_id = f"{DOMAIN}.{self._unique_id}"
200200

custom_components/aqara_gateway/binary_sensor.py

100644100755
File mode changed.

custom_components/aqara_gateway/config_flow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ async def async_step_discovery_confirm(self, user_input=None):
153153
return self.async_show_form(
154154
step_id="discovery_confirm",
155155
description_placeholders={"name": self._name,
156-
"device_info": self._device_info}
156+
"device_info": self._device_info}
157157
)
158158

159159
async def async_step_zeroconf(self, discovery_info: DiscoveryInfoType):

custom_components/aqara_gateway/core/const.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,18 @@
119119
POWER = "power"
120120
VOLTAGE = "voltage"
121121

122-
DOMAINS = ['air_quality',
123-
'alarm_control_panel',
124-
'binary_sensor',
125-
'climate',
126-
'cover',
127-
'light',
128-
'remote',
129-
'select',
130-
'sensor',
131-
'switch']
122+
DOMAINS = [
123+
'air_quality',
124+
'alarm_control_panel',
125+
'binary_sensor',
126+
'climate',
127+
'cover',
128+
'light',
129+
'remote',
130+
'select',
131+
'sensor',
132+
'switch'
133+
]
132134

133135
UNITS = {
134136
SensorDeviceClass.BATTERY: PERCENTAGE,

custom_components/aqara_gateway/core/gateway.py

Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing import Optional
1010
from random import randint
1111
from paho.mqtt.client import Client, MQTTMessage
12+
from datetime import datetime
1213

1314
from homeassistant.config_entries import ConfigEntry
1415
from homeassistant.core import Event, HomeAssistant
@@ -57,7 +58,7 @@ def __init__(self, hass: HomeAssistant, host: str, config: dict, **options):
5758

5859
self._debug = options.get('debug', '') # for fast access
5960
self.parent_scan_interval = (-1 if options.get('parent') is None
60-
else options['parent'])
61+
else options['parent'])
6162
self.default_devices = config['devices'] if config else None
6263

6364
self.devices = {}
@@ -165,7 +166,7 @@ async def async_run(self):
165166
if not self._mqtt_connect() or not self._prepare_gateway():
166167
if self.host in self.hass.data[DOMAIN]["mqtt"]:
167168
self.hass.data[DOMAIN]["mqtt"].remove(self.host)
168-
await asyncio.sleep(60)
169+
await asyncio.sleep(10)
169170
continue
170171

171172
self._mqttc.loop_start()
@@ -199,36 +200,36 @@ def _prepare_gateway(self, get_devices: bool = False):
199200
device_name = Utils.get_device_name(self._model).lower()
200201
if "g2h pro" in device_name:
201202
shell = TelnetShellG2HPro(self.host,
202-
self.options.get(CONF_PASSWORD, ''))
203+
self.options.get(CONF_PASSWORD, ''))
203204
elif "g2h" in device_name:
204205
shell = TelnetShellG2H(self.host,
205-
self.options.get(CONF_PASSWORD, ''))
206+
self.options.get(CONF_PASSWORD, ''))
206207
elif "e1" in device_name:
207208
shell = TelnetShellE1(self.host,
208-
self.options.get(CONF_PASSWORD, ''))
209+
self.options.get(CONF_PASSWORD, ''))
209210
elif "g3" in device_name:
210211
shell = TelnetShellG3(self.host,
211-
self.options.get(CONF_PASSWORD, ''))
212+
self.options.get(CONF_PASSWORD, ''))
212213
elif "m2 2022" in device_name:
213214
shell = TelnetShellM2POE(self.host,
214-
self.options.get(CONF_PASSWORD, ''))
215+
self.options.get(CONF_PASSWORD, ''))
215216
elif "m1s 2022" in device_name:
216217
shell = TelnetShellM1S22(self.host,
217-
self.options.get(CONF_PASSWORD, ''))
218+
self.options.get(CONF_PASSWORD, ''))
218219
elif "m3" in device_name:
219220
shell = TelnetShellM3(self.host,
220-
self.options.get(CONF_PASSWORD, ''))
221+
self.options.get(CONF_PASSWORD, ''))
221222
else:
222223
shell = TelnetShell(self.host,
223-
self.options.get(CONF_PASSWORD, ''))
224+
self.options.get(CONF_PASSWORD, ''))
224225
shell.login()
225-
processes = shell.get_running_ps()
226+
processes = shell.get_running_ps("mosquitto")
226227
public_mosquitto = shell.check_public_mosquitto()
227228
if not public_mosquitto:
228229
self.debug("mosquitto is not running as public!")
229230

230-
if "/data/bin/mosquitto -d" not in processes:
231-
if "mosquitto" not in processes or not public_mosquitto:
231+
if "mosquitto" not in processes or not public_mosquitto:
232+
if "/data/bin/mosquitto -d" not in processes:
232233
shell.run_public_mosquitto()
233234

234235
if get_devices:
@@ -251,21 +252,27 @@ def _get_devices(self, shell):
251252
try:
252253
# 1. Read coordinator info
253254
value = {}
254-
255-
zb_coordinator = shell.get_prop("sys.zb_coordinator")
256-
model = shell.get_prop("persist.sys.model")
255+
prop_raw = shell.get_prop("")
256+
data = re.search(r"\[sys\.zb_coordinator\\]: \[([a-zA-Z0-9.-]+)\]", prop_raw)
257+
zb_coordinator = data.group(1) if data else shell.get_prop("sys.zb_coordinator")
258+
data = re.search(r"\[persist\.sys\.model\]: \[([a-zA-Z0-9.-]+)\]", prop_raw)
259+
model = data.group(1) if data else shell.get_prop("persist.sys.model")
257260
if len(zb_coordinator) >= 1:
258261
raw = shell.read_file(zb_coordinator, with_newline=False)
259-
did = shell.get_prop("persist.sys.did")
260-
model = shell.get_prop("ro.sys.model")
262+
data = re.search(r"\[persist\.sys\.did\]: \[([a-zA-Z0-9.-]+)\]", prop_raw)
263+
did = data.group(1) if data else shell.get_prop("persist.sys.did")
264+
data = re.search(r"\[persist\.sys\.model\]: \[([a-zA-Z0-9.-]+)\]", prop_raw)
265+
model = data.group(1) if data else shell.get_prop("ro.sys.model")
261266
elif any(name in model for name in [
262267
'lumi.gateway', 'lumi.aircondition',
263268
'lumi.camera.gwpagl01', 'lumi.camera.agl001',
264269
'lumi.camera.acn003']):
265270
raw = shell.read_file(
266271
'/data/zigbee/coordinator.info', with_newline=False)
267-
did = shell.get_prop("persist.sys.did")
268-
model = shell.get_prop("ro.sys.model")
272+
data = re.search(r"\[persist\.sys\.did\]: \[([a-zA-Z0-9.-]+)\]", prop_raw)
273+
did = data.group(1) if data else shell.get_prop("persist.sys.did")
274+
data = re.search(r"\[ro\.sys\.model\]: \[([a-zA-Z0-9.-]+)\]", prop_raw)
275+
model = data.group(1) if data else shell.get_prop("ro.sys.model")
269276
else:
270277
raw = str(shell.read_file('/mnt/config/miio/device.conf'))
271278
if len(raw) <= 1:
@@ -291,7 +298,8 @@ def _get_devices(self, shell):
291298
self._model = model
292299

293300
# zigbee devices
294-
zb_device = shell.get_prop("sys.zb_device")
301+
data = re.search(r"\[sys\.zb_device\]: \[([a-zA-Z0-9.-]+)\]", prop_raw)
302+
zb_device = data.group(1) if data else shell.get_prop("sys.zb_device")
295303
if len(zb_device) >= 1:
296304
raw = shell.read_file(zb_device, with_newline=False)
297305
else:
@@ -429,25 +437,25 @@ def process_gateway_stats(self, payload: dict = None):
429437
device_name = Utils.get_device_name(self._model).lower()
430438
if "g2h pro" in device_name:
431439
shell = TelnetShellG2HPro(self.host,
432-
self.options.get(CONF_PASSWORD, ''))
440+
self.options.get(CONF_PASSWORD, ''))
433441
elif "g2h" in device_name:
434442
shell = TelnetShellG2H(self.host,
435-
self.options.get(CONF_PASSWORD, ''))
443+
self.options.get(CONF_PASSWORD, ''))
436444
elif "e1" in device_name:
437445
shell = TelnetShellE1(self.host,
438-
self.options.get(CONF_PASSWORD, ''))
446+
self.options.get(CONF_PASSWORD, ''))
439447
elif "g3" in device_name:
440448
shell = TelnetShellG3(self.host,
441-
self.options.get(CONF_PASSWORD, ''))
449+
self.options.get(CONF_PASSWORD, ''))
442450
elif "m2 2022" in device_name:
443451
shell = TelnetShellM2POE(self.host,
444-
self.options.get(CONF_PASSWORD, ''))
452+
self.options.get(CONF_PASSWORD, ''))
445453
elif "m1s 2022" in device_name:
446454
shell = TelnetShellM1S22(self.host,
447-
self.options.get(CONF_PASSWORD, ''))
455+
self.options.get(CONF_PASSWORD, ''))
448456
elif "m3" in device_name:
449457
shell = TelnetShellM3(self.host,
450-
self.options.get(CONF_PASSWORD, ''))
458+
self.options.get(CONF_PASSWORD, ''))
451459
else:
452460
shell = TelnetShell(self.host,
453461
self.options.get(CONF_PASSWORD, ''))
@@ -531,25 +539,25 @@ def _process_devices_info(self, prop, value):
531539
device_name = Utils.get_device_name(self._model).lower()
532540
if "g2h pro" in device_name:
533541
shell = TelnetShellG2HPro(self.host,
534-
self.options.get(CONF_PASSWORD, ''))
542+
self.options.get(CONF_PASSWORD, ''))
535543
elif "g2h" in device_name:
536544
shell = TelnetShellG2H(self.host,
537-
self.options.get(CONF_PASSWORD, ''))
545+
self.options.get(CONF_PASSWORD, ''))
538546
elif "e1" in device_name:
539547
shell = TelnetShellE1(self.host,
540-
self.options.get(CONF_PASSWORD, ''))
548+
self.options.get(CONF_PASSWORD, ''))
541549
elif "g3" in device_name:
542550
shell = TelnetShellG3(self.host,
543-
self.options.get(CONF_PASSWORD, ''))
551+
self.options.get(CONF_PASSWORD, ''))
544552
elif "m2 2022" in device_name:
545553
shell = TelnetShellM2POE(self.host,
546-
self.options.get(CONF_PASSWORD, ''))
554+
self.options.get(CONF_PASSWORD, ''))
547555
elif "m1s 2022" in device_name:
548556
shell = TelnetShellM1S22(self.host,
549-
self.options.get(CONF_PASSWORD, ''))
557+
self.options.get(CONF_PASSWORD, ''))
550558
elif "m3" in device_name:
551559
shell = TelnetShellM3(self.host,
552-
self.options.get(CONF_PASSWORD, ''))
560+
self.options.get(CONF_PASSWORD, ''))
553561
else:
554562
shell = TelnetShell(self.host,
555563
self.options.get(CONF_PASSWORD, ''))
@@ -895,9 +903,13 @@ def is_aqaragateway(host: str,
895903
if device_name and 'g2h pro' in device_name:
896904
shell = TelnetShellG2HPro(host, password)
897905
shell.login()
898-
model = shell.get_prop("ro.sys.model")
899-
name = shell.get_prop("ro.sys.name")
900-
mac = shell.get_prop("persist.sys.miio_mac")
906+
prop_raw = shell.get_prop("")
907+
data = re.search(r"\[ro\.sys\.model\]: \[([a-zA-Z0-9.-]+)\]", prop_raw)
908+
model = data.group(1) if data else shell.get_prop("ro.sys.model")
909+
data = re.search(r"\[ro\.sys\.name\]: \[([a-zA-Z0-9.-]+)\]", prop_raw)
910+
name = data.group(1) if data else shell.get_prop("ro.sys.name")
911+
data = re.search(r"\[persist\.sys\.miio_mac\]: \[([a-zA-Z0-9.-]+)\]", prop_raw)
912+
mac = data.group(1) if data else shell.get_prop("persist.sys.miio_mac")
901913
token = shell.get_token()
902914
elif device_name and 'g2h' in device_name:
903915
shell = TelnetShellG2H(host, password)
@@ -924,9 +936,13 @@ def is_aqaragateway(host: str,
924936
else:
925937
shell = TelnetShell(host, password)
926938
shell.login()
927-
model = shell.get_prop("persist.sys.model")
928-
name = shell.get_prop("ro.sys.name")
929-
mac = shell.get_prop("persist.sys.miio_mac")
939+
prop_raw = shell.get_prop("")
940+
data = re.search(r"\[persist\.sys\.model\]: \[([a-zA-Z0-9.-]+)\]", prop_raw)
941+
model = data.group(1) if data else shell.get_prop("persist.sys.model")
942+
data = re.search(r"\[ro\.sys\.name\]: \[([a-zA-Z0-9.-]+)\]", prop_raw)
943+
name = data.group(1) if data else shell.get_prop("ro.sys.name")
944+
data = re.search(r"\[persist\.sys\.miio_mac\]: \[([a-zA-Z0-9.-]+)\]", prop_raw)
945+
mac = data.group(1) if data else shell.get_prop("persist.sys.miio_mac")
930946
token = shell.get_token()
931947

932948
except (ConnectionError, EOFError, socket.error):

custom_components/aqara_gateway/core/lock_data.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@
6969
"someone detected": {"default": "Someone is lingering at the door"},
7070
"li battery notify":
7171
{"default": "Li Battery notify",
72-
"0": "Li Battery is abnormal",
73-
"1": "Li Battery is normal"},
72+
"0": "Li Battery is abnormal",
73+
"1": "Li Battery is normal"},
7474
"battery notify":
7575
{"default": "Battery notify",
76-
"0": "Battery is die",
77-
"1": "Battery level is low",
78-
"2": "Battery level is middle",
79-
"3": "Battery level is full"},
76+
"0": "Battery is die",
77+
"1": "Battery level is low",
78+
"2": "Battery level is middle",
79+
"3": "Battery level is full"},
8080
"camera connected": {"default": "Camera is connected"},
8181
"open in away mode": {
8282
"default":

custom_components/aqara_gateway/core/shell.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
WGET = "(wget http://master.dl.sourceforge.net/project/aqarahub/{0}?viasf=1 " \
11-
"-O /data/bin/{1} && chmod +x /data/bin/{1})"
11+
"-O /data/bin/{1} && chmod +x /data/bin/{1})"
1212

1313
CHECK_SOCAT = "(md5sum /data/socat | grep 92b77e1a93c4f4377b4b751a5390d979)"
1414
DOWNLOAD_SOCAT = "(wget -O /data/socat http://pkg.simple-ha.ru/mipsel/socat && chmod +x /data/socat)"
@@ -41,7 +41,8 @@ def login(self):
4141
self.read_until(b"Password: ", timeout=1)
4242
self.write(self._password.encode() + b"\n")
4343
self.run_command("stty -echo")
44-
self.read_until(b"/ # ", timeout=10)
44+
self.write(b"\n")
45+
self.read_until(b" # ", timeout=2)
4546

4647
# self.run_command("export PS1='# '")
4748

@@ -50,8 +51,8 @@ def run_command(self, command: str, as_bytes=False) -> Union[str, bytes]:
5051
# pylint: disable=broad-except
5152
try:
5253
self.write(command.encode() + b"\n")
53-
suffix = "\r\n{}".format(self._suffix)
54-
raw = self.read_until(suffix.encode(), timeout=30)
54+
suffix = "\n{}".format(self._suffix)
55+
raw = self.read_until(suffix.encode(), timeout=15)
5556
except Exception:
5657
raw = b''
5758
return raw if as_bytes else raw.decode()
@@ -92,12 +93,16 @@ def run_public_mosquitto(self):
9293
def check_public_mosquitto(self) -> bool:
9394
""" get processes list """
9495
raw = self.run_command("mosquitto")
95-
if "Binding listener to interface" in raw:
96-
return False
97-
return True
96+
if 'Binding listener to interface ""' in raw:
97+
return True
98+
if 'Binding listener to interface ' not in raw:
99+
return True
100+
return False
98101

99-
def get_running_ps(self) -> str:
102+
def get_running_ps(self, ps=None) -> str:
100103
""" get processes list """
104+
if isinstance(ps, str):
105+
return self.run_command(f"ps | grep {ps}")
101106
return self.run_command("ps")
102107

103108
def redirect_ha_master2mqtt(self, pattern: str):

custom_components/aqara_gateway/core/utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from aiohttp import web
1010
from miio import Device, DeviceException
1111

12+
from homeassistant.components import persistent_notification
1213
from homeassistant.components.http import HomeAssistantView
1314
from homeassistant.helpers.device_registry import DeviceRegistry
1415
from homeassistant.helpers.typing import HomeAssistantType
@@ -1908,8 +1909,8 @@ def gateway_alarm_mode_supported(model: str) -> Optional[bool]:
19081909
def gateway_infrared_supported(model: str) -> Optional[bool]:
19091910
""" return the gateway infrared supported """
19101911
if model in ('lumi.aircondition.acn05', 'lumi.gateway.iragl5',
1911-
'lumi.gateway.iragl7', 'lumi.gateway.iragl01',
1912-
'lumi.gateway.iragl8', 'lumi.gateway.agl001'):
1912+
'lumi.gateway.iragl7', 'lumi.gateway.iragl01',
1913+
'lumi.gateway.iragl8', 'lumi.gateway.agl001'):
19131914
return True
19141915
return False
19151916

@@ -2000,7 +2001,7 @@ def __init__(self, hass: HomeAssistantType):
20002001
self.url = "/{}".format(uuid.uuid4())
20012002

20022003
hass.http.register_view(self)
2003-
hass.components.persistent_notification.async_create(
2004+
persistent_notification.async_create(
20042005
NOTIFY_TEXT % self.url, title=TITLE)
20052006

20062007
def handle(self, rec: logging.LogRecord) -> None:

custom_components/aqara_gateway/sensor.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,6 @@
6060
DEVICE_MAPPINGS,
6161
)
6262

63-
GATEWAY_PLATFORMS = ["binary_sensor",
64-
"sensor",
65-
"switch",
66-
"light",
67-
"cover",
68-
"lock"]
69-
GATEWAY_PLATFORMS_NO_KEY = ["binary_sensor", "sensor"]
70-
7163

7264
async def async_setup_entry(hass, entry, async_add_entities):
7365
""" setup config entry """

0 commit comments

Comments
 (0)