Python Plugin: Creating devices using TypeName

Python and python framework

Moderator: leecollings

Post Reply
BakSeeDaa
Posts: 485
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version:

Python Plugin: Creating devices using TypeName

Post by BakSeeDaa »

Hi! Running Domoticz 3.3364

The following code fails:

The following code as described by the wiki using the named argument TypeName

Code: Select all

Domoticz.Device(Name="Indoor TempHum",  Unit=1, TypeName="Temp+Hum").Create()
fails with the following error:

Code: Select all

2017-01-18 16:58:59.227 Error: (WH2600) 'CDevice_init' failed 'TypeError':''TypeName' is an invalid keyword argument for this function'.
2017-01-18 16:58:59.227 Error: Expected: myVar = Domoticz.Device(Name="myDevice", Unit=0, Type=0, Subtype=0, Switchtype=0, Image=0, Options="")
Maybe I need to use the numerical Type instead. I tried to do that but then the created devices where missing subtype attributes when listing them in the GUI. I'm trying to create devices for the types "Temp+Hum", "Barometer", "Rain", "Wind", "UV", "Solar Radiation" and "Alert". I don't know if they have numerical subtypes that I can specify.
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: Creating devices using TypeName

Post by Dnpwwo »

@BakSeeDaa,

The TypeName parameter was added in the last pull request I did and that was only merged 5 days ago, can you confirm that you are running a more recent beta than that?

I tend to update the wiki as I make changes so sometimes the wiki doco is slightly ahead, but not in this case.

Edit: I checked github and the code is there from 4 days ago, the error message has changed to include TypeName so you have an old version. Given that TypeName supports all the device types you want I would recommend upgrading rather that working out the Types and SubTypes manually.

The built in HTTP support is still rudimentary and buggy, I'm working on it at the moment.
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
BakSeeDaa
Posts: 485
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: Python Plugin: Creating devices using TypeName

Post by BakSeeDaa »

Dnpwwo wrote:@BakSeeDaa,

The TypeName parameter was added in the last pull request I did and that was only merged 5 days ago, can you confirm that you are running a more recent beta than that?
Thanks a lot @Dnpwwo :D

I'm now on 3.6371 and it worked much better now!

The Barometer type did not work though. A device number was skipped but no device was created.

Using the code below:

Code: Select all

		if (len(Devices) == 0):
			Domoticz.Device(Name="Indoor TempHum", Unit=1, TypeName="Temp+Hum").Create()
			Domoticz.Device(Name="Outdoor TempHum", Unit=2, TypeName="Temp+Hum").Create()
			Domoticz.Device(Name="Barometer", Unit=3, TypeName="Barometer").Create()
			Domoticz.Device(Name="Rain", Unit=4, TypeName="Rain").Create()
			Domoticz.Device(Name="Wind", Unit=5, TypeName="Wind").Create()
			Domoticz.Device(Name="UV", Unit=6, TypeName="UV").Create()
			Domoticz.Device(Name="Solar Radiation", Unit=7, TypeName="Solar Radiation").Create()
			Domoticz.Device(Name="Battery", Unit=8, TypeName="Alert").Create()
			Domoticz.Log("Devices created.")
The Python Plugin Framework (Is it named like that?) is great news for Domoticz due to all the 3rd party plugins that will be available. The potential is so great. But shouldn't we have a dedicated sub forum here for it? It would make it easier to see if a question has already been asked (and answered) before.

Cheers!
BakSeeDaa
Posts: 485
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: Python Plugin: Creating devices using TypeName

Post by BakSeeDaa »

