diff --git a/custom_components/ds_air/ds_air_service/dao.py b/custom_components/ds_air/ds_air_service/dao.py index 4069f3f..38d6350 100644 --- a/custom_components/ds_air/ds_air_service/dao.py +++ b/custom_components/ds_air/ds_air_service/dao.py @@ -94,13 +94,15 @@ def __init__(self): self.switch: EnumSwitch -class Sensor(Device): - STATUS_ATTR = ["mac", "type1", "type2", "start_time", "stop_time", "sensor_type", "temp", "humidity", "pm25", "co2", - "voc", "tvoc", "hcho", "switch_on_off", "temp_upper", "temp_lower", "humidity_upper", - "humidity_lower", "pm25_upper", "pm25_lower", "co2_upper", "co2_lower", "voc_lower", "tvoc_upper", - "hcho_upper", "connected", "sleep_mode_count", "time_millis"] +STATUS_ATTR = ["mac", "type1", "type2", "start_time", "stop_time", "sensor_type", "temp", "humidity", "pm25", "co2", + "voc", "tvoc", "hcho", "switch_on_off", "temp_upper", "temp_lower", "humidity_upper", + "humidity_lower", "pm25_upper", "pm25_lower", "co2_upper", "co2_lower", "voc_lower", "tvoc_upper", + "hcho_upper", "connected", "sleep_mode_count", "time_millis"] + +UNINITIALIZED_VALUE = -1000 - UNINITIALIZED_VALUE = -1000 + +class Sensor(Device): def __init__(self): Device.__init__(self) diff --git a/custom_components/ds_air/ds_air_service/decoder.py b/custom_components/ds_air/ds_air_service/decoder.py index 77b99d3..795d7ea 100644 --- a/custom_components/ds_air/ds_air_service/decoder.py +++ b/custom_components/ds_air/ds_air_service/decoder.py @@ -5,7 +5,8 @@ from .config import Config from .ctrl_enum import EnumDevice, EnumCmdType, EnumFanDirection, EnumOutDoorRunCond, EnumFanVolume, EnumControl, \ EnumSensor, FreshAirHumidification, ThreeDFresh -from .dao import Room, AirCon, Geothermic, Ventilation, HD, Device, AirConStatus, get_device_by_aircon, Sensor +from .dao import Room, AirCon, Geothermic, Ventilation, HD, Device, AirConStatus, get_device_by_aircon, Sensor, \ + UNINITIALIZED_VALUE from .param import GetRoomInfoParam, AirConRecommendedIndoorTempParam, AirConCapabilityQueryParam, \ AirConQueryStatusParam, Sensor2InfoParam @@ -167,13 +168,13 @@ def __init__(self, cmd_id: int, target: EnumDevice): self._sensors: typing.List[Sensor] = [] def load_bytes(self, b): - d = Decode(b) - self._mode = d.read1() - count = d.read1() + data = Decode(b) + self._mode = data.read1() + count = data.read1() self._count = count while count > 0: - self._room_id = d.read1() - d = Decode(d.read(d.read1())) + self._room_id = data.read1() + d = Decode(data.read(data.read1())) self._sensor_type = d.read1() unit_id = d.read1() sensor = Sensor() @@ -185,24 +186,24 @@ def load_bytes(self, b): sensor.name = sensor.alias sensor.type1 = d.read1() sensor.type2 = d.read1() - humidity = Sensor.UNINITIALIZED_VALUE - hcho = Sensor.UNINITIALIZED_VALUE - temp = Sensor.UNINITIALIZED_VALUE + humidity = UNINITIALIZED_VALUE + hcho = UNINITIALIZED_VALUE + temp = UNINITIALIZED_VALUE if (sensor.type1 & 1) == 1: temp = d.read2() if ((sensor.type1 >> 1) & 1) == 1: humidity = d.read2() - pm25 = Sensor.UNINITIALIZED_VALUE + pm25 = UNINITIALIZED_VALUE if (sensor.type1 >> 2) & 1 == 1: pm25 = d.read2() - co2 = Sensor.UNINITIALIZED_VALUE + co2 = UNINITIALIZED_VALUE if (sensor.type1 >> 3) & 1 == 1: co2 = d.read2() voc = EnumSensor.Voc.STEP_UNUSE if (sensor.type1 >> 4) & 1 == 1: f = d.read1() voc = EnumSensor.Voc(f) - tvoc = Sensor.UNINITIALIZED_VALUE + tvoc = UNINITIALIZED_VALUE if (sensor.type1 >> 5) & 1 == 1: tvoc = d.read2() if (sensor.type1 >> 6) & 1 == 1: diff --git a/custom_components/ds_air/ds_air_service/service.py b/custom_components/ds_air/ds_air_service/service.py index 36ee662..5c148ea 100644 --- a/custom_components/ds_air/ds_air_service/service.py +++ b/custom_components/ds_air/ds_air_service/service.py @@ -5,7 +5,7 @@ from threading import Thread, Lock from .ctrl_enum import EnumDevice -from .dao import Room, AirCon, AirConStatus, get_device_by_aircon, Sensor +from .dao import Room, AirCon, AirConStatus, get_device_by_aircon, Sensor, STATUS_ATTR from .decoder import decoder, BaseResult from .display import display from .param import Param, HandShakeParam, HeartbeatParam, AirConControlParam, AirConQueryStatusParam, Sensor2InfoParam @@ -75,12 +75,17 @@ def recv(self) -> (typing.List[BaseResult], bytes): return [], None time.sleep(3) self.do_connect() - d = data + if data is not None: + _log("hex: 0x"+data.hex()) while data: - r, b = decoder(data) - res.append(r) - data = b - return res, d + try: + r, b = decoder(data) + res.append(r) + data = b + except Exception as e: + _log(e) + data = None + return res class RecvThread(Thread): @@ -95,14 +100,16 @@ def terminate(self): def run(self) -> None: while self._running: - res, data = self._sock.recv() - if data is not None: - _log("hex: 0x"+data.hex()) + res = self._sock.recv() for i in res: _log('\033[31mrecv:\033[0m') _log(display(i)) self._locker.acquire() - i.do() + try: + if i is not None: + i.do() + except Exception as e: + _log(e) self._locker.release() @@ -266,17 +273,17 @@ def set_aircon_status(target: EnumDevice, room: int, unit: int, status: AirConSt @staticmethod def set_sensors_status(sensors: typing.List[Sensor]): - for newSensor in sensors: + for new_sensor in sensors: for sensor in Service._sensors: - if sensor.name == newSensor.name or sensor.alias == newSensor.alias: - for attr in Sensor.STATUS_ATTR: - setattr(sensor, attr, getattr(newSensor, attr)) + if sensor.unique_id == new_sensor.unique_id: + for attr in STATUS_ATTR: + setattr(sensor, attr, getattr(new_sensor, attr)) break for item in Service._sensor_hook: unique_id, func = item - if newSensor.unique_id == unique_id: + if new_sensor.unique_id == unique_id: try: - func(newSensor) + func(new_sensor) except Exception as e: _log(str(e)) diff --git a/custom_components/ds_air/manifest.json b/custom_components/ds_air/manifest.json index 00535b0..b03abec 100644 --- a/custom_components/ds_air/manifest.json +++ b/custom_components/ds_air/manifest.json @@ -5,6 +5,6 @@ "dependencies": [], "codeowners": [], "requirements": [], - "version": "1.3.0", + "version": "1.3.1", "config_flow": true } diff --git a/custom_components/ds_air/sensor.py b/custom_components/ds_air/sensor.py index a46eb87..aae3f5a 100644 --- a/custom_components/ds_air/sensor.py +++ b/custom_components/ds_air/sensor.py @@ -5,7 +5,7 @@ from homeassistant.helpers.entity import DeviceInfo from .const import DOMAIN, SENSOR_TYPES -from .ds_air_service.dao import Sensor +from .ds_air_service.dao import Sensor, UNINITIALIZED_VALUE from .ds_air_service.service import Service @@ -88,8 +88,8 @@ def state(self): def parse_data(self, device: Sensor, not_update: bool = False): """Parse data sent by gateway.""" - self._is_available = device.connected and device.switch_on_off - if Sensor.UNINITIALIZED_VALUE != getattr(device, self._data_key): + self._is_available = device.connected + if UNINITIALIZED_VALUE != getattr(device, self._data_key): if type(SENSOR_TYPES.get(self._data_key)[3]) != int: self._state = str(getattr(device, self._data_key)) else: