Skip to content

Commit

Permalink
The very small imporved performance
Browse files Browse the repository at this point in the history
  • Loading branch information
niceboy committed Mar 13, 2024
1 parent f0d4da0 commit d424b29
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 78 deletions.
4 changes: 2 additions & 2 deletions custom_components/aqara_gateway/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
# migrate data (also after first setup) to options
if entry.data:
hass.config_entries.async_update_entry(entry, data={},
options=entry.data)
options=entry.data)

await _setup_logger(hass)

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

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

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

Expand Down
3 changes: 0 additions & 3 deletions custom_components/aqara_gateway/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,6 @@ def update(self, data: dict = None):
'entity_id': self.entity_id, 'click_type': self._state
})

time.sleep(.1)

self._state = ''

self.schedule_update_ha_state()
Expand Down Expand Up @@ -707,7 +705,6 @@ def update(self, data: dict = None):
'entity_id': self.entity_id, 'click_type': self._state
})

time.sleep(.1)

self._state = ''

Expand Down
2 changes: 1 addition & 1 deletion custom_components/aqara_gateway/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ async def async_step_discovery_confirm(self, user_input=None):
return self.async_show_form(
step_id="discovery_confirm",
description_placeholders={"name": self._name,
"device_info": self._device_info}
"device_info": self._device_info}
)

async def async_step_zeroconf(self, discovery_info: DiscoveryInfoType):
Expand Down
22 changes: 12 additions & 10 deletions custom_components/aqara_gateway/core/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,18 @@
POWER = "power"
VOLTAGE = "voltage"

DOMAINS = ['air_quality',
'alarm_control_panel',
'binary_sensor',
'climate',
'cover',
'light',
'remote',
'select',
'sensor',
'switch']
DOMAINS = [
'air_quality',
'alarm_control_panel',
'binary_sensor',
'climate',
'cover',
'light',
'remote',
'select',
'sensor',
'switch'
]

