def get_light(light_index): #returns light(s) selected by light_index gateway = Gateway() devices_commands = run_command(gateway.get_devices()) devices = run_command(devices_commands) lights = [dev for dev in devices if dev.has_light_control] #if many light wanted try: return [lights[l] for l in light_index] except: return lights[light_index]
class Register(): def __init__(self): self.config_file = '/home/pi/code/python/pytradfri/tradfri_standalone_psk.conf' self.host = "192.168.2.167" self.api = self.authenticate_api() self.gateway = Gateway() self._devices = self.get_devices() self._lights = self.get_lights() self._groups = self.get_groups() self.clusters = None @property def groups(self): pp(self._groups) @property def lights(self): pp(self._lights) @property def devices(self): pp(self._devices) def get_groups(self): commands_to_get_groups = self.run_command(self.gateway.get_groups()) return self.run_command(commands_to_get_groups) def get_devices(self): commands_to_get_devices = self.run_command(self.gateway.get_devices()) return self.run_command(commands_to_get_devices) def get_lights(self): return [dev for dev in self._devices if dev.has_light_control] def run_command(self, command): return self.api(command) def authenticate_api(self): #returns an authenticated API object conf = load_json(self.config_file) identity = conf[self.host].get('identity') psk = conf[self.host].get('key') api_factory = APIFactory(host=self.host, psk_id=identity, psk=psk) return api_factory.request
def main(): logging.basicConfig(filename='plantnet.log', level=logging.INFO) # api setup host = '192.168.15.11' with open('gateway.key', 'r') as keyfile: gateway_key = keyfile.read().replace('\n', '') api = api_factory(host, gateway_key) gateway = Gateway() devices_commands = api(gateway.get_devices()) devices = api(*devices_commands) lights = [dev for dev in devices if dev.has_light_control] # If number is supplied on light_setlevel = -1 tc = -1 if len(sys.argv) > 1: try: light_setlevel = int(sys.argv[1]) tc = -1 except ValueError: None if light_setlevel < 0: light_setlevel, tc = computelightlevel() # Set lights logstr = 'plantnet {}: tc = {:0.2f}, setting lights to {}'.format( time.strftime('%c'), tc, light_setlevel) logging.info(logstr) for light in lights: # Set on/off state target_state = light_setlevel > 0 current_state = light.light_control.lights[0].state if not current_state == target_state: api(light.light_control.set_state(target_state)) if target_state == True: api(light.light_control.set_dimmer(light_setlevel))
try: psk = api_factory.generate_psk(args.key) print('Generated PSK: ', psk) conf[args.host] = {'identity': identity, 'key': psk} save_json(CONFIG_FILE, conf) except AttributeError: raise PytradfriError("Please provide the 'Security Code' on the " "back of your Tradfri gateway using the " "-K flag.") api = api_factory.request gateway = Gateway() devices_commands = api(gateway.get_devices()) devices = api(devices_commands) lights = [dev for dev in devices if dev.has_light_control] if lights: light = lights[0] else: print("No lights found!") light = None groups = api(gateway.get_groups()) if groups: group = groups[0] else: print("No groups found!") group = None moods = api(gateway.get_moods()) if moods: