studioimaginaire / phue

A Python library for the Philips Hue system
Other
1.52k stars 267 forks source link

Trying to scrape bridge's IP from api to a .txt and then use it to connect with phue. No success. #153

Closed jpcst closed 4 years ago

jpcst commented 5 years ago

This is the current code:

from tkinter import *
from phue import Bridge
import requests

try:
    with open('C:/Scrape/hue.txt','r') as f:
        b = Bridge(f.read()) # Scrapa o ip do .txt no arg
        b.connect()
    #print('try') # Clareza

except:
    ip = requests.get('https://www.meethue.com/api/nupnp').json()[0]['internalipaddress'] # Scrapa o ip da API
    with open('C:/Scrape/hue.txt','w') as f:
        f.write(ip) # Salva o ip em hue.txt
    b = Bridge(ip)
    b.connect()
    #print('except') # Clareza
.
.
.

I can't tell why this doesn't work, if for any reason the IP is changed, I must go to the file 'hue.txt' and change it manually. Any help is appreciated.

landonstewart commented 5 years ago

I tried your code with few minor mods (and on macOS in a python3 virtualenv) and got this:

Deleted the file to be sure: $ rm hue.txt

Ran it the first time:

$ ./test.py
[Errno 2] No such file or directory: 'hue.txt'
Finding IP from meethue.
192.168.1.197

Ran it a second time:

(test)Landons-iMac:test lstewart$ ./test.py
Read from hue.txt.
192.168.1.197

Here's the modified code:

#!/usr/bin/env python

from phue import Bridge
import requests

try:
    with open('hue.txt', 'r') as f:
        b = Bridge(f.read())  # Scrapa o ip do .txt no arg
        b.connect()
        print('Read from hue.txt.')
except Exception as read_err:
    print(str(read_err))
    print('Finding IP from meethue.')
    ip = requests.get('https://www.meethue.com/api/nupnp') \
        .json()[0]['internalipaddress']  # Scrapa o ip da API
    with open('hue.txt', 'w') as f:
        f.write('{}'.format(ip))  # Salva o ip em hue.txt
        b = Bridge(ip)
        b.connect()
else:
    pass
finally:
    with open('hue.txt', 'r') as f:
        print(f.read())
jpcst commented 5 years ago

Thank you for your time, I think it will work now for when the ip changes. Have a nice day.

jpcst commented 5 years ago

Hello, unfortunately the new code is not working, the IP of the bridge changed, and now this is what I get as an error:

Read from hue.txt.
192.168.33.101
GET Request to 192.168.33.101/api/6rlakf3YjUJ25dNxiLlouIJxV7A1IGYhRrn4B9ai/lights/ timed out.
Traceback (most recent call last):
  File "C:\Python\lib\site-packages\phue.py", line 651, in request
    connection.request(mode, address)
  File "C:\Python\lib\http\client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Python\lib\http\client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Python\lib\http\client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Python\lib\http\client.py", line 1026, in _send_output
    self.send(msg)
  File "C:\Python\lib\http\client.py", line 964, in send
    self.connect()
  File "C:\Python\lib\http\client.py", line 936, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "C:\Python\lib\socket.py", line 722, in create_connection
    raise err
  File "C:\Python\lib\socket.py", line 713, in create_connection
    sock.connect(sa)
socket.timeout: timed out
Traceback (most recent call last):
  File "C:\Python\lib\site-packages\phue.py", line 651, in request
    connection.request(mode, address)
  File "C:\Python\lib\http\client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Python\lib\http\client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Python\lib\http\client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Python\lib\http\client.py", line 1026, in _send_output
    self.send(msg)
  File "C:\Python\lib\http\client.py", line 964, in send
    self.connect()
  File "C:\Python\lib\http\client.py", line 936, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "C:\Python\lib\socket.py", line 722, in create_connection
    raise err
  File "C:\Python\lib\socket.py", line 713, in create_connection
    sock.connect(sa)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/João Pedro Costa/Documents/Python/ /hue2.pyw", line 44, in <module>
    lights = b.get_light_objects('id')
  File "C:\Python\lib\site-packages\phue.py", line 771, in get_light_objects
    lights = self.request('GET', '/api/' + self.username + '/lights/')
  File "C:\Python\lib\site-packages\phue.py", line 661, in request
    raise PhueRequestTimeout(None, error)
phue.PhueRequestTimeout: (None, 'GET Request to 192.168.33.101/api/6rlakf3YjUJ25dNxiLlouIJxV7A1IGYhRrn4B9ai/lights/ timed out.')

Process finished with exit code 1

It looks like the code is trying to use the ip from the txt without scraping and changing it first, any suggestions now? Just like before, I still need to change the ip manually.

natcl commented 5 years ago

Do you have your full code ? This doesn't seem related to the actual library...

-- lecaude.com studioimaginaire.com

Le 17 avr. 2019 à 15:40, jpcst notifications@github.com a écrit :

Hello, unfortunately the new code is not working, the IP of the bridge changed, and now this is what I get as an error:

