本文整理汇总了Python中ouimeaux.environment.Environment.list_switches方法的典型用法代码示例。如果您正苦于以下问题:Python Environment.list_switches方法的具体用法?Python Environment.list_switches怎么用?Python Environment.list_switches使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ouimeaux.environment.Environment
的用法示例。
在下文中一共展示了Environment.list_switches方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Wemo
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import list_switches [as 别名]
class Wemo(Command):
def __init__(self):
self.env = Environment(with_discovery=False, with_subscribers=False)
self.env.start()
def on(self, device):
switch = self.env.get_switch(closest_match(device, self.env.list_switches()))
switch.basicevent.SetBinaryState(BinaryState=1)
def off(self, device):
switch = self.env.get_switch(closest_match(device, self.env.list_switches()))
switch.basicevent.SetBinaryState(BinaryState=0)
示例2: main
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import list_switches [as 别名]
def main():
'''
Server routine
'''
port = "9801"
context = zmq.Context.instance()
# Receive input from the outside world
socket = context.socket(zmq.DEALER)
# Specify unique identity
socket.setsockopt(zmq.IDENTITY, b"WeMo")
socket.connect("tcp://127.0.0.1:%s" % port)
print "Ready to receive"
# Where we will store references to the worker threads
worker_sockets = {}
# Start the ouimeaux environment for discovery
env = Environment(with_subscribers = False, with_discovery=True, with_cache=False)
env.start()
discovered.connect(discovered_wemo)
# Run the polling mechanism in the background
BackgroundDiscovery(env).start()
while True:
# Get the outside message in several parts
# Store the client_addr
client_addr, _, msg = socket.recv_multipart()
print "Received request {} from '{}'".format(msg, client_addr)
msg = msg.split(' ')
command = msg[0]
# General commands
if command == 'list':
# Send the current set of devices (only switches supported)
socket.send_multipart([client_addr, b'', ",".join(env.list_switches())])
continue
# Commands on objects
switch_name = msg[1]
print switch_name
s = env.get_switch(switch_name)
if command == 'on':
s.on()
socket.send_multipart([client_addr, b'', 'OK'])
elif command == 'off':
s.off()
socket.send_multipart([client_addr, b'', 'OK'])
elif command == 'state':
st = s.get_state()
st = 'on' if st else 'off'
socket.send_multipart([client_addr, b'', st])
示例3: devices
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import list_switches [as 别名]
def devices():
try:
env = Environment()
env.start()
env.discover(seconds=3)
result = env.list_switches()
except:
raise
return result
示例4: Wemo
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import list_switches [as 别名]
class Wemo(object):
state_map = {'on': 1, 'off': 0}
state_map_wemo = {'1': 'on', '0': 'off'}
def __init__(self):
self.env = Environment(with_cache=False)
self.env.start()
self.env.discover()
self.list_switches = self.env.list_switches()
print self.list_switches
def get(self, name, use_cache=False):
'''
get - returns the state of the given device
'''
status = 0
state = None
s = self.env.get_switch(name)
try:
state = s.basicevent.GetBinaryState()
if 'BinaryState' in state:
print 'get info: {}'.format(state)
state = Wemo.state_map_wemo[state['BinaryState']]
return status, state
except Exception as e:
print 'exception {}'.format(e)
status = 2
return status, state
def set(self, name, val_on_off, use_cache=False):
'''
set - turns the wemo switch either on or off. Will
execute for all the switches in the list
'''
status = 0
s = self.env.get_switch(name)
print 'state: {}'.format(val_on_off)
state = Wemo.state_map[val_on_off.lower()]
try:
s.basicevent.SetBinaryState(BinaryState=state)
except:
status = 2
return status
示例5: get_switch
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import list_switches [as 别名]
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
示例6: main
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import list_switches [as 别名]
def main():
# define input parameters
parser = OptionParser()
parser.add_option("-a", help="all of the wemo devices", default=False, action="store_true")
parser.add_option("-g", help="get switch state", default=False, action="store_true")
parser.add_option("-l", help="list all the switches", default=False, action="store_true")
parser.add_option("--off", help="off flag - default is on", default=False, action="store_true")
parser.add_option("-s", help="switch name", default=None)
# read input values
(options, args) = parser.parse_args()
flag_on_off = ON
if options.off:
flag_on_off = OFF
flag_list = options.l
flag_get = options.g
switch_name = options.s
#
# environment - wemo
env = Environment()
env.start()
env.discover()
switches = env.list_switches()
if switch_name is not None:
if switch_name not in switches:
print "%s is not available" % switch_name
return {"status": 1}
else:
switches = [switch_name]
if flag_list:
return {"status": 0, "msg": switches}
if flag_get:
return {"status": 0, "msg": switch_get(env, switches)}
#
# switch - on/off
switch_set(env, switches, flag_on_off)
return {"status": 0, "msg": "switch(es):%s val:%s" % (switches, flag_on_off)}
示例7: _on_switch
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import list_switches [as 别名]
class WemoController:
_env = None
def _on_switch(self, switch):
print "Light Switch found: ", switch.name
def _on_motion(self, motion):
print "Motion Sensor found: ", motion.name
def connect(self):
self._env = Environment(self._on_switch, self._on_motion)
try:
self._env.start()
except TypeError:
print "Start error"
try:
self._env.discover(seconds=3)
except TypeError:
print "Discovery error"
def find_switches(self):
result = []
switches = self._env.list_switches()
print "Found " + str(len(switches))
print switches
for s in switches:
print "Found " + s
result.append({ "name" : s, "state" : self._env.get_switch(s).get_state() })
return result
def switch_on(self, switch_name):
switch = self._env.get_switch(switch_name)
switch.on()
def switch_off(self, switch_name):
switch = self._env.get_switch(switch_name)
switch.off()
def switch_state(self, switch_name):
switch = self._env.get_switch(switch_name)
return switch.get_state()
示例8: go_pi
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import list_switches [as 别名]
def go_pi():
try:
sys.stderr = open("otherlog.txt", "wa+")
sys.stdout = open("log.txt", "wa+")
print "Powering up server..."
switches = []
env = Environment(on_switch, on_motion)
env.start()
#search until a switch is found
tries = 0
print "Searching for switches..."
while True:
tries += 1
names = env.list_switches()
if len(names) > 0: break
if tries == 100:
print "FAILED to find a switch after 100 tries"
sys.exit()
#create a dictionary object wrapper for each found switch
for switch_name in names:
s = {NAME : switch_name, SWITCH : None, STATE : OFF, ID : None, THRESH : None, MIN : 0}
if switch_name == "ymcmb":
ymcmb = env.get_switch('ymcmb')
s[SWITCH] = ymcmb
s[ID] = YMCMB_ID
s[THRESH] = YMCMB_THRESH
s[MIN] = YMCMB_MIN
elif switch_name == "patty":
paddy = env.get_switch('patty')
s[SWITCH] = paddy
s[ID] = PADDY_ID
s[THRESH] = PADDY_THRESH
#create a list of all found switches
switches.append(s)
run_local_server(switches)
except:
pass
示例9: Environment
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import list_switches [as 别名]
print "WeMo Randomizer"
print "---------------"
env = Environment()
# TODO: run from 10am to 10pm
env.start()
env.discover(seconds=3)
time.sleep(3)
#reading Config files setting
configParser = ConfigParser.RawConfigParser()
configFilePath = r'config.ini'
configParser.read(configFilePath)
print env.list_switches()
switchList = env.list_switches()
start=time.time()
'''
In this loop we will go throuh list of devices and for each one will store the following information
switch name, switch mac address, and current power consumption.
we also search for new devices based on the rediscovery time
for example if it's 20 we will refresh list of devices every 20 seconds.
this is for times when we unplug one device and plug it again for example.
'''
while True:
for i in range(0,len(switchList)):
switch = env.get_switch(switchList[i])
try:
power= switch.insight.GetPower()["InstantPower"]
print power
示例10: list_switches
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import list_switches [as 别名]
def list_switches():
env = Environment(on_switch, on_motion)
env.start()
env.discover(seconds=1)
return env.list_switches()
示例11: WemoInsightSwitch
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import list_switches [as 别名]
class WemoInsightSwitch(basemodule.SensorModule):
def __init__(self):
self.env = Environment(switch_callback=self.on_switch, motion_callback=self.on_motion, with_cache=False)
self.env.start()
self.devices = []
def trigger_device_discovery(self):
self.env.discover(seconds=10)
self.env.list_switches()
devices_found = self.env.list_switches()
timestamp = time.time()
data = []
self.devices = []
for device_name in devices_found:
data.append((device_name, timestamp))
self.devices.append(device_name)
return data
def trigger_device_check(self):
devices = self.devices
data = []
timestamp = time.time()
for device_name in devices:
try:
insight = self.env.get_switch(device_name)
tkwh = insight.today_kwh
cp = insight.current_power
tot = insight.today_on_time
of = insight.on_for
lc = insight.last_change
tsbt = insight.today_standby_time
ot = insight.ontotal
tmw = insight.totalmw
data.append((device_name, tkwh, cp, tot, of, lc, tsbt, ot, tmw, timestamp))
except:
print 'Error connecting to WeMo'
print '-'*20
traceback.print_exc(file=sys.stdout)
print '-'*20
return data
@staticmethod
def discovery_timer():
return 900
@staticmethod
def check_timer():
return 30
@staticmethod
def data_format():
return [('device_name', 'text'), ('today_kwh', 'integer'), ('current_power', 'integer'), ('today_on_time', 'integer'), ('on_for', 'integer'), ('last_change', ' integer'), ('today_standby_time', 'integer'), ('ontotal', 'integer'), ('totalmw', 'integer'), ('date', 'integer')];
@staticmethod
def database_version():
return 1
def on_switch(self, switch):
print "Switch found!", switch.name
def on_motion(self, motion):
print "Motion found!", motion.name
示例12: Environment
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import list_switches [as 别名]
import ouimeaux
from ouimeaux.environment import Environment
from time import sleep
print "env"
wemo = Environment()
print "start"
wemo.start()
print "discover"
wemo.discover(5)
print wemo.list_switches()
wemoFan = wemo.get_switch("wemoFan02")
print "on"
wemoFan.on()
sleep(5)
print "off"
wemoFan.off()
sleep(5)
print "toggle"
wemoFan.toggle()
示例13: Environment
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import list_switches [as 别名]
# 2_7_Client.py
# Echo client program
import socket
from ouimeaux.environment import Environment
env = Environment()
env.start()
switches = list()
env.discover(seconds=3)
switchesList = env.list_switches()
#switches = list()
for s in switchesList:
switches.append(env.get_switch(s))
str_switches = ', '.join(switchesList) # to be sent to server
HOST = 'localhost' # The remote host
PORT = 50010 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#s.connect((HOST, PORT))
s.bind((HOST, PORT))
print('Ready for Connections')
示例14: W1ThermSensor
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import list_switches [as 别名]
# TS = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20,'0008006b3137')
TRoom = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20,TRoomID) # a new DS18B20 sensor
TRoof = W1ThermSensor(16,TRoofID) # an old DS1820 sensor
# check temperature sensors
print "Find Temp Sensors"
for s in W1ThermSensor.get_available_sensors():
print ">Found:",s.id, s.get_temperature(),"'C"
# setup WeMo switch
print "\nFind Wemo Switches"
wemo = Environment()
print ">start"
wemo.start()
print ">discover"
wemo.discover(5)
print ">Found: ", wemo.list_switches()
print ">get RoofFan"
RoofFan01 = wemo.get_switch(RoofFanID)
# humidty sensor
print "Check Humidity Sensors"
print "Roof ",
hrf,trf2 = readHumidity(HS1_Pin)
print "Room ",
hrm,trm2 = readHumidity(HS2_Pin)
# setup GPIO for buzzer and LED
gpio.setup(PWM_Pin,gpio.OUT) #alarm buzzer
gpio.setup(LED_Pin,gpio.OUT) #LED
示例15: Environment
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import list_switches [as 别名]
import datetime
import time
import ouimeaux
from ouimeaux.environment import Environment
# http://pydoc.net/Python/ouimeaux/0.7.3/ouimeaux.examples.watch/
if __name__ == "__main__":
print ""
print "WeMo Randomizer"
print "---------------"
env = Environment()
# TODO: run from 10am to 10pm
try:
env.start()
env.discover(100)
print env.list_switches()
print env.list_motions()
print "---------------"
while True:
# http://stackoverflow.com/questions/306400/how-do-i-randomly-select-an-item-from-a-list-using-python
switchRND = env.get_switch( random.choice( env.list_switches() ) )
print switchRND
switchRND.toggle()
env.wait(90)
except (KeyboardInterrupt, SystemExit):
print "---------------"
print "Goodbye!"
print "---------------"
# Turn off all switches
for switch in ( env.list_switches() ):