Iterator added to registry
1 parent 4b01055 commit 30d1028c250f5bb3ad72d2f4dc52a905a25b8c2d
@David Whale David Whale authored on 29 May 2016
Showing 2 changed files
View
2
■■■
src/control_any_reg.py
global socket_state
 
print("Setting socket switches to %s" % str(socket_state))
 
for device in energenie.registry.devices(): #TODO: Make energenie.registry iterable
for device in energenie.registry.devices():
# Only try to toggle the switch for devices that actually have a switch
 
if device.has_switch():
print(" socket id %s" % device)
View
66
src/energenie/Registry.py
def size(self):
"""How many entries are there in the registry?"""
return self.store.size()
 
@untested
def devices(self):
"""Get a list of all device classes in the registry"""
#TODO: Temporary method until we read up about iterable, so we can say
# for devices in energenie.registry
dl = []
for k in self.store.keys():
d = self.store[k]
dl.append(d)
return dl
 
"""A generator/iterator that can be used to get a list of device instances"""
 
# first get a list of all devices, in case the registry changes while iterating
devices = self.store.keys()
 
# now 'generate' one per call
i = 0
while i < len(devices):
k = devices[i]
device = self.store[k]
yield device
i += 1
 
 
@untested
def names(self):
"""Get a list of all the names in the registry"""
return self.store.keys()
 
"""A generator/iterator that can be used to get a list of device names"""
# first get a list of all devices, in case the registry changes while iterating
devices = self.store.keys()
 
# now 'generate' one per call
i = 0
while i < len(devices):
k = devices[i]
yield k
i += 1
 
 
 
#TODO: Might move this to energenie.init() so that it is optional
#will make it possible to run all the test cases together also.
registry = DeviceRegistry()
import os
if os.path.isfile(DeviceRegistry.DEFAULT_FILENAME):
registry.load_from(DeviceRegistry.DEFAULT_FILENAME)
 
# Might rename these, especially when we add in other protocols
# such as devices that are 868 wirefree doorbells etc.
 
#TODO: Might move this to energenie.init() so that it is optional
#will make it possible to run all the test cases together also.
 
#TODO: Name is not completely representative of function.
# This is the Energenie 433.92MHz with OpenThings
fsk_router = Router("fsk")
 
y = y.upper()
if y in ['Y', 'YES']: return True
return False
 
#TODO: Might move this to energenie.init() so that it is optional
#will make it possible to run all the test cases together also.
 
# Default discovery mode, unless changed by app
##discovery_none()
##discovery_auto()