Exemplo n.º 1
0
def test_fetch_lights(monkeypatch):
    test_light_name = 'test light'

    class MockResponse:
        def json(self):
            return {
                '1': {
                    'name': test_light_name,
                    'state': {}
                }
            }

    test_url = 'http://test.com'

    def mock_get(*args, **kwargs):
        assert args[0] == test_url + '/lights'
        return MockResponse()

    monkeypatch.setattr(requests, 'get', mock_get)
    api = HueApi()
    api.base_url = test_url
    api.fetch_lights()
    assert len(api.lights) == 1
    assert api.lights[0].name == test_light_name
    assert api.lights[0].id == 1
from hue_api import HueApi
import bs4
import urllib.request
import time

api = HueApi()

# api.load_existing('Hue IP here')
api.create_new_user('Hue IP here')

api.fetch_lights()

previous_price = 0
day_high = 0

api.turn_on()


def get_price():
    url = 'https://uk.finance.yahoo.com/quote/GME/chart?p=GME' \
          '#eyJpbnRlcnZhbCI6MSwicGVyaW9kaWNpdHkiOjEsInRpbWVVbml0IjoibWludXRlIiwiY2FuZGxlV2lkdGgiOjguMTY1NDY3NjI1ODk5MjgsImZsaXBwZWQiOmZhbHNlLCJ2b2x1bWVVbmRlcmxheSI6dHJ1ZSwiYWRqIjp0cnVlLCJjcm9zc2hhaXIiOnRydWUsImNoYXJ0VHlwZSI6ImxpbmUiLCJleHRlbmRlZCI6ZmFsc2UsIm1hcmtldFNlc3Npb25zIjp7fSwiYWdncmVnYXRpb25UeXBlIjoib2hsYyIsImNoYXJ0U2NhbGUiOiJsaW5lYXIiLCJwYW5lbHMiOnsiY2hhcnQiOnsicGVyY2VudCI6MSwiZGlzcGxheSI6IkdNRSIsImNoYXJ0TmFtZSI6ImNoYXJ0IiwiaW5kZXgiOjAsInlBeGlzIjp7Im5hbWUiOiJjaGFydCIsInBvc2l0aW9uIjpudWxsfSwieWF4aXNMSFMiOltdLCJ5YXhpc1JIUyI6WyJjaGFydCIsIuKAjHZvbCB1bmRy4oCMIl19fSwic2V0U3BhbiI6bnVsbCwibGluZVdpZHRoIjoyLCJzdHJpcGVkQmFja2dyb3VuZCI6dHJ1ZSwiZXZlbnRzIj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ydCJ9fX19 '

    page = urllib.request.urlopen(url)

    soup = bs4.BeautifulSoup(page, "html.parser")

    price = soup.find('div', {
        'class': 'My(6px) Pos(r) smartphone_Mt(6px)'
    }).find('span').text

    return float(price)
Exemplo n.º 3
0
    elif foundDefaultCount > 1:
        phase(-1)
        phase(phaseMinutes)


while (True):
    time = datetime.now()
    currentHour = time.hour
    sr = sun.get_local_sunrise_time(local_time_zone=denver).replace(
        tzinfo=None)
    ss = sunset()
    ssOffset = (ss - time).seconds
    srOffset = (sr - time).seconds
    #CHECK DEFAULTS

    for light in api.fetch_lights():
        if light.state.hue == 8417 and light.state.saturation == 140:  #the manufacturer default color
            #If it's the same poll as another light, don't increment the counter.

            #   light.set_state({'hue': 8418, 'sat': 140, 'transitiontime': 1})
            if firstDefaultStateTime == 0:
                firstDefaultStateTime = time
                #   light.set_state({'hue': 8418, 'sat': 140, 'transitiontime': 1})
                foundDefaultCount = 1
                print("first default light")
            elif (time - firstDefaultStateTime
                  ).seconds > 30:  #start counting again after X seconds
                firstDefaultStateTime = time
                foundDefaultCount = 1

            elif (time - firstDefaultStateTime