Skip to content
Snippets Groups Projects
Commit 58758d8c authored by jkerdreu's avatar jkerdreu
Browse files

Added support for battery and AQ2



git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Python/branches/0.7@2595 b32b6428-25c9-4566-ad07-03861ab6144f
parent d53836b2
No related branches found
No related tags found
No related merge requests found
......@@ -13,6 +13,7 @@ AQARA_ENCRYPT_IV = b'\x17\x99\x6d\x09\x3d\x28\xdd\xb3\xba\x69\x5a\x2e\x6f\x58\x5
logger = logging.getLogger(__name__)
BATTERY_LESS = ['lumi.ctrl_dualchn','gateway']
def find_device_class(model):
if model in ['sensor_switch.aq3','sensor_switch.aq2','switch','86sw1']:
......@@ -23,8 +24,10 @@ def find_device_class(model):
return Gateway
if model == 'weather.v1':
return Weather
if model in ['motion','sensor_motion.aq2']:
if model == 'motion':
return Motion
if model == 'sensor_motion.aq2':
return MotionAQ2
if model in ['magnet','sensor_magnet.aq2']:
return Magnet
if model == 'vibration':
......@@ -32,12 +35,11 @@ def find_device_class(model):
if model == 'sensor_cube.aqgl01':
return Cube
if model == 'sensor_wleak.aq1':
return RelayController
return WaterLeak
if model == 'lumi.ctrl_dualchn':
return RelayController
return None
class AqaraDev(object):
def __init__(self,sid,model,base_addr,xaal_gw):
self.sid = sid
......@@ -48,11 +50,23 @@ class AqaraDev(object):
self.devices = []
logger.info(f"Loading AqaraDevice {model} {sid}")
self.setup()
self.add_battery()
self.init_properties()
def setup(self):
logger.warning(f"Please overide setup() in {self.__class__}")
def add_battery(self):
self.battery = None
if self.model in BATTERY_LESS:
return
if len(self.devices)!=0:
addr = self.devices[-1].address + 1
bat = devices.battery(addr)
bat.attributes['devices'] = [dev.address for dev in self.devices]
self.devices.append(bat)
self.battery = bat
def init_properties(self):
for dev in self.devices:
dev.vendor_id = 'Xiaomi / Aqara'
......@@ -73,6 +87,7 @@ class AqaraDev(object):
# json in json really ? grr
data = json.loads(pload)
if cmd == 'report':
#print(data)
self.on_report(data)
if cmd == 'heartbeat':
self.on_heartbeat(data)
......@@ -81,11 +96,25 @@ class AqaraDev(object):
else:
logger.info(pkt)
def parse_voltage(self,data):
# https://github.com/home-assistant/core/blob/e63e8b6ffe627dce8ee7574c652af99267eb7376/homeassistant/components/xiaomi_aqara/__init__.py#L355
if not self.battery:
return
val = data.get('voltage',None)
if val:
voltage = int(val)
max_volt = 3300
min_volt = 2800
voltage = min(voltage, max_volt)
voltage = max(voltage, min_volt)
percent = ((voltage - min_volt) / (max_volt - min_volt)) * 100
self.battery.attributes['level'] = percent
def on_report(self,data):
logger.info('Unhandled report %s' % data)
def on_heartbeat(self,data):
#logger.info('Unhandled heartbeat %s' % data)
self.parse_voltage(data)
self.on_report(data)
def on_iam(self,data):
......@@ -151,8 +180,9 @@ class SensorHT(AqaraDev):
class Motion(AqaraDev):
def setup(self):
self.devices.append(devices.motion(self.base_addr))
def on_report(self,data):
# motion
val = data.get('status',None)
if val and val == 'motion':
self.devices[0].attributes['presence'] = True
......@@ -160,6 +190,16 @@ class Motion(AqaraDev):
if val:
self.devices[0].attributes['presence'] = False
class MotionAQ2(Motion):
def setup(self):
Motion.setup(self)
self.devices.append(devices.luxmeter(self.base_addr+1))
def on_report(self,data):
Motion.on_report(self,data)
val = data.get('illumination',None)
if val:
self.devices[1].attributes['illuminance'] = int(val)
class Magnet(AqaraDev):
def setup(self):
......@@ -217,6 +257,9 @@ class Gateway(AqaraDev):
siren.methods['debug'] = self.debug
self.devices.append(siren)
lux = devices.luxmeter(self.base_addr+2)
self.devices.append(lux)
def debug(self):
import pdb;pdb.set_trace()
......@@ -410,6 +453,9 @@ class Gateway(AqaraDev):
rgb = int(rgb)
if rgb != 0:
self.rgb = rgb
val = data.get('illumination',None)
if val:
self.devices[2].attributes['illuminance'] = int(val)
def on_iam(self,data):
self.search_ip(data)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment