Esempio n. 1
0
    def set_target_state(name, state):
        print "!!!  Set '%s' State = %s"%(name, state)
        for switch_name in env.list_switches():
            if matcher(name)(switch_name):
                print "Found a switch matching name %s" % name
                switch = env.get_switch(switch_name)
                switch.set_state(state)

        if len(env.list_bridges()) > 0:
            bridge = env.get_bridge(env.list_bridges()[0])
            for light_name in bridge.bridge_get_lights():
                if matcher(name)(light_name):
                    print "telling target (via bridge) to set state to %s"%state
                    light = bridge.bridge_get_lights()[light_name]
                    bridge.light_set_state(light, state=state, dim=254 if state==1 else None)
Esempio n. 2
0
 def something_happened(sender, **kwargs):
     print "Something happened with %s"%sender
     for key in triggers.keys():
         if matcher(key)(sender.name):
             state = 1 if kwargs.get('state') else 0
             print "{} state is {state}".format(sender.name, state=state)
             if isinstance(triggers[key], list):
                 for target in triggers[key]:
                     set_target_state(target, state)
             else:
                 set_target_state(triggers[key], state)
Esempio n. 3
0
    def found(sender, **kwargs):
        all_targets = []
        for target in triggers.values():
            if isinstance(target, list):
                all_targets.extend(target)
            else:
                all_targets.append(target)

        for key in (triggers.keys() + all_targets):
            if matcher(key)(sender.name):
                print "Found device:", sender.name
Esempio n. 4
0
 def get(self, name):
     alias = self._config.aliases.get(name)
     if alias:
         matches = lambda x: x == alias
     elif name:
         matches = matcher(name)
     else:
         matches = _NOOP
     for k in self.devices:
         if matches(k):
             return self.devices[k]
     else:
         raise UnknownDevice(name)
Esempio n. 5
0
 def get(self, name):
     alias = self._config.aliases.get(name)
     if alias:
         matches = lambda x: x == alias
     elif name:
         matches = matcher(name)
     else:
         matches = _NOOP
     for k in self.devices:
         if matches(k):
             return self.devices[k]
     else:
         raise UnknownDevice(name)
Esempio n. 6
0
def mainloop(name):
    matches = matcher(name)

    @receiver(devicefound)
    def found(sender, **kwargs):
        if matches(sender.name):
            print "Found device:", sender.name

    @receiver(statechange)
    def motion(sender, **kwargs):
        if matches(sender.name):
            print "{} state is {state}".format(
                sender.name, state="on" if kwargs.get('state') else "off")

    env = Environment(with_cache=False)
    try:
        env.start()
        env.discover(10)
        env.wait()
    except (KeyboardInterrupt, SystemExit):
        print "Goodbye!"
        sys.exit(0)
Esempio n. 7
0
def mainloop(name):
    matches = matcher(name)

    @receiver(devicefound)
    def found(sender, **kwargs):
        if matches(sender.name):
            print "Found device:", sender.name

            while 1:
                print_some_times(sender)



   
    env = Environment(with_cache=False)
    try:
        env.start()
        env.discover(10)
        env.wait()
    except (KeyboardInterrupt, SystemExit):
        print "Goodbye!"
        sys.exit(0)
Esempio n. 8
0
def mainloop(name):
    matches = matcher(name)

    def is_standby(name):
        print "Waiting for standby or powerof"
        switch = env.get_switch(name)
        time.sleep(10)
        normal_power = switch.current_power
        time.sleep(10)

        while switch.get_state():
            if switch.current_power < (normal_power * 0.2):
                print "Device in standby mode!"
                break
            time.sleep(10)

    @receiver(devicefound)
    def found(sender, **kwargs):
        if matches(sender.name):
            print "Found device:", sender.name

    @receiver(statechange)
    def on_off(sender, **kwargs):
        if matches(sender.name):
            print "{} state is {state}".format(
                sender.name, state="on" if kwargs.get('state') else "off")
            if kwargs.get('state'):
                is_standby(sender.name)

    env = Environment(with_cache=False)
    try:
        env.start()
        env.discover(10)
        env.wait()
    except (KeyboardInterrupt, SystemExit):
        print "Goodbye!"
        sys.exit(0)
Esempio n. 9
0
def mainloop(name):
    matches = matcher(name)
    queueName = 'HUECOMMANDS'
    channel = None
    def connect():
        global channel
        global queue
        print "Connecting to RabbitMQ"
        connection = pika.BlockingConnection(pika.ConnectionParameters(
               '54.69.168.245'))
        channel = connection.channel()
        channel.queue_declare(queue=queueName)
        print "Connected to RabbitMQ"

    def postState():
        global channel
        
        print "motion detected ", datetime.now().time()
        
        #TODO: this should be posting events, not commands, but this works for now...
        if channel is not None:
            channel.basic_publish(exchange='',
                      routing_key=queueName,
                      body=buildMessage("5").SerializeToString())
            channel.basic_publish(exchange='',
                      routing_key=queueName,
                      body=buildMessage("6").SerializeToString())
            channel.basic_publish(exchange='',
                      routing_key=queueName,
                      body=buildMessage("7").SerializeToString())
            print "message posted"
        
    def buildMessage( id ):
        m = UpdateLightCommand.UpdateLightCommand()
        m.is_on = True
        m.msg_version = 1
        m.light_id = id
        m.rgb_red = 255
        m.rgb_green = 255
        m.rgb_blue = 255
        return m
    
    @receiver(devicefound)
    def found(sender, **kwargs):
        if matches(sender.name):
            print "Found device:", sender.name
            connect()

    @receiver(statechange)
    def motion(sender, **kwargs):
        if matches(sender.name) and kwargs.get('state'):
            postState()

    env = Environment(with_cache=False)
    try:
        env.start()
        env.discover(10)
        env.wait()
    except (KeyboardInterrupt, SystemExit):
        print "Goodbye!"
        sys.exit(0)
Esempio n. 10
0
import time
from ouimeaux.environment import Environment
from ouimeaux.utils import matcher
from celery import Celery
from settings import CELERY_VHOST

SWITCH_NAME = 'Real mercury outlet'
matches = matcher(SWITCH_NAME)

mercury_task = Celery('start_up',
                      broker='amqp://guest@localhost/%s' % CELERY_VHOST)


def get_switch():
    env = Environment()

    try:
        env.start()
    except Exception:
        pass

    env.discover(5)
    found = None
    for switch in env.list_switches():
        if matches(switch):
            found = env.get_switch(switch)
            break
    else:
        raise Exception('Switch not found!')

    return found
Esempio n. 11
0
import time
from ouimeaux.environment import Environment
from ouimeaux.utils import matcher
from celery import Celery
from settings import CELERY_VHOST

SWITCH_NAME = 'Real mercury outlet'
matches = matcher(SWITCH_NAME)

mercury_task = Celery('start_up', backend='redis://localhost', broker='amqp://guest@localhost/%s' % CELERY_VHOST)

def get_switch():
    env = Environment()

    try:
        env.start()
    except:
        pass

    env.discover(5)
    found = None
    for switch in env.list_switches():
        if matches(switch):
            found = env.get_switch(switch)
            break
    else:
        raise Exception('Switch not found!')

    return found

@mercury_task.task