Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic xiaomi candela support #119

Open
wants to merge 4 commits into
base: beta
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 61 additions & 0 deletions core/config/devices/yeelight/yeelight_candela.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"yeelight_candela": {
"name": "Yeelight - Candela",
"groupe" : "Lumières",
"configuration" : {
"cancontrol" : 1,
"name" : "yeelight_candela",
"xiaomi" : 1,
"canbelocked" : 1
},
"commands": [
{
"name": "On",
"type": "action",
"subtype": "other",
"isVisible": 1,
"unite": "",
"logicalId": "name:yeelight_candela,type:on",
"display": {
"icon": "<i class=\"icon jeedom2-light-bulb2\"><\/i>"
}
},
{
"name": "Off",
"type": "action",
"subtype": "other",
"isVisible": 1,
"unite": "",
"logicalId": "name:yeelight_candela,type:off",
"display": {
"icon": "<i class=\"icon jeedom2-light-bulb7\"><\/i>"
}
},
{
"name": "Luminosité",
"type": "action",
"subtype": "slider",
"isVisible": 1,
"unite": "",
"logicalId": "name:yeelight_candela,value:#slider#,type:brightness",
"configuration": {
"minValue": 1,
"maxValue": 64
}
}
],
"compatibility": [
{
"manufacturer": "Yeelight",
"name": "Candela",
"doc": "",
"type": "Lumières",
"battery_type": "",
"ref" : "",
"comlink": "",
"inclusion" : "Mode inclusion",
"imglink": "yeelight_candela"
}
]
}
}
116 changes: 17 additions & 99 deletions resources/blead/devices/yeelightcandela.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,134 +9,52 @@

class YeelightCandela():
def __init__(self):
self.name = 'yeelightcandela'
self.name = 'yeelight_candela'
self.ignoreRepeat = True
self.key = 'eff6e316dcf300000000000000000000'

def isvalid(self,name,manuf='',data='',mac=''):
if 'yeelight_ms' in [name.lower()] or name.lower()==self.name:
return True

def parse(self,data,mac,name,manuf):
action={}
action['present'] = 1
return action

def action(self,message):
logging.debug('toto')
logging.debug('action for yeelight_candela received')
type =''
mac = message['device']['id']
handle = message['command']['handle']
value = message['command']['value']
if 'type' in message['command']:
type = message['command']['type']
if mac in globals.KEEPED_CONNECTION:
logging.debug('Already a connection for ' + mac + ' use it')
conn = globals.KEEPED_CONNECTION[mac]
conn = globals.KEEPED_CONNECTION[mac.upper()]
else:
logging.debug('Creating a new connection for ' + mac)
conn = Connector(mac)
globals.KEEPED_CONNECTION[mac]=conn
conn.connect()
if not conn.isconnected:
conn.connect()
if not conn.isconnected:
return
if type == 'pair':
conn.writeCharacteristic('0x12','4367'+self.key)
time.sleep(5)
if type == 'switch':
conn.writeCharacteristic('0x12','4367'+self.key)
conn.writeCharacteristic(handle,value.ljust(36,'0'))
if type == 'color':
conn.writeCharacteristic('0x12','4367'+self.key)
if value == '000000':
conn.writeCharacteristic(handle,('434002').ljust(36,'0'))
if conn.isconnected:
if conn.mac.upper() in globals.KNOWN_DEVICES and globals.KNOWN_DEVICES[conn.mac.upper()]['islocked'] == 1:
# Pair
conn.writeCharacteristic('0x001f','4367'+self.key)
else:
conn.writeCharacteristic(handle,('4341'+value).ljust(36,'0'))
return
if type == 'on':
conn.writeCharacteristic('0x001f','434001')
if type == 'off':
conn.writeCharacteristic('0x001f','434002')
if type == 'brightness':
init = utils.tuple_to_hex(struct.unpack('18B',conn.readCharacteristic('0x12')))
conn.writeCharacteristic('0x12','4367'+self.key)
if str(init)[0:4]=='4343':
logging.debug(str(init)[0:8])
conn.writeCharacteristic(handle,str(init)[0:8]+ hex(int(value))[2:].zfill(2)+ str(init)[10:36])
else:
conn.writeCharacteristic(handle,str(init)[0:12]+ hex(int(value))[2:].zfill(2)+ str(init)[14:36])
if type == 'white':
conn.writeCharacteristic('0x12','4367'+self.key)
logging.debug('4343'+ (hex(int(value))[2:].zfill(4)+ '00').ljust(36,'0'))
conn.writeCharacteristic(handle,('4343'+ hex(int(value))[2:].zfill(4)+ '00').ljust(36,'0'))
value = min(64, max(0, int(float(message['command']['value']))))
conn.writeCharacteristic('0x001f','4342'+str(value).rjust(2, '0'))
conn.disconnect()
return

def read(self,mac,connection=''):
# TODO
result={}
try:
if mac in globals.KEEPED_CONNECTION:
logging.debug('Already a connection for ' + mac + ' use it')
conn = globals.KEEPED_CONNECTION[mac]
else:
if connection != '':
conn = connection
else:
logging.debug('Creating a new connection for ' + mac)
conn = Connector(mac)
globals.KEEPED_CONNECTION[mac]=conn
conn.connect()
if not conn.isconnected:
conn.connect()
if not conn.isconnected:
return
refreshlist = globals.KNOWN_DEVICES[mac]['refreshlist']
logging.debug('Here is the list to refresh ' + str(refreshlist))
if 'color' in refreshlist:
try:
color = utils.tuple_to_hex(struct.unpack('4B',conn.readCharacteristic(refreshlist['color'])))
if color[0:2] != '00':
color = 'FFFFFF'
else:
color = color[2:]
result['color'] = '#'+color
except Exception as e:
logging.debug(str(e))
if 'effect' in refreshlist:
try:
effect = utils.tuple_to_hex(struct.unpack('8B',conn.readCharacteristic(refreshlist['effect'])))
mode = effect[8:10]
if mode == '04':
result['mode'] = 'Bougie'
elif mode =='01':
result['mode'] = 'Fondu uni'
elif mode =='00':
result['mode'] = 'Flash uni'
elif mode =='02':
result['mode'] = 'Flash arc-en-ciel'
elif mode =='03':
result['mode'] = 'Fondu arc-en-ciel'
else:
result['mode'] = 'Aucun'
speed = 255-int(effect[12:14],16)
result['speed'] = speed
except Exception as e:
logging.debug(str(e))
if 'battery' in refreshlist:
try:
if 'hasbatteryinfo' in refreshlist and refreshlist['hasbatteryinfo'] == 1:
battery = struct.unpack('2B',conn.readCharacteristic(refreshlist['battery']))
else:
battery = struct.unpack('1B',conn.readCharacteristic(refreshlist['battery']))
result['battery'] = battery[0]
if 'hasbatteryinfo' in refreshlist and refreshlist['hasbatteryinfo'] == 1:
if battery[1]:
result['mode'] = result['mode'] + ' (En charge)'
else:
result['mode'] = result['mode'] + ' (En décharge)'
except Exception as e:
logging.debug(str(e))
except Exception as e:
logging.debug(str(e))
conn.disconnect()
return
logging.debug(str(result))
conn.disconnect()
result['id'] = mac
return result

Expand Down