99from typing import Optional
1010from random import randint
1111from paho .mqtt .client import Client , MQTTMessage
12+ from datetime import datetime
1213
1314from homeassistant .config_entries import ConfigEntry
1415from 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 ):
0 commit comments