Exemple #1
0
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
Exemple #2
0
        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:
    mood = moods[0]
else:
    print("No moods found!")
    mood = None
tasks = api(gateway.get_smart_tasks())
homekit_id = api(gateway.get_gateway_info()).homekit_id