How to check that some devices have been created some not in a plugin

Python and python framework

Moderator: leecollings

Post Reply
darrepac
Posts: 142
Joined: Tuesday 04 February 2014 21:31
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Isère, France
Contact:

How to check that some devices have been created some not in a plugin

Post by darrepac »

Hi

I am just starting to put my hands on plugin in order to update a plugin. I read the wiki and some others plugin but did not understand how the devices creation work precisely particularly when you want to update the plugin:
I understood this from wiki:

Code: Select all

import Domoticz
 #
 def onStart():
   if (len(Devices) == 0):
       Domoticz.Device(Name="Status",  Unit=1, Type=17,  Switchtype=17).Create()
But if I want to create 1 device more with the same plugin without removing the already created devices, how can I do?
thx
User avatar
waltervl
Posts: 7018
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2026.3
Location: NL
Contact:

Re: How to check that some devices have been created some not in a plugin

Post by waltervl »

In the following plugin there is an example how to handle this. https://github.com/mvzut/maxcube-Domoti ... /plugin.py
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
darrepac
Posts: 142
Joined: Tuesday 04 February 2014 21:31
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Isère, France
Contact:

Re: How to check that some devices have been created some not in a plugin

Post by darrepac »

not obvious to me but will take a 2nd look
User avatar
waltervl
Posts: 7018
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2026.3
Location: NL
Contact:

Re: How to check that some devices have been created some not in a plugin

Post by waltervl »

This part will check what type devices are supposed to be there and if new ones should be created (or even deleted, depends on plugin settings)

Code: Select all

 def CheckDevice(self, name, deviceid, typename):

        # Initialization of variables for device creation

        switchtype = 0

        image = 0

        options = {"LevelActions": "|||", 

                   "LevelNames": "Auto|Manual|Vacation|Boost",

                   "LevelOffHidden": "false",

                   "SelectorStyle": "0"}        

        if typename == "Valve":

            devicetype = 243

            subtype = 6

        elif typename == "Thermostat":

            devicetype = 242

            subtype = 1

        elif typename == "Temperature":

            devicetype = 80

            subtype = 5

        elif typename == "Mode":

            devicetype = 244

            subtype = 62

            switchtype = 18

            image = 15

        elif typename == "Contact":

            devicetype = 244

            subtype = 73

            switchtype = 2



        # Check if device is wanted

        DeviceWanted = True

        if Parameters["Mode1"] == "False" and typename == "Valve" or Parameters["Mode2"] == "False" and typename == "Mode":

            DeviceWanted = False



        # Check if device with given deviceid and devicetype is already present

        DeviceFound = False

        for Device in Devices:

            if Devices[Device].DeviceID == deviceid and Devices[Device].Type == devicetype:

                DeviceFound = True

                # Delete found device if not wanted anymore

                if not DeviceWanted:

                    Domoticz.Log("Deleted device " + Devices[Device].Name)

                    Devices[Device].Delete()

                    break



        # If device not found but wanted, create it

        if not DeviceFound and DeviceWanted:

            # Check first available unit ID

            availableID = 1

            if Devices:

                for device in Devices:

                    if device > availableID: break

                    else: availableID += 1

            # Create device

            Domoticz.Device(Name=name + " - " + typename, Unit=availableID, DeviceID=deviceid, Type=devicetype, Subtype=subtype, Switchtype=switchtype, Options=options, Image = image, Used=1).Create()

            if availableID in Devices:

                # Device created

                Domoticz.Log("Created device '" + Parameters["Name"] + " - " + name + " - " + typename + "'")

            else:

                # Device not created!

                Domoticz.Error("Device '" + Parameters["Name"] + " - " + name + " - " + typename + "' could not be created. Is 'Accept new Hardware Devices' enabled under Settings?")

Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
darrepac
Posts: 142
Joined: Tuesday 04 February 2014 21:31
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Isère, France
Contact:

Re: How to check that some devices have been created some not in a plugin

Post by darrepac »

Thanks I eventually succeeded
Post Reply