2017-06-09, 16:20
Hey guys, I want to add a pyXBMCt script but I cannot find this at all. From what I can tell, it used to be part of Gothem? Im new to the whole thing but why dont I have the official Kodi repo? Is it even on it?
(2017-06-09, 17:21)Roman_V_M Wrote: Library addons are not visible in the Kodi Addon Manager (although they used to be and I think it was a mistake to remove this feature). More info about PyXBMCt you can find in this thread: https://forum.kodi.tv/showthread.php?tid=174859 Please carefully read all the available documentation before using PyXBMCt.
(2017-06-09, 22:42)el_Paraguayo Wrote: You need to learn a bit of python and how to use the module. I won't do it all for you as you'll learn nothing.
Have a read of the documentation. You should get an idea of how to change labels and add actions.
Once you've done that, I will help you link this to your gpio pins.
import xbmcaddon
import xbmcgui
import os
import xbmc
import sys
sys.path.append('/storage/.kodi/addons/script.module.pyxbmct/lib')
import pyxbmct
# Import PyXBMCt module.
import pyxbmct
_addon = xbmcaddon.Addon()
_path = _addon.getAddonInfo("path")
_check_icon = os.path.join(_path, "check.png") # Don't decode _path to utf-8!!!
class MultiChoiceDialog(pyxbmct.AddonDialogWindow):
def __init__(self, title="", items=None):
super(MultiChoiceDialog, self).__init__(title)
self.setGeometry(450, 300, 4, 4)
self.selected = []
self.set_controls()
self.connect_controls()
self.listing.addItems(items or [])
self.set_navigation()
def set_controls(self):
self.listing = pyxbmct.List(_imageWidth=15)
self.placeControl(self.listing, 0, 0, rowspan=3, columnspan=4)
self.ok_button = pyxbmct.Button("OK")
self.placeControl(self.ok_button, 3, 1)
self.cancel_button = pyxbmct.Button("Cancel")
self.placeControl(self.cancel_button, 3, 2)
def connect_controls(self):
self.connect(self.listing, self.check_uncheck)
self.connect(self.ok_button, self.ok)
self.connect(self.cancel_button, self.close)
def set_navigation(self):
self.listing.controlUp(self.ok_button)
self.listing.controlDown(self.ok_button)
self.ok_button.setNavigation(self.listing, self.listing, self.cancel_button, self.cancel_button)
self.cancel_button.setNavigation(self.listing, self.listing, self.ok_button, self.ok_button)
if self.listing.size():
self.setFocus(self.listing)
else:
self.setFocus(self.cancel_button)
def check_uncheck(self):
list_item = self.listing.getSelectedItem()
if list_item.getLabel2() == "checked":
list_item.setIconImage("")
list_item.setLabel2("unchecked")
else:
list_item.setIconImage(_check_icon)
list_item.setLabel2("checked")
def ok(self):
self.selected = [index for index in xrange(self.listing.size())
if self.listing.getListItem(index).getLabel2() == "checked"]
super(MultiChoiceDialog, self).close()
def close(self):
self.selected = []
super(MultiChoiceDialog, self).close()
if __name__ == "__main__":
items = ["Relay {0}".format(i) for i in xrange(1, 11)]
dialog = MultiChoiceDialog("Activate/deactivate", items)
dialog.doModal()
xbmcgui.Dialog().notification("Finished", "Selected: {0}".format(str(dialog.selected)))
del dialog #You need to delete your instance when it is no longer needed
#because underlying xbmcgui classes are not grabage-collected.
sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib')
import RPi.GPIO as GPIO
# Set pin number once
RELAY_PIN = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(RELAY_PIN, GPIO.OUT)
# read the current state of the pin
current_state = GPIO.input(RELAY_PIN)
if current_state == GPIO.HIGH:
# Pin is on so we should turn off
GPIO.output(RELAY_PIN, GPIO.LOW)
state = "off"
else:
# Pin is off so we should turn on
GPIO.output(RELAY_PIN, GPIO.HIGH)
state = "on"
# Send notification
xbmcgui.Dialog().notification("Relay", "Relay is now {0}!".format(state))
addon = xbmcaddon.Addon()
addonname = addon.getAddonInfo('name')
line1 = "Turned on/off a relay"
line2 = "Version: " + addon.getAddonInfo('version')
os.system("sh /storage/.kodi/addons/relay-addon-kodi-master/relay.sh")
xbmc.executebuiltin('Notification(Relay Addon, The relay was turned on/off,5000,//storage/.kodi/addons/script.relay.master/icon.png)')
#xbmcgui.Dialog().ok(addonname, line1, line2)
import xbmcaddon
import xbmcgui
import os
import xbmc
import sys
sys.path.append('/storage/.kodi/addons/script.module.pyxbmct/lib')
import pyxbmct
sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib')
import RPi.GPIO as GPIO
RELAYS = [("Relay 1", 13),
("Relay 2", 18),
("Relay 3", 22),
("Relay 4", 25)]
_addon = xbmcaddon.Addon()
_path = _addon.getAddonInfo("path")
_check_icon = os.path.join(_path, "check.png") # Don't decode _path to utf-8!!!
class MultiChoiceDialog(pyxbmct.AddonDialogWindow):
def __init__(self, title="", items=None):
super(MultiChoiceDialog, self).__init__(title)
GPIO.setmode(GPIO.BCM)
self.item_labels = [x[0] for x in items]
self.item_pins = [x[1] for x in items]
GPIO.setup(self.item_pins, GPIO.OUT)
self.setGeometry(450, 300, 4, 4)
self.selected = []
self.set_controls()
self.connect_controls()
self.listing.addItems(self.item_labels or [])
self.get_initial_state()
self.set_navigation()
def get_initial_state(self):
for index, pin in enumerate(self.item_pins):
if GPIO.input(pin):
item = self.listing.getListItem(index)
self.toggle_relay(item, update=False)
def set_controls(self):
self.listing = pyxbmct.List(_imageWidth=15)
self.placeControl(self.listing, 0, 0, rowspan=3, columnspan=4)
self.ok_button = pyxbmct.Button("Finished")
self.placeControl(self.ok_button, 3, 1, columnspan=2)
def connect_controls(self):
self.connect(self.listing, self.toggle_relay)
self.connect(self.ok_button, self.ok)
def set_navigation(self):
self.listing.controlUp(self.ok_button)
self.listing.controlDown(self.ok_button)
self.ok_button.setNavigation(self.listing, self.listing, self.ok_button, self.ok_button)
if self.listing.size():
self.setFocus(self.listing)
else:
self.setFocus(self.cancel_button)
def toggle_relay(self, item=None, update=True):
if item is None:
list_item = self.listing.getSelectedItem()
pos = self.listing.getSelectedPosition()
else:
list_item = item
pos = -1
if list_item.getLabel2() == "checked":
list_item.setIconImage("")
list_item.setLabel2("unchecked")
if update and pos >= 0:
GPIO.output(self.item_pins[pos], GPIO.LOW)
else:
list_item.setIconImage(_check_icon)
list_item.setLabel2("checked")
if update and pos >= 0:
GPIO.output(self.item_pins[pos], GPIO.HIGH)
def ok(self):
super(MultiChoiceDialog, self).close()
if __name__ == "__main__":
dialog = MultiChoiceDialog("Activate/deactivate", RELAYS)
dialog.doModal()
del dialog #You need to delete your instance when it is no longer needed
(2017-06-12, 20:06)el_Paraguayo Wrote: Try this:
Code:import xbmcaddon
import xbmcgui
import os
import xbmc
import sys
sys.path.append('/storage/.kodi/addons/script.module.pyxbmct/lib')
import pyxbmct
sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib')
import RPi.GPIO as GPIO
RELAYS = [("Relay 1", 13),
("Relay 2", 18),
("Relay 3", 22),
("Relay 4", 25)]
_addon = xbmcaddon.Addon()
_path = _addon.getAddonInfo("path")
_check_icon = os.path.join(_path, "check.png") # Don't decode _path to utf-8!!!
class MultiChoiceDialog(pyxbmct.AddonDialogWindow):
def __init__(self, title="", items=None):
super(MultiChoiceDialog, self).__init__(title)
GPIO.setmode(GPIO.BCM)
self.item_labels = [x[0] for x in items]
self.item_pins = [x[1] for x in items]
GPIO.setup(self.item_pins, GPIO.OUT)
self.setGeometry(450, 300, 4, 4)
self.selected = []
self.set_controls()
self.connect_controls()
self.listing.addItems(self.item_labels or [])
self.get_initial_state()
self.set_navigation()
def get_initial_state(self):
for index, pin in enumerate(self.item_pins):
if GPIO.input(pin):
item = self.listing.getListItem(index)
self.toggle_relay(item, update=False)
def set_controls(self):
self.listing = pyxbmct.List(_imageWidth=15)
self.placeControl(self.listing, 0, 0, rowspan=3, columnspan=4)
self.ok_button = pyxbmct.Button("Finished")
self.placeControl(self.ok_button, 3, 1, columnspan=2)
def connect_controls(self):
self.connect(self.listing, self.toggle_relay)
self.connect(self.ok_button, self.ok)
def set_navigation(self):
self.listing.controlUp(self.ok_button)
self.listing.controlDown(self.ok_button)
self.ok_button.setNavigation(self.listing, self.listing, self.ok_button, self.ok_button)
if self.listing.size():
self.setFocus(self.listing)
else:
self.setFocus(self.cancel_button)
def toggle_relay(self, item=None, update=True):
if item is None:
list_item = self.listing.getSelectedItem()
pos = self.listing.getSelectedPosition()
else:
list_item = item
pos = -1
if list_item.getLabel2() == "checked":
list_item.setIconImage("")
list_item.setLabel2("unchecked")
if update and pos >= 0:
GPIO.output(self.item_pins[pos], GPIO.LOW)
else:
list_item.setIconImage(_check_icon)
list_item.setLabel2("checked")
if update and pos >= 0:
GPIO.output(self.item_pins[pos], GPIO.HIGH)
def ok(self):
super(MultiChoiceDialog, self).close()
if __name__ == "__main__":
dialog = MultiChoiceDialog("Activate/deactivate", RELAYS)
dialog.doModal()
del dialog #You need to delete your instance when it is no longer needed
If you look near the top, you'll see a variable called RELAYS. This is where you define your relays. Each item in the list is a "tuple": the first item is the name of the relay (i.e what shows on the screen), the second item is the GPIO pin number.
When the script runs, it will check which relays are already on and will set the "checked" flag accordingly.
Clicking on an item toggles the relay on and off.
Click "Finished" when you're done.
Let me know if it works. I've done it on my laptop so can't test it.