Python Plugin: import Module Topic is solved

Python and python framework

Moderator: leecollings

Post Reply
zaraki673
Posts: 32
Joined: Thursday 19 January 2017 23:46
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Python Plugin: import Module

Post by zaraki673 »

Hi,

I want to create a new plugin who use bluepy to check and control Awox Smartplug
I already write a python script for domoticz who read status from the plug and send data to domoticz.

My domoticz system is a rpi3 with raspbian jessie lite and I compile domoticz from source (Version: 3.6389), PluginSystem work very well (I tried with Kody plugins, all work fine)

But with my new plugin, when i ask to import bluepy it's fail with this error :

Code: Select all

2017-01-19 23:59:03.109 Error: (smp) Module Import failed, exception: 'ImportError'
2017-01-19 23:59:03.109 Error: (smp) Module Import failed: ' Name: bluepy'
My code work for configuration in Hardware menu and create news devices, but don't load bluepy module

here the beginning of my plugin :
Spoiler: show

Code: Select all

#		   Awox SmartPlug Plugin
#
#		   Author:	 zaraki673, 2017
#
"""
<plugin key="AwoxSMP" name="Awox SmartPlug" author="zaraki673" version="1.0.0">
	<params>
		<param field="Mode1" label="MAC Address" width="150px" required="true"/>
		<param field="Mode6" label="Debug" width="75px">
			<options>
				<option label="True" value="Debug"/>
				<option label="False" value="Normal"  default="true" />
			</options>
		</param>
	</params>
</plugin>
"""
import Domoticz
import binascii
import struct
from bluepy import btle


START_OF_MESSAGE = b'\x0f'
END_OF_MESSAGE = b'\xff\xff'
SMPstate = 0
SMPconso = 0


# Domoticz call back functions
def onStart():
	global SMPstate, SMPconso
	if Parameters["Mode6"] == "Debug":
		Domoticz.Debugging(1)
	if (len(Devices) == 0):
		Domoticz.Device(Name="Status",  Unit=1, TypeName="Switch").Create()
		Domoticz.Device(Name="Conso", Unit=2, TypeName="Usage").Create()
		Domoticz.Log("Devices created.")
	else:
		if (1 in Devices): SMPstate = Devices[1].nValue
		if (2 in Devices): SMPconso = Devices[2].nValue
	DumpConfigToLog()
	Domoticz.Heartbeat(5)
	return True

def onConnect(Status, Description):

	return True

def onMessage(Data, Status, Extra):

	return True

def onCommand(Unit, Command, Level, Hue):

	return True

def onNotification(Data):
	Domoticz.Log("Notification: " + str(Data))
	return

def onHeartbeat():
	global SMPstate, SMPconso
	try:
		(state, power) = plug.status_request()
	except btle.BTLEException as err:
		Domoticz.Log('error when requesting stat to plug %s (code %d)' % (args.ble_addr, err.code))
	# print result
	status = 'on' if state else 'off'
	Domoticz.Log('plug state = %s' % status)
	SMPstate = status
	Domoticz.Log('plug power = %d W' % power)
	SMPconso = power
	
	

	return True

def onDisconnect():

	return True

def onStop():

	return True

# Generic helper functions
def DumpConfigToLog():
	for x in Parameters:
		if Parameters[x] != "":
			Domoticz.Debug( "'" + x + "':'" + str(Parameters[x]) + "'")
	Domoticz.Debug("Device count: " + str(len(Devices)))
	for x in Devices:
		Domoticz.Debug("Device:		   " + str(x) + " - " + str(Devices[x]))
		Domoticz.Debug("Device ID:	   '" + str(Devices[x].ID) + "'")
		Domoticz.Debug("Device Name:	 '" + Devices[x].Name + "'")
		Domoticz.Debug("Device nValue:	" + str(Devices[x].nValue))
		Domoticz.Debug("Device sValue:   '" + Devices[x].sValue + "'")
		Domoticz.Debug("Device LastLevel: " + str(Devices[x].LastLevel))
	return


How does Python Framework work in Domoticz ?
I try in command line to import the module, it's work ...

thank's for your help
User avatar
Dnpwwo
Posts: 820
Joined: Sunday 23 March 2014 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: Python Plugin: import Module

Post by Dnpwwo »

@zaraki673,

I had a look at the script and it looks okay (I'm not a Python expert even though I wrote the framework) and as you have said it is complaining about the bluepy import which implies that it has found and loaded the plugin itself.

There should be another message right above the 2 errors you posted that shows the Python path that will be searched for imports, if you have that can you confirm that the bluepy library is in one of those folders or in the same folder as the plugin?
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
zaraki673
Posts: 32
Joined: Thursday 19 January 2017 23:46
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: Python Plugin: import Module

Post by zaraki673 »

:oops:
you're right

Code: Select all

pip3 install bluepy
install bluepy in /usr/local/lib/python3.4/dist-packages/bluepy/
but python framework search in /usr/lib/python3.4/

i just do

Code: Select all

sudo cp -r /usr/local/lib/python3.4/dist-packages/bluepy/ /usr/lib/python3.4/
and now it's load :D

thank's ;)
deennoo
Posts: 784
Joined: Wednesday 10 December 2014 13:06
Target OS: Linux
Domoticz version: beta
Location: Bordeaux France
Contact:

Re: Python Plugin: import Module

Post by deennoo »

@Dnpwwo

Did you plan to introduce a dependency installer, or a install script to automatise installation of some dependency ?

I have idea to make a plugin for Yeelight Bt Bedside lamp, found a python Lib for it but need to be installed : https://github.com/rytilahti/python-yeelightbt and it use bluepy and bluez.
Domoticz stable 3.5877 for real & Domoticz beta for test
Rfxtrxe / RFLink / Milight / Yeelight / Tasmota / MQTT / BLE / Zigate
http://domo-attitude.fr
User avatar
Dnpwwo
Posts: 820
Joined: Sunday 23 March 2014 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: Python Plugin: import Module

Post by Dnpwwo »

@deenoo,

I wasn't planning on doing this because it shouldn't be required.

I haven't done it but my understanding is that if you put the import target in the same directory as the plugin (or anywhere in the Python Path) it should be found. This is standard Python functionality and not something I have done.

Can you try it and report back?
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest