Ejemplo n.º 1
0
async def main():
    # Call the definition of a light bulb, replace the IP address with the one found with kasa discover
    bulb = SmartBulb("10.0.1.3")
    # 'await' tells the program to wait till getting a result from the light bulb
    result = await bulb.turn_on()
    # Once we receive it, we call print() to show the result in the Terminal
    print(result)
Ejemplo n.º 2
0
class BulbController():
    def __init__(self, bulb_one_name, bulb_two_name):
        bulbs = self.discover_bulbs(bulb_one_name, bulb_two_name)
        self.bulb1 = SmartBulb(bulbs[0])
        self.bulb2 = SmartBulb(bulbs[1])

    def discover_bulbs(self, bulb_one_name, bulb_two_name):
        bulbs = []
        found_devices = asyncio.run(Discover.discover())
        ip_addresses = [item for item in found_devices]
        for count, device in enumerate(found_devices.values()):
            if device.is_bulb:
                bulbs.append(ip_addresses[count])
        return bulbs

    def update_bulbs(self):
        asyncio.run(self.bulb1.update())
        asyncio.run(self.bulb2.update())

    def turn_color(self, hue=255, saturation=100, value=100):
        self.update_bulbs()
        asyncio.run(self.bulb1.set_hsv(hue, saturation, value))
        asyncio.run(self.bulb2.set_hsv(hue, saturation, value))

    def bulb_off(self):
        self.update_bulbs()
        asyncio.run(self.bulb1.turn_off())
        asyncio.run(self.bulb2.turn_off())

    def bulb_on(self):
        self.update_bulbs()
        asyncio.run(self.bulb1.turn_on())
        asyncio.run(self.bulb2.turn_on())

    def turn_blue(self):
        self.bulb_on()
        self.turn_color()

    def turn_red(self):
        self.bulb_on()
        self.turn_color(1, 100, 100)

    def dim(self, brightness=50):
        self.update_bulbs()
        self.bulb_on()
        asyncio.run(self.bulb1.set_brightness(brightness))
        asyncio.run(self.bulb2.set_brightness(brightness))
Ejemplo n.º 3
0
async def main():
    bulb = SmartBulb(KASA_DEVICE_IP)
    await bulb.update()
    print(bulb.alias)
    hue = 0
    while (True):
        await bulb.set_hsv(hue, 100, 100)
        hue = (hue + int(KASA_HUE_SPEED)) % 360
Ejemplo n.º 4
0
    def get_power_state(self):
        if self.device_type in [self.PLUG, self.BULB]:
            device = (SmartPlug(self.ip) if self.device_type in [self.PLUG]
                      else SmartBulb(self.ip))

            asyncio.run(device.update())

            return device.is_on

        return False
Ejemplo n.º 5
0
    def get_device(self):
        if self.device_type in [self.PLUG, self.BULB]:
            device = (SmartPlug(self.ip) if self.device_type in [self.PLUG]
                      else SmartBulb(self.ip))
            asyncio.run(device.update())

            return device

        else:
            return self
Ejemplo n.º 6
0
async def gsinfo():
    ip = get_device_ip(bulb_model)
    bulb = SmartBulb(ip)
    await bulb.update()
    data = {}
    data["alias"] = bulb.alias
    data["model"] = bulb.model
    data["rssi"] = bulb.rssi
    data["mac"] = bulb.mac
    data["trange"] = bulb.valid_temperature_range
    return {"data" : data}
Ejemplo n.º 7
0
async def setcolor():
    ip = get_device_ip(bulb_model)
    bulb = SmartBulb(ip)
    await bulb.update()

    form = await request.form
    h,s,v = form['h'],form['s'],form['v']

    await bulb.set_hsv(int(h), int(s), int(v))
    await bulb.update()
    return {"ccolor" : bulb.hsv}
Ejemplo n.º 8
0
async def setbrightness():
    ip = get_device_ip(bulb_model)
    bulb = SmartBulb(ip)
    await bulb.update()

    form = await request.form
    bright = form['bright']

    await bulb.set_brightness(int(bright))
    await bulb.update()

    return {"cbrightness" : bulb.brightness}
Ejemplo n.º 9
0
async def setcolortemp():
    ip = get_device_ip(bulb_model)
    bulb = SmartBulb(ip)
    await bulb.update()

    form = await request.form
    temp = form['temp']

    await bulb.set_color_temp(int(temp))
    await bulb.update()

    return {"ctemp" : bulb.color_temp}
Ejemplo n.º 10
0
async def cli(ctx, host, alias, target, debug, bulb, plug, lightstrip, strip,
              klap, user, password):
    """A tool for controlling TP-Link smart home devices."""  # noqa
    if debug:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.INFO)

    if ctx.invoked_subcommand == "discover":
        return

    if alias is not None and host is None:
        click.echo(f"Alias is given, using discovery to find host {alias}")
        host = await find_host_from_alias(alias=alias, target=target)
        if host:
            click.echo(f"Found hostname is {host}")
        else:
            click.echo(f"No device with name {alias} found")
            return

    if password != "" and user == "":
        click.echo("Using a password requires a username")
        return

    if klap or user != "":
        authentication = Auth(user=user, password=password)
    else:
        authentication = None

    if host is None:
        click.echo("No host name given, trying discovery..")
        await ctx.invoke(discover)
        return
    else:
        if not bulb and not plug and not strip and not lightstrip:
            click.echo("No --strip nor --bulb nor --plug given, discovering..")
            dev = await Discover.discover_single(host, authentication)
        elif bulb:
            dev = SmartBulb(host)
        elif plug:
            dev = SmartPlug(host, authentication)
        elif strip:
            dev = SmartStrip(host)
        elif lightstrip:
            dev = SmartLightStrip(host)
        else:
            click.echo(
                "Unable to detect type, use --strip or --bulb or --plug!")
            return
        ctx.obj = dev

    if ctx.invoked_subcommand is None:
        await ctx.invoke(state)
Ejemplo n.º 11
0
def temperature(dev: SmartBulb, temperature):
    """Get or set color temperature."""
    if temperature is None:
        click.echo(f"Color temperature: {dev.color_temp}")
        valid_temperature_range = dev.valid_temperature_range
        if valid_temperature_range != (0, 0):
            click.echo("(min: {}, max: {})".format(*valid_temperature_range))
        else:
            click.echo("Temperature range unknown, please open a github issue"
                       f" or a pull request for model '{dev.model}'")
    else:
        click.echo(f"Setting color temperature to {temperature}")
        asyncio.run(dev.set_color_temp(temperature))
Ejemplo n.º 12
0
async def toggle_bulb(bulb, state):
    p = SmartBulb(bulb)
    await p.update()
    if state == "off":
        await p.turn_on()
    elif state == "on":
        await p.turn_off()
    elif state == "toggle":
        if p.is_off:
            await p.turn_on()
        elif p.is_on:
            await p.turn_off()
        else:
            print(datetime.now(), "weird, %s is neither on nor off" % bulb)
Ejemplo n.º 13
0
async def flip():
    ip = get_device_ip(bulb_model)
    bulb = SmartBulb(ip)
    await bulb.update()
    final_state = ''
    cstate = bulb.is_on
    if cstate:
        await bulb.turn_off()
        final_state = status(not cstate)
    else:
        await bulb.turn_on()
        final_state = status(not cstate)
    
    return {"status" : final_state}
Ejemplo n.º 14
0
    def set_power_state(self, power):
        if self.device_type in [self.PLUG, self.BULB]:
            device = (SmartPlug(self.ip) if self.device_type in [self.PLUG]
                      else SmartBulb(self.ip))

            if power:
                asyncio.run(device.turn_on())
            else:
                asyncio.run(device.turn_off())

            asyncio.run(device.update())

        elif self.device_type in [self.PI, self.LINUX]:
            stdin = b''
            stdout = b''
            stderr = b''
            if not power:
                client = SSHClient()
                client.set_missing_host_key_policy(AutoAddPolicy())
                client.connect(self.ip,
                               port=22,
                               username=self.username,
                               password=self.password)
                stdin_model, stdout_model, stderr_model = client.exec_command(
                    'sudo -S shutdown -h now')
                stdin_model.write('%s\n' % self.password)
                stderr_model.flush()
                # print the results

                if stdin_model.readable():
                    stdin = stdin_model.read()
                if stdout_model.readable():
                    stdout = stdout_model.read()
                if stderr_model.readable():
                    stderr = stderr_model.read()

                client.close()

                return (stdin, stdout, stderr)

        elif self.device_type in [self.ROKU]:
            roku = Roku(self.ip)
            roku.select()
        return (None, None, None)