UNITS = {
SensorDeviceClass.BATTERY: PERCENTAGE,
Expand Down
54 changes: 29 additions & 25 deletions custom_components/aqara_gateway/core/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,28 +199,28 @@ def _prepare_gateway(self, get_devices: bool = False):
device_name = Utils.get_device_name(self._model).lower()
if "g2h pro" in device_name:
shell = TelnetShellG2HPro(self.host,
self.options.get(CONF_PASSWORD, ''))
self.options.get(CONF_PASSWORD, ''))
elif "g2h" in device_name:
shell = TelnetShellG2H(self.host,
self.options.get(CONF_PASSWORD, ''))
self.options.get(CONF_PASSWORD, ''))
elif "e1" in device_name:
shell = TelnetShellE1(self.host,
self.options.get(CONF_PASSWORD, ''))
self.options.get(CONF_PASSWORD, ''))
elif "g3" in device_name:
shell = TelnetShellG3(self.host,
self.options.get(CONF_PASSWORD, ''))
self.options.get(CONF_PASSWORD, ''))
elif "m2 2022" in device_name:
shell = TelnetShellM2POE(self.host,
self.options.get(CONF_PASSWORD, ''))
self.options.get(CONF_PASSWORD, ''))
elif "m1s 2022" in device_name:
shell = TelnetShellM1S22(self.host,
self.options.get(CONF_PASSWORD, ''))
self.options.get(CONF_PASSWORD, ''))
elif "m3" in device_name:
shell = TelnetShellM3(self.host,
self.options.get(CONF_PASSWORD, ''))
self.options.get(CONF_PASSWORD, ''))
else:
shell = TelnetShell(self.host,
self.options.get(CONF_PASSWORD, ''))
self.options.get(CONF_PASSWORD, ''))
shell.login()
processes = shell.get_running_ps()
public_mosquitto = shell.check_public_mosquitto()
Expand Down Expand Up @@ -429,25 +429,25 @@ def process_gateway_stats(self, payload: dict = None):
device_name = Utils.get_device_name(self._model).lower()
if "g2h pro" in device_name:
shell = TelnetShellG2HPro(self.host,
self.options.get(CONF_PASSWORD, ''))
self.options.get(CONF_PASSWORD, ''))
elif "g2h" in device_name:
shell = TelnetShellG2H(self.host,
self.options.get(CONF_PASSWORD, ''))
self.options.get(CONF_PASSWORD, ''))
elif "e1" in device_name:
shell = TelnetShellE1(self.host,
self.options.get(CONF_PASSWORD, ''))
self.options.get(CONF_PASSWORD, ''))
elif "g3" in device_name:
shell = TelnetShellG3(self.host,
self.options.get(CONF_PASSWORD, ''))
self.options.get(CONF_PASSWORD, ''))
elif "m2 2022" in device_name:
shell = TelnetShellM2POE(self.host,
self.options.get(CONF_PASSWORD, ''))
self.options.get(CONF_PASSWORD, ''))
elif "m1s 2022" in device_name:
shell = TelnetShellM1S22(self.host,
self.options.get(CONF_PASSWORD, ''))
self.options.get(CONF_PASSWORD, ''))
elif "m3" in device_name:
shell = TelnetShellM3(self.host,
self.options.get(CONF_PASSWORD, ''))
self.options.get(CONF_PASSWORD, ''))
else:
shell = TelnetShell(self.host,
self.options.get(CONF_PASSWORD, ''))
Expand Down Expand Up @@ -531,25 +531,25 @@ def _process_devices_info(self, prop, value):
device_name = Utils.get_device_name(self._model).lower()
if "g2h pro" in device_name:
shell = TelnetShellG2HPro(self.host,
self.options.get(CONF_PASSWORD, ''))
self.options.get(CONF_PASSWORD, ''))
elif "g2h" in device_name:
shell = TelnetShellG2H(self.host,
self.options.get(CONF_PASSWORD, ''))
self.options.get(CONF_PASSWORD, ''))
elif "e1" in device_name:
shell = TelnetShellE1(self.host,
self.options.get(CONF_PASSWORD, ''))
self.options.get(CONF_PASSWORD, ''))
elif "g3" in device_name:
shell = TelnetShellG3(self.host,
self.options.get(CONF_PASSWORD, ''))
self.options.get(CONF_PASSWORD, ''))
elif "m2 2022" in device_name:
shell = TelnetShellM2POE(self.host,
self.options.get(CONF_PASSWORD, ''))
self.options.get(CONF_PASSWORD, ''))
elif "m1s 2022" in device_name:
shell = TelnetShellM1S22(self.host,
self.options.get(CONF_PASSWORD, ''))
self.options.get(CONF_PASSWORD, ''))
elif "m3" in device_name:
shell = TelnetShellM3(self.host,
self.options.get(CONF_PASSWORD, ''))
self.options.get(CONF_PASSWORD, ''))
else:
shell = TelnetShell(self.host,
self.options.get(CONF_PASSWORD, ''))
Expand Down Expand Up @@ -924,9 +924,13 @@ def is_aqaragateway(host: str,
else:
shell = TelnetShell(host, password)
shell.login()
model = shell.get_prop("persist.sys.model")
name = shell.get_prop("ro.sys.name")
mac = shell.get_prop("persist.sys.miio_mac")
prop_raw = shell.get_prop("")
data = re.search(r"\[persist\.sys\.model\]: \[([a-zA-Z0-9.-]+)\]", prop_raw)
model = data.group(1) if data else shell.get_prop("persist.sys.model")
data = re.search(r"\[ro\.sys\.name\]: \[([a-zA-Z0-9.-]+)\]", prop_raw)
name = data.group(1) if data else shell.get_prop("ro.sys.name")
data = re.search(r"\[persist\.sys\.miio_mac\]: \[([a-zA-Z0-9.-]+)\]", prop_raw)
mac = data.group(1) if data else shell.get_prop("persist.sys.miio_mac")
token = shell.get_token()

except (ConnectionError, EOFError, socket.error):
Expand Down
12 changes: 6 additions & 6 deletions custom_components/aqara_gateway/core/lock_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@
"someone detected": {"default": "Someone is lingering at the door"},
"li battery notify":
{"default": "Li Battery notify",
"0": "Li Battery is abnormal",
"1": "Li Battery is normal"},
"0": "Li Battery is abnormal",
"1": "Li Battery is normal"},
"battery notify":
{"default": "Battery notify",
"0": "Battery is die",
"1": "Battery level is low",
"2": "Battery level is middle",
"3": "Battery level is full"},
"0": "Battery is die",
"1": "Battery level is low",
"2": "Battery level is middle",
"3": "Battery level is full"},
"camera connected": {"default": "Camera is connected"},
"open in away mode": {
"default":
Expand Down
36 changes: 13 additions & 23 deletions custom_components/aqara_gateway/core/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@
# pylint: disable=line-too-long
import time
import base64
import asyncio

from typing import Union
from telnetlib import Telnet


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

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

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 &)"
HA_BLE2MQTT = "(ha_ble | awk '{print $0;fflush();}' | mosquitto_pub -t log/ha_ble -l &)"


