Package: mfi-mpower
Asynchronous Python API for mFi mPower devicesNotesThis package provides adirectasynchronous API for Ubiquiti mFi mPower devices based onAIOHTTPandAsyncSSH. The mFi product line which are is sadly EOL since 2015 and the latest available mFi firmware is version 2.1.11, which can be foundhere.Please note that even with the latest available mFi firmware, Ubiquiti mFi mPower Devices are quite unhurried and use OpenSSL 1.0.0g (18 Jan 2012) as well as Dropbear SSH 0.51 (27 Mar 2008).SLL connections are thus limited to TLSv1.0. The mFi mPower package pins the cipher use explicitly toAES128-SHAin order to get the fastest 2048 bit strength and avoid DES and RC4. This results in the highest possible rating according to thenmap enum-cipher-script. The default device certificate is self-signed and too weak (512 bit) for todays standards. SSL certificate verification is therefore disabled by default. The certificate can however be replaced with your own.As mFi mPower devices are usually communicating only in a local network and not via the internet, some old SSL still seems to be much better than no encryption at all.Be aware that SSL is only supported until TLSv1.0 is eventually removed from Python - at least unless someone finds a way to replace the OpenSSL binary with a more recent version until then.A brief description of the old "REST" API can be found in theUI Communitybut some additional "reverse engineering" was necessary to extract device info. There still seems no way to extract board or model information without SSH. Any hints are very much appreciated!To extract board information via SSH, only thessh-rsahost key algorithm in combination with thediffie-hellman-group1-sha1key exchange is supported. The latter is available aslegacy option. There is also aknown bugin older Dropbear versions which truncates the list of offered key algorithms. The mFi mPower package therefore limits the offered key algorithms tossh-rsaand the encryption algorithm toaes128-cbc. Known host checks will bedisabledas this would require user interaction.Basic exampleimportasyncioimportaiohttpfrommfi_mpower.deviceimportMPowerDeviceasyncdefmain():data={"host":"name_or_ip","username":"ubnt","password":"ubnt","use_ssl":True,"verify_ssl":False,"board_info":None,}asyncwithaiohttp.ClientSession()assession:asyncwithMPowerDevice(**data,session=session)asdevice:# Turn port 1 off and toggle it afterwards back onswitch1=awaitdevice.create_switch(1)awaitswitch1.set(False)awaitasyncio.sleep(5)awaitswitch1.toggle()asyncio.run(main())Better exampleimportasyncioimportaiohttpfrommfi_mpower.deviceimportMPowerDeviceasyncdefquery(host:str)->None:"""Async query"""data={"username":"ubnt","password":"ubnt","use_ssl":True,"verify_ssl":False,"board_info":None,}data["host"]=hostasyncwithaiohttp.ClientSession()assession:asyncwithMPowerDevice(**data,session=session)asdevice:# Print device infoprint(device)# Print device board dataprint(device.board)# Print all switches and their stateswitches=awaitdevice.create_switches()forswitchinswitches:print(switch)# Print all sensors and their datasensors=awaitdevice.create_sensors()forsensorinsensors:print(sensor)asyncdefmain()->None:"""Async main"""hosts=["host1","host2","host3","host4","host5","host6",]awaitasyncio.gather(*[query(host)forhostinhosts])asyncio.run(main())