Page 1 of 1

Plugin Python :how update SignalLevel and BatteryLevel (and custumImage)

Posted: Tuesday 21 March 2017 11:19
by zaraki673
Hi everybody!!

For a plugin, i need to update the BatteryLevel to devices in domoticz
To do it, i have update the UpdateDevice code like that :
before :

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
after :

Code: Select all

def UpdateDeviceWithBattery(Unit, nValue, sValue, CustomImage, SignalLevel, BatteryLevel):
	# 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) or (Devices[Unit].SignalLevel != SignalLevel) or (Devices[Unit].BatteryLevel != BatteryLevel) :
			Devices[Unit].Update(nValue, str(sValue),CustomImage, SignalLevel, BatteryLevel)
			Domoticz.Log("Update "+str(nValue)+":'"+str(sValue)+"' SignalLevel:"+str(SignalLevel)+" batteryLevel:'"+str(BatteryLevel)+"%' ("+Devices[Unit].Name+")")
	return
As you can see, i also update the CustumImage attribut, but i didn't find how to get it back from domoticz's DB, so i can't check if the value have change :cry:

If anyone know how to get it ...

thk's

Re: Plugin Python :how update SignalLevel and BatteryLevel (and custumImage)

Posted: Wednesday 22 March 2017 2:23
by Dnpwwo
@zaraki673,

At the end of the Device.Update() call the python framework reloads all the device details from the database for you and refreshes the Device object contents. This means that if the Device[Unit].Image is set to what you want, the database update was successful (although Device.Update() will return an error if it fails anyway).

If you are really worried you can call Device[Unit].Refresh() inside your plugin to manually force a device refresh but it shouldn't be required because all updates should be driven by the plugin itself.

Re: Plugin Python :how update SignalLevel and BatteryLevel (and custumImage)

Posted: Thursday 23 March 2017 17:15
by zaraki673
Sorry to answer so late,

Thank you for your answer, I undestand what you said, the only think i miss was 'Device[Unit].image' as customImage (name in DB).

now all seem to work fine :D
thks