Skip to content

Commit d424b29

Browse files
author
niceboy
committed
The very small imporved performance
1 parent f0d4da0 commit d424b29

File tree

8 files changed

+63
-78
lines changed

8 files changed

+63
-78
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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,6 @@ def update(self, data: dict = None):
589589
'entity_id': self.entity_id, 'click_type': self._state
590590
})
591591

592-
time.sleep(.1)
593-
594592
self._state = ''
595593

596594
self.schedule_update_ha_state()
@@ -707,7 +705,6 @@ def update(self, data: dict = None):
707705
'entity_id': self.entity_id, 'click_type': self._state
708706
})
709707

710-
time.sleep(.1)
711708

712709
self._state = ''
713710

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: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -199,28 +199,28 @@ def _prepare_gateway(self, get_devices: bool = False):
199199
device_name = Utils.get_device_name(self._model).lower()
200200
if "g2h pro" in device_name:
201201
shell = TelnetShellG2HPro(self.host,
202-
self.options.get(CONF_PASSWORD, ''))
202+
self.options.get(CONF_PASSWORD, ''))
203203
elif "g2h" in device_name:
204204
shell = TelnetShellG2H(self.host,
205-
self.options.get(CONF_PASSWORD, ''))
205+
self.options.get(CONF_PASSWORD, ''))
206206
elif "e1" in device_name:
207207
shell = TelnetShellE1(self.host,
208-
self.options.get(CONF_PASSWORD, ''))
208+
self.options.get(CONF_PASSWORD, ''))
209209
elif "g3" in device_name:
210210
shell = TelnetShellG3(self.host,
211-
self.options.get(CONF_PASSWORD, ''))
211+
self.options.get(CONF_PASSWORD, ''))
212212
elif "m2 2022" in device_name:
213213
shell = TelnetShellM2POE(self.host,
214-
self.options.get(CONF_PASSWORD, ''))
214+
self.options.get(CONF_PASSWORD, ''))
215215
elif "m1s 2022" in device_name:
216216
shell = TelnetShellM1S22(self.host,
217-
self.options.get(CONF_PASSWORD, ''))
217+
self.options.get(CONF_PASSWORD, ''))
218218
elif "m3" in device_name:
219219
shell = TelnetShellM3(self.host,
220-
self.options.get(CONF_PASSWORD, ''))
220+
self.options.get(CONF_PASSWORD, ''))
221221
else:
222222
shell = TelnetShell(self.host,
223-
self.options.get(CONF_PASSWORD, ''))
223+
self.options.get(CONF_PASSWORD, ''))
224224
shell.login()
225225
processes = shell.get_running_ps()
226226
public_mosquitto = shell.check_public_mosquitto()
@@ -429,25 +429,25 @@ def process_gateway_stats(self, payload: dict = None):
429429
device_name = Utils.get_device_name(self._model).lower()
430430
if "g2h pro" in device_name:
431431
shell = TelnetShellG2HPro(self.host,
432-
self.options.get(CONF_PASSWORD, ''))
432+
self.options.get(CONF_PASSWORD, ''))
433433
elif "g2h" in device_name:
434434
shell = TelnetShellG2H(self.host,
435-
self.options.get(CONF_PASSWORD, ''))
435+
self.options.get(CONF_PASSWORD, ''))
436436
elif "e1" in device_name:
437437
shell = TelnetShellE1(self.host,
438-
self.options.get(CONF_PASSWORD, ''))
438+
self.options.get(CONF_PASSWORD, ''))
439439
elif "g3" in device_name:
440440
shell = TelnetShellG3(self.host,
441-
self.options.get(CONF_PASSWORD, ''))
441+
self.options.get(CONF_PASSWORD, ''))
442442
elif "m2 2022" in device_name:
443443
shell = TelnetShellM2POE(self.host,
444-
self.options.get(CONF_PASSWORD, ''))
444+
self.options.get(CONF_PASSWORD, ''))
445445
elif "m1s 2022" in device_name:
446446
shell = TelnetShellM1S22(self.host,
447-
self.options.get(CONF_PASSWORD, ''))
447+
self.options.get(CONF_PASSWORD, ''))
448448
elif "m3" in device_name:
449449
shell = TelnetShellM3(self.host,
450-
self.options.get(CONF_PASSWORD, ''))
450+
self.options.get(CONF_PASSWORD, ''))
451451
else:
452452
shell = TelnetShell(self.host,
453453
self.options.get(CONF_PASSWORD, ''))
@@ -531,25 +531,25 @@ def _process_devices_info(self, prop, value):
531531
device_name = Utils.get_device_name(self._model).lower()
532532
if "g2h pro" in device_name:
533533
shell = TelnetShellG2HPro(self.host,
534-
self.options.get(CONF_PASSWORD, ''))
534+
self.options.get(CONF_PASSWORD, ''))
535535
elif "g2h" in device_name:
536536
shell = TelnetShellG2H(self.host,
537-
self.options.get(CONF_PASSWORD, ''))
537+
self.options.get(CONF_PASSWORD, ''))
538538
elif "e1" in device_name:
539539
shell = TelnetShellE1(self.host,
540-
self.options.get(CONF_PASSWORD, ''))
540+
self.options.get(CONF_PASSWORD, ''))
541541
elif "g3" in device_name:
542542
shell = TelnetShellG3(self.host,
543-
self.options.get(CONF_PASSWORD, ''))
543+
self.options.get(CONF_PASSWORD, ''))
544544
elif "m2 2022" in device_name:
545545
shell = TelnetShellM2POE(self.host,
546-
self.options.get(CONF_PASSWORD, ''))
546+
self.options.get(CONF_PASSWORD, ''))
547547
elif "m1s 2022" in device_name:
548548
shell = TelnetShellM1S22(self.host,
549-
self.options.get(CONF_PASSWORD, ''))
549+
self.options.get(CONF_PASSWORD, ''))
550550
elif "m3" in device_name:
551551
shell = TelnetShellM3(self.host,
552-
self.options.get(CONF_PASSWORD, ''))
552+
self.options.get(CONF_PASSWORD, ''))
553553
else:
554554
shell = TelnetShell(self.host,
555555
self.options.get(CONF_PASSWORD, ''))
@@ -924,9 +924,13 @@ def is_aqaragateway(host: str,
924924
else:
925925
shell = TelnetShell(host, password)
926926
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")
927+
prop_raw = shell.get_prop("")
928+
data = re.search(r"\[persist\.sys\.model\]: \[([a-zA-Z0-9.-]+)\]", prop_raw)
929+
model = data.group(1) if data else shell.get_prop("persist.sys.model")
930+
data = re.search(r"\[ro\.sys\.name\]: \[([a-zA-Z0-9.-]+)\]", prop_raw)
931+
name = data.group(1) if data else shell.get_prop("ro.sys.name")
932+
data = re.search(r"\[persist\.sys\.miio_mac\]: \[([a-zA-Z0-9.-]+)\]", prop_raw)
933+
mac = data.group(1) if data else shell.get_prop("persist.sys.miio_mac")
930934
token = shell.get_token()
931935

932936
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 & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,20 @@
22
# pylint: disable=line-too-long
33
import time
44
import base64
5+
import asyncio
56

67
from typing import Union
78
from telnetlib import Telnet
89

910

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

1314
CHECK_SOCAT = "(md5sum /data/socat | grep 92b77e1a93c4f4377b4b751a5390d979)"
1415
DOWNLOAD_SOCAT = "(wget -O /data/socat http://pkg.simple-ha.ru/mipsel/socat && chmod +x /data/socat)"
1516
RUN_SOCAT_BT_IRDA = "/data/socat tcp-l:8888,reuseaddr,fork /dev/ttyS2"
1617
RUN_SOCAT_ZIGBEE = "/data/socat tcp-l:8888,reuseaddr,fork /dev/ttyS1"
1718

18-
HA_MASTER2MQTT = "(ha_master -H -r /lib/libha_ir_m2.so -a /lib/libha_auto.so -g /lib/libha_energy.so | awk '/%s/{print $0;fflush()}' | mosquitto_pub -t log/ha_master -l &)"
19-
HA_BLE2MQTT = "(ha_ble | awk '{print $0;fflush();}' | mosquitto_pub -t log/ha_ble -l &)"
20-
2119

2220
class TelnetShell(Telnet):
2321
""" Telnet Shell """
@@ -41,7 +39,7 @@ def login(self):
4139
self.read_until(b"Password: ", timeout=1)
4240
self.write(self._password.encode() + b"\n")
4341
self.run_command("stty -echo")
44-
self.read_until(b"/ # ", timeout=10)
42+
self.read_until(b" # ", timeout=3)
4543

4644
# self.run_command("export PS1='# '")
4745

@@ -50,8 +48,8 @@ def run_command(self, command: str, as_bytes=False) -> Union[str, bytes]:
5048
# pylint: disable=broad-except
5149
try:
5250
self.write(command.encode() + b"\n")
53-
suffix = "\r\n{}".format(self._suffix)
54-
raw = self.read_until(suffix.encode(), timeout=30)
51+
suffix = "\n{}".format(self._suffix)
52+
raw = self.read_until(suffix.encode(), timeout=15)
5553
except Exception:
5654
raw = b''
5755
return raw if as_bytes else raw.decode()
@@ -92,26 +90,18 @@ def run_public_mosquitto(self):
9290
def check_public_mosquitto(self) -> bool:
9391
""" get processes list """
9492
raw = self.run_command("mosquitto")
95-
if "Binding listener to interface" in raw:
96-
return False
97-
return True
93+
if 'Binding listener to interface ""' in raw:
94+
return True
95+
if 'Binding listener to interface ' not in raw:
96+
return True
97+
return False
9898

99-
def get_running_ps(self) -> str:
99+
def get_running_ps(self, ps) -> str:
100100
""" get processes list """
101+
if isinstance(ps, str):
102+
return self.run_command(f"ps | grep {ps}")
101103
return self.run_command("ps")
102104

103-
def redirect_ha_master2mqtt(self, pattern: str):
104-
""" redirect ha_master logs to mqtt """
105-
self.run_command("killall ha_master")
106-
time.sleep(.1)
107-
self.run_command(HA_MASTER2MQTT % pattern)
108-
109-
def redirect_ha_ble2mqtt(self):
110-
""" redirect ha_ble logs to mqtt """
111-
self.run_command("killall ha_ble")
112-
time.sleep(.1)
113-
self.run_command(HA_BLE2MQTT)
114-
115105
def read_file(self, filename: str, as_base64=False, with_newline=True):
116106
""" read file content """
117107
# pylint: disable=broad-except

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)