Skip to content

Commit

Permalink
Merge pull request #25 from mypal/test
Browse files Browse the repository at this point in the history
version1.3.1
  • Loading branch information
mypal authored Oct 10, 2021
2 parents 112c6d8 + 73e5a83 commit bb0313e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 38 deletions.
14 changes: 8 additions & 6 deletions custom_components/ds_air/ds_air_service/dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 13 additions & 12 deletions custom_components/ds_air/ds_air_service/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand All @@ -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:
Expand Down
39 changes: 23 additions & 16 deletions custom_components/ds_air/ds_air_service/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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()


Expand Down Expand Up @@ -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))

Expand Down
2 changes: 1 addition & 1 deletion custom_components/ds_air/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"dependencies": [],
"codeowners": [],
"requirements": [],
"version": "1.3.0",
"version": "1.3.1",
"config_flow": true
}
6 changes: 3 additions & 3 deletions custom_components/ds_air/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit bb0313e

Please sign in to comment.