python 使用Chromecast时(或更具体地说,当某些应用程序处于打开状态时),快速破解使飞利浦Hue灯泡变暗。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 使用Chromecast时(或更具体地说,当某些应用程序处于打开状态时),快速破解使飞利浦Hue灯泡变暗。相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python

import os
import requests
import libxml2
import time
import phue
import daemon
import lockfile
import signal

trigger_apps = ["Plex/Chromecast"]
sleep_interval = 10

class Chromecast(object):
    apps_url = "http://%s:8008/apps/"

    def __init__(s, device_name):
        s.device_name = device_name

    def runningApp(s):
        try:
            response = requests.get(s.apps_url % s.device_name, timeout=2.0)
            data = libxml2.parseDoc(response.text)

            context = data.xpathNewContext()
            context.xpathRegisterNs("root", "urn:dial-multiscreen-org:schemas:dial")
            context.xpathRegisterNs("cast", "urn:chrome.google.com:cast")

            description = context.xpathEval("//cast:description")

            return description[0].content
        except:
            return "None"

class Hue(object):
    def __init__(s, device_name):
        s.device_name = device_name
        s.bridge = phue.Bridge(device_name)
        s.bridge.connect()
        s.dimmed = False

    def dimUp(s):
        s.dimmed = False
        for light in s.lights:
            s.bridge.set_light(light['id'], {'transitiontime':20,'on':True,'bri':light['bri'],'hue':light['hue'],'sat':light['sat']})

    def dimDown(s):
        s.dimmed = True
        s.lights = []
        for light in s.bridge.lights:
            s.lights.append({'id':light.light_id,'hue':light.hue,'on':light.on,'bri':light.brightness,'sat':light.saturation})

        s.bridge.set_light([2,3], {'transitiontime':20,'on':False})
        s.bridge.set_light(1, {'transitiontime':20,'on':True,'bri':1,'hue':65535,'sat':255})

def cleanup():
    if hue.dimmed:
        hue.dimUp()

chromecast = Chromecast("chromecast")
hue = Hue("philips-hue")

context = daemon.DaemonContext(pidfile = lockfile.FileLock(os.path.join(os.environ['HOME'], ".chromelight")))
context.signal_map = {
    signal.SIGTERM: cleanup,
    signal.SIGHUP: 'terminate'
}

context.stdout = open(os.path.join(os.environ['HOME'], '.chromelight.log'), 'w+')
context.stderr = context.stdout

with context:
    while True:
        app = chromecast.runningApp()
        print "Running:", app

        if app in trigger_apps:
            if not hue.dimmed:
                hue.dimDown()
                print "Dimming down"
        elif hue.dimmed:
            hue.dimUp()
            print "Dimming up"

        time.sleep(sleep_interval)

以上是关于python 使用Chromecast时(或更具体地说,当某些应用程序处于打开状态时),快速破解使飞利浦Hue灯泡变暗。的主要内容,如果未能解决你的问题,请参考以下文章

更具体的 HTTP 代码是“Semver 重大更改”吗?

在隐含的上下文中,“比更具体”到底是啥意思?

使用 Scrapy 在 Python 中选择部分文本字段

过滤或隐藏可用的 ChromeCast 设备

当 chromecast 关机时,chromecast 需要很长时间才能隐藏 chromecat 按钮

Typescript 接口合并以使返回类型更具体