Ejemplo n.º 15
0
async def cli(ctx, host, alias, target, debug, bulb, plug, strip):
    """A cli tool for controlling TP-Link smart home plugs."""  # noqa
    if debug:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.INFO)

    if ctx.invoked_subcommand == "discover":
        return

    if alias is not None and host is None:
        click.echo(f"Alias is given, using discovery to find host {alias}")
        host = await find_host_from_alias(alias=alias, target=target)
        if host:
            click.echo(f"Found hostname is {host}")
        else:
            click.echo(f"No device with name {alias} found")
            return

    if host is None:
        click.echo("No host name given, trying discovery..")
        await ctx.invoke(discover)
        return
    else:
        if not bulb and not plug and not strip:
            click.echo("No --strip nor --bulb nor --plug given, discovering..")
            dev = await Discover.discover_single(host)
        elif bulb:
            dev = SmartBulb(host)
        elif plug:
            dev = SmartPlug(host)
        elif strip:
            dev = SmartStrip(host)
        else:
            click.echo(
                "Unable to detect type, use --strip or --bulb or --plug!")
            return
        ctx.obj = dev

    if ctx.invoked_subcommand is None:
        await ctx.invoke(state)
Ejemplo n.º 16
0
plug = SmartPlug("192.168.1.x")


async def plug_turn_on():
    await plug.update()
    await plug.turn_on()


async def plug_turn_off():
    await plug.turn_off()


### TPLINK KASA KL130
### Change the ip addresss accordingly
print('Loading smartbulb <--- KL60')
bulb = SmartBulb("192.168.2.117")


def red_alert_mode():
    bulb.turn_on()
    bulb.hsv = (0, 75, 100)
    time.sleep(0.7)
    bulb.turn_off()
    time.sleep(0.7)
    bulb.turn_on()
    time.sleep(0.7)
    bulb.turn_off()
    time.sleep(0.7)
    bulb.turn_on()
    time.sleep(0.7)
    bulb.turn_off()
Ejemplo n.º 17
0
async def hsinfo():
    ip = get_device_ip(bulb_model)
    bulb = SmartBulb(ip)
    await bulb.update()
    return {"data" : bulb.hw_info}
Ejemplo n.º 18
0
async def state():
    ip = get_device_ip(bulb_model)
    print(ip)
    bulb = SmartBulb(ip)
    await bulb.update()
    return {"status" : status(bulb.is_on)}
Ejemplo n.º 19
0
import asyncio

from kasa import SmartBulb

bulb = SmartBulb('10.0.2.23')
asyncio.run(bulb.update())
SINGLE_CHANGE_DUR = 0.12

from src.controller.VLampController import VLampController
vlc = VLampController()
Ejemplo n.º 20
0
async def ereading():
    ip = get_device_ip(bulb_model)
    bulb = SmartBulb(ip)
    await bulb.update()
    reading = bulb.emeter_realtime
    return {"reading" : reading}
Ejemplo n.º 21
0
 def newdev(self):
     return SmartBulb(self.host)
Ejemplo n.º 22
0
import hashlib
import struct
import pyaudio
from picovoice import Picovoice
import pigpio
import requests
import asyncio
from kasa import SmartBulb
import time

gpio = pigpio.pi()
gpio.set_mode(27, pigpio.OUTPUT)
gpio.set_mode(17, pigpio.OUTPUT)
gpio.set_mode(22, pigpio.OUTPUT)

lights = SmartBulb('192.168.0.18')

colours = {
    'black': (0, 0, 0),
    'blue': (0, 0, 10),
    'green': (0, 10, 0),
    'orange': (10, 5, 0),
    'pink': (10, 2, 2),
    'purple': (10, 0, 10),
    'red': (10, 0, 0),
    'white': (10, 10, 10),
    'yellow': (10, 10, 0),
    'warm': (10, 5, 2),
    'cold': (8, 8, 10)
}
Ejemplo n.º 23
0
 def __init__(self, bulb_one_name, bulb_two_name):
     bulbs = self.discover_bulbs(bulb_one_name, bulb_two_name)
     self.bulb1 = SmartBulb(bulbs[0])
     self.bulb2 = SmartBulb(bulbs[1])