I dump another question here... (I hope that's OK)

The piece of hardware that I'm connecting to (WH2600 Weather Station) has a web interface and I could use that to scrape data. However I can also set up the WH2600 to connect to any host at any port and dump its weather data there. That is, I can set it to opening an URL at my Raspberry Pi and all the weather data is passed as URL args.

(For the moment I've built a python listener that takes care of the data and passing it on to Domoticz using the Domoticz URL API. My listener also passes the data further on to Weather Underground.)

Now I'm investigating if the Python Plugin Framework can be set up to listen for messages from the WH2600. That is, Domoticz will not initiate the connection. Domoticz should just wait for a connection from WH2600 and then I would like to receive the data in

Code: Select all

onMessage(Data, Status, Extra):
Would that be possible or am I trying to do something that the Python Plugin Framework is not intended to do?

Thanks for Your time @Dnpwwo
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: Creating devices using TypeName

Post by Dnpwwo »

@BakSeeDaa,

I was surprised about the barometer not working because I copied the Dummy Hardware implementation. I will have a look at it.

Edit: The barometer device is actually there, try

Code: Select all

Domoticz.Update(0,"1000.00;0")
and it should show up. I think there is some logic somewhere in Domoticz that fails if it doesn't have an sValue set.

The framework does not current support listening for connections but it sounds like a good idea and I think it would be easy to add. I'll look at creating a 'Domoticz.Listen' function similar to the 'Domoticz.Connect'. The other call backs should work as expected.
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
BakSeeDaa
Posts: 485
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: Python Plugin: Creating devices using TypeName

Post by BakSeeDaa »

Dnpwwo wrote:@BakSeeDaa,

I was surprised about the barometer not working because I copied the Dummy Hardware implementation. I will have a look at it.

Edit: The barometer device is actually there, try

Code: Select all

Domoticz.Update(0,"1000.00;0")
and it should show up. I think there is some logic somewhere in Domoticz that fails if it doesn't have an sValue set.
You were right about that. It's there now. :D
Dnpwwo wrote:The framework does not current support listening for connections but it sounds like a good idea and I think it would be easy to add. I'll look at creating a 'Domoticz.Listen' function similar to the 'Domoticz.Connect'.
That would be awesome @Dnpwwo and I'd be very excited to try it out if You succeed adding it.

Is there a way that I can donate a couple of beers to show my appreciation for your efforts in this project :?:
BakSeeDaa
Posts: 485
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: Python Plugin: Creating devices using TypeName

Post by BakSeeDaa »

Another thing that I came to think of...

I like the following code from the example projects:

Code: Select all

def UpdateDevice(Unit, nValue, sValue):
    # Make sure that the Domoticz device still exists (they can be deleted) before updating it 
    if (Unit in Devices):
        if (Devices[Unit].nValue != nValue) or (Devices[Unit].sValue != sValue):
            Devices[Unit].Update(nValue, str(sValue))
            Domoticz.Log("Update "+str(nValue)+":'"+str(sValue)+"' ("+Devices[Unit].Name+")")
    return
It makes an update to the Domoticz device only if there is an actual change. I believe that's good because Domoticz gets a lot of work to do every time a device is updated. But some devices like the "Solar Radiation" device will turn red in the Domoticz GUI unless it isn't updated at least once an hour. These devices have the attribute "HaveTimeout" set to true. It would be nice to be able to access the Domoticz.Device[n].LastUpdate and Domoticz.Device[n].HaveTimeout to be able to implement something like this (code from another of my projects):

Code: Select all

	# Now, looking for a reason to update the sensor
	# Does the Domotic's sensor need an update in order not to time out?
	sensorTimedOut = False
	if 'HaveTimeout' in r['result'][0]:
		if (r['result'][0]['HaveTimeout'] and ((datetime.now() - datetime.strptime(r['result'][0]['LastUpdate'], '%Y-%m-%d %H:%M:%S')).seconds >= 3000)):
			sensorTimedOut = True
Cheers!
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: Python Plugin: Creating devices using TypeName

Post by G3rard »

BakSeeDaa wrote: The Python Plugin Framework (Is it named like that?) is great news for Domoticz due to all the 3rd party plugins that will be available. The potential is so great.
I totally agree with that!
BakSeeDaa wrote: But shouldn't we have a dedicated sub forum here for it? It would make it easier to see if a question has already been asked (and answered) before.
I second that. There already is a Python subforum, but I think this Python plugin should have it's own sub forum to keep things clean.
Not using Domoticz anymore
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest