pythonpython-3.7philips-hue

Ip address changing to an integer


I am making a script that will ping an ip address using the subprocess module. But when I try to run it, it says:

Traceback (most recent call last):
  File "C:\Users\YUZi5\OneDrive\Desktop\huetest.py", line 99, in <module>
    subprocess.run("ping", int(hhip), "/dev/null", capture_output=True) #doesnt work rn
ValueError: invalid literal for int() with base 10: '192.168.1.109'

I have looked at some other methods to do this but it doesn't work. Could someone help please. The full code is here:

from phue import Bridge
import subprocess


# https://github.com/studioimaginaire/phue
# in and for meaning: https://www.quora.com/In-Python-what-does-for-return-and-in-mean
# https://stackoverflow.com/questions/19845924/python-how-to-generate-list-of-variables-with-new-number-at-end
# the hub 192.168.1.107
while True:
    b = Bridge('192.168.1.107')

    b.connect() 
    b.get_api()

    b.get_light(1, 'on')
    b.get_light(1, 'name')

    lights = b.lights

    print ("NOTICE: Only 1 light is supported in this script more will be allowed in the future.")
    print ("This script is highly work in progress, contact Yusef (YUZi22) on Github if any errors occur")
    print()
    print ("Please make sure to input the local ip address of your hue hub, and press the button on it before you run this script")
    print()

    # Print names of all lights connected to hue hub.
    for l in lights:
        print ("Lights:")
        print(l.name)
        print()

    lselect = input("""Select Light (Top to bottom, has to be a number. Goes like this: light on top of list starts with 1 and as it goes down it will go 2,3,4,5 etc..)

Type here: """)


    if lselect == ('1'):
        print ("Light 1 selected")
        print()
        print()
        operation = input("""Select Operation:
1. Brightness 
2. Colour select
3. Turn off
4. Turn on
5. Brightness percentage
6. Hue hub connection test

(1 - 5)

Type here: """)

        select100per = ('255')
        select75per = ('191')
        select60per = ('153')
        select50per = ('127')
        select25per = ('63') #working on adding percentage brightnesses, i will work on it tmrw aswell

        if operation == ('1'):
            brightsel = input("Input a brightness value (0-255): ")
            brightop = l.brightness = int(brightsel)
            print ("Set brightness of", (l.name), "to", "Brightness:", (brightsel))

        if operation == ('2'):
            colhue = input("Set hue: ")
            colop = b.hue = int(colhue)
            colsat = input("Set saturation: ")
            colsaop = b.saturation = int(colsat)
            print ("Saturation set to", (colsat), "and hue set to", (colhue), "on", (l.name))

        if operation == ('3'):
            b.set_light([(l.name)], 'on', False)
            print ("Turned", (l.name), "off")

        if operation == ('4'):
            b.set_light([(l.name)], 'on', True)
            print ("Turned", (l.name), "on")

        if operation == ('5'):
            prcnt = input("What percentage of brightness do you want? 100%, 75%, 60%, 50% or 25% (More percentages will be added soon): ")
            if prcnt == ('100%'):
                cmdexec = l.brightness = int(select100per)
                print ("Set", (l.name), "to 100%")            
            if prcnt == ('75%'):
                cmdexec = l.brightness = int(select75per)
                print ("Set", (l.name), "to 75%")
            if prcnt == ('60%'):
                cmdexec = l.brightness = int(select60per)
                print ("Set", (l.name), "to 60%")
            if prcnt == ('50%'):
                cmdexec = l.brightness = int(select50per)
                print ("Set", (l.name), "to 50%")
            if prcnt == ('25%'):
                cmdexec = l.brightness = int(select25per)
                print ("Set", (l.name), "to 25%")

        if operation == ('6'):
            hhip = input("Please input the ip address of your philips hue hub: ")
            subprocess.run("ping", int(hhip), "/dev/null", capture_output=True) #doesnt work rn

        restart = input("Would you like to restart the script? (y/n): ")
        if restart == ('n'):
            ques = input("Are you sure? (y/n): ")
            if ques == ('y'):
                break
            if ques == ('n'):
                continue

Look at if operation == ('6') to see the specific part i'm trying to add.


Solution

  • You need to pass the arguments as a list in subprocess.run method. So your line

    subprocess.run("ping", int(hhip), "/dev/null", capture_output=True)
    

    should be changed to

    subprocess.run(["ping", hhip, "/dev/null"], capture_output=True)