Exemplo n.º 1
0
def _setup_gateway(hass, hass_config, host, key, allow_tradfri_groups):
    """Create a gateway."""
    from pytradfri import Gateway, RequestError
    from pytradfri.api.libcoap_api import api_factory

    try:
        api = api_factory(host, key)
    except RequestError:
        return False

    gateway = Gateway()
    # pylint: disable=no-member
    gateway_id = api(gateway.get_gateway_info()).id
    hass.data.setdefault(KEY_API, {})
    hass.data.setdefault(KEY_GATEWAY, {})
    gateways = hass.data[KEY_GATEWAY]
    hass.data[KEY_API][gateway_id] = api

    hass.data.setdefault(KEY_TRADFRI_GROUPS, {})
    tradfri_groups = hass.data[KEY_TRADFRI_GROUPS]
    tradfri_groups[gateway_id] = allow_tradfri_groups

    # Check if already set up
    if gateway_id in gateways:
        return True

    gateways[gateway_id] = gateway
    hass.async_add_job(discovery.async_load_platform(
        hass, 'light', DOMAIN, {'gateway': gateway_id}, hass_config))
    return True
Exemplo n.º 2
0
def run():
    # Assign configuration variables.
    # The configuration check takes care they are present.
    api = api_factory(sys.argv[1], sys.argv[2])

    gateway = Gateway()

    devices_command = gateway.get_devices()
    devices_commands = api(devices_command)
    devices = api(*devices_commands)

    lights = [dev for dev in devices if dev.has_light_control]

    tasks_command = gateway.get_smart_tasks()
    tasks = api(tasks_command)

    # Print all lights
    print(lights)

    # Lights can be accessed by its index, so lights[1] is the second light
    light = lights[0]

    observe(api, light)

    # Example 1: checks state of the light 2 (true=on)
    print(light.light_control.lights[0].state)

    # Example 2: get dimmer level of light 2
    print(light.light_control.lights[0].dimmer)

    # Example 3: What is the name of light 2
    print(light.name)

    # Example 4: Set the light level of light 2
    dim_command = light.light_control.set_dimmer(255)
    api(dim_command)

    # Example 5: Change color of light 2
    # f5faf6 = cold | f1e0b5 = normal | efd275 = warm
    color_command = light.light_control.set_hex_color('efd275')
    api(color_command)

    # Example 6: Return the transition time (in minutes) for task#1
    if tasks:
        print(tasks[0].task_control.tasks[0].transition_time)

        # Example 7: Set the dimmer stop value to 30 for light#1 in task#1
        dim_command_2 = tasks[0].start_action.devices[0].item_controller\
            .set_dimmer(30)
        api(dim_command_2)

    print("Sleeping for 2 min to receive the rest of the observation events")
    print("Try altering the light (%s) in the app, and watch the events!" %
          light.name)
    time.sleep(120)
Exemplo n.º 3
0
    def init(self):
        if self.hubip is None:
            conf = configparser.ConfigParser()
            conf.read('tradfri.cfg')
            #print(conf)

            self.hubip = conf.get('tradfri', 'hubip')
            self.securityid = conf.get('tradfri', 'securityid')
            self.api = api_factory(self.hubip, self.securityid)
            self.gateway = Gateway()

            groups_command = self.gateway.get_groups()
            groups_commands = self.api(groups_command)
            groups = self.api(*groups_commands)
            self.groups = dict((g.name, g) for g in groups)
            print(str(self.groups))
Exemplo n.º 4
0
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))
Exemplo n.º 5
0
        c = order[index % len(order)]
    else:
        c = colors[arg]

    command = light.light_control.set_hex_color(c)
    api(command)


if __name__ == "__main__":
    if len(sys.argv) != 6:
        print("Syntax: lights.py <IP> <KEY> <NAME> <COMMAND> <ARGUMENT>")
        sys.exit(1)

    ip, key, name, command, arg = sys.argv[1:]

    api = api_factory(ip, key)
    gateway = Gateway()

    if name.startswith("group:"):
        light = find_group(name.split(":")[1])
    elif name.startswith("light:"):
        light = find_light(name.split(":")[1])
    else:
        light = find_light(name)

    if light == None:
        print("No devices named", name, "found")
        sys.exit(1)

    if command == "dim":
        dim(light, arg)