class TelnetShell(Telnet):
""" Telnet Shell """
Expand All @@ -41,7 +39,7 @@ def login(self):
self.read_until(b"Password: ", timeout=1)
self.write(self._password.encode() + b"\n")
self.run_command("stty -echo")
self.read_until(b"/ # ", timeout=10)
self.read_until(b" # ", timeout=3)

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

Expand All @@ -50,8 +48,8 @@ def run_command(self, command: str, as_bytes=False) -> Union[str, bytes]:
# pylint: disable=broad-except
try:
self.write(command.encode() + b"\n")
suffix = "\r\n{}".format(self._suffix)
raw = self.read_until(suffix.encode(), timeout=30)
suffix = "\n{}".format(self._suffix)
raw = self.read_until(suffix.encode(), timeout=15)
except Exception:
raw = b''
return raw if as_bytes else raw.decode()
Expand Down Expand Up @@ -92,26 +90,18 @@ def run_public_mosquitto(self):
def check_public_mosquitto(self) -> bool:
""" get processes list """
raw = self.run_command("mosquitto")
if "Binding listener to interface" in raw:
return False
return True
if 'Binding listener to interface ""' in raw:
return True
if 'Binding listener to interface ' not in raw:
return True
return False

def get_running_ps(self) -> str:
def get_running_ps(self, ps) -> str:
""" get processes list """
if isinstance(ps, str):
return self.run_command(f"ps | grep {ps}")
return self.run_command("ps")

def redirect_ha_master2mqtt(self, pattern: str):
""" redirect ha_master logs to mqtt """
self.run_command("killall ha_master")
time.sleep(.1)
self.run_command(HA_MASTER2MQTT % pattern)

def redirect_ha_ble2mqtt(self):
""" redirect ha_ble logs to mqtt """
self.run_command("killall ha_ble")
time.sleep(.1)
self.run_command(HA_BLE2MQTT)

def read_file(self, filename: str, as_base64=False, with_newline=True):
""" read file content """
# pylint: disable=broad-except
Expand Down
8 changes: 0 additions & 8 deletions custom_components/aqara_gateway/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,6 @@
DEVICE_MAPPINGS,
)

GATEWAY_PLATFORMS = ["binary_sensor",
"sensor",
"switch",
"light",
"cover",
"lock"]
GATEWAY_PLATFORMS_NO_KEY = ["binary_sensor", "sensor"]


async def async_setup_entry(hass, entry, async_add_entities):
""" setup config entry """
Expand Down

0 comments on commit d424b29

Please sign in to comment.