Read from hue.txt. 192.168.33.101 GET Request to 192.168.33.101/api/6rlakf3YjUJ25dNxiLlouIJxV7A1IGYhRrn4B9ai/lights/ timed out. Traceback (most recent call last): File "C:\Python\lib\site-packages\phue.py", line 651, in request connection.request(mode, address) File "C:\Python\lib\http\client.py", line 1239, in request self._send_request(method, url, body, headers, encode_chunked) File "C:\Python\lib\http\client.py", line 1285, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "C:\Python\lib\http\client.py", line 1234, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "C:\Python\lib\http\client.py", line 1026, in _send_output self.send(msg) File "C:\Python\lib\http\client.py", line 964, in send self.connect() File "C:\Python\lib\http\client.py", line 936, in connect (self.host,self.port), self.timeout, self.source_address) File "C:\Python\lib\socket.py", line 722, in create_connection raise err File "C:\Python\lib\socket.py", line 713, in create_connection sock.connect(sa) socket.timeout: timed out Traceback (most recent call last): File "C:\Python\lib\site-packages\phue.py", line 651, in request connection.request(mode, address) File "C:\Python\lib\http\client.py", line 1239, in request self._send_request(method, url, body, headers, encode_chunked) File "C:\Python\lib\http\client.py", line 1285, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "C:\Python\lib\http\client.py", line 1234, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "C:\Python\lib\http\client.py", line 1026, in _send_output self.send(msg) File "C:\Python\lib\http\client.py", line 964, in send self.connect() File "C:\Python\lib\http\client.py", line 936, in connect (self.host,self.port), self.timeout, self.source_address) File "C:\Python\lib\socket.py", line 722, in create_connection raise err File "C:\Python\lib\socket.py", line 713, in create_connection sock.connect(sa) socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:/Users/João Pedro Costa/Documents/Python/ /hue2.pyw", line 44, in lights = b.get_light_objects('id') File "C:\Python\lib\site-packages\phue.py", line 771, in get_light_objects lights = self.request('GET', '/api/' + self.username + '/lights/') File "C:\Python\lib\site-packages\phue.py", line 661, in request raise PhueRequestTimeout(None, error) phue.PhueRequestTimeout: (None, 'GET Request to 192.168.33.101/api/6rlakf3YjUJ25dNxiLlouIJxV7A1IGYhRrn4B9ai/lights/ timed out.')

Process finished with exit code 1```

It looks like the code is trying to use the ip from the txt without scraping and changing it first, any suggestions now? — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

jpcst commented 5 years ago

Here's the code:

from tkinter import *
from phue import Bridge
import requests

try:
    with open('C:/Scrape/hue.txt', 'r') as f:
        b = Bridge(f.read())  # Scrapa o ip do .txt no arg
        b.connect()
        print('Read from hue.txt.')

except Exception as read_err:
    print(str(read_err))
    print('Finding IP from meethue.')
    ip = requests.get('https://www.meethue.com/api/nupnp') \
        .json()[0]['internalipaddress']  # Scrapa o ip da API
    with open('C:/Scrape/hue.txt', 'w') as f:
        f.write('{}'.format(ip))  # Salva o ip em hue.txt
        b = Bridge(ip)
        b.connect()
else:
    pass
finally:
    with open('C:/Scrape/hue.txt', 'r') as f:
        print(f.read())

root = Tk()
horizontal_frame = Frame(root)
horizontal_frame.pack()

lights = b.get_light_objects('id')

def tres():
    b.set_light(3, 'on', True, transitiontime=0)
    lights[3].brightness=254
    #luzes = b.get_light_objects()
    for luz in b.get_light_objects(): # = luzes
        luz.xy=[.3, .3]

def off():
    b.set_light([1,2,3,4], 'on', False, transitiontime=0) # Lista de luzes = [1,2,3,4]

def cor():
    b.set_light(1, 'on', True, transitiontime=0)
    lights[3].brightness=254
    for luz in b.get_light_objects():
        luz.xy=[.7, .75]

for light_id in lights:
    channel_frame = Frame(horizontal_frame)
    channel_frame.pack(side = LEFT)

    scale_command = lambda x, light_id=light_id: b.set_light(light_id,{'bri': int(x), 'transitiontime': 0})
    scale = Scale(channel_frame, from_ = 254, to = 0, command = scale_command, length = 200, showvalue = 1)
    scale.set(b.get_light(light_id,'bri'))
    scale.pack()

    button_var = BooleanVar()
    button_var.set(b.get_light(light_id, 'on'))
    button_command = lambda button_var=button_var, light_id=light_id: b.set_light(light_id, 'on', button_var.get())
    button = Checkbutton(channel_frame, variable = button_var, command = button_command)
    button.pack()

    label = Label(channel_frame)
    label.config(text = b.get_light(light_id,'name'))
    label.pack()

action = Button(root, text=('1.A'), command=cor)
action.pack(side = 'left')
action = Button(root, text=('3.B'), command=tres)
action.pack(side = 'left')
action = Button(root, text=('OFF'), command=off)
action.pack(side = 'left')

root.mainloop()