class TpLinkHandler(SmartDeviceException):
    def __init__(self, address):
        self.device = SmartPlug(address)

    def update(self):
        asyncio.run(self.device.update())

    def update_two(self):
        asyncio.create_task(self.device.update())

    def shutdown_btn(self):
        asyncio.create_task(self.device.turn_off())
        return "shutdown"

    def turnOn_btn(self):
        asyncio.create_task(self.device.turn_on())
        return "Turning on"

    def shutdown(self):
        asyncio.run(self.device.turn_off())
        return "shutdown"

    def turnOn(self):
        asyncio.run(self.device.turn_on())
        return "Turning on"

    def get_plug_information(self):
        return self.device.hw_info

    def __repr__(self):
        pass
Esempio n. 2
0
class PlugDevice:
    def __init__(self, config, mqtt):
        self.mqtt = mqtt
        self.plug = SmartPlug(config['host'])
        self.device_id = config['device_id']

    def update(self):
        asyncio.run(self._update())

    def subscribe(self, mqtt):
        mqtt.subscribe(f"{self.device_id}/states/switch_on")

    def on_message(self, msg):
        value = msg.strip().lower()
        if value == "true" or value == "yes":
            print('Turning on ' + self.device_id)
            asyncio.run(self.plug.turn_on())
            self.update()
        elif value == "false" or value == "no":
            print('Turning off ' + self.device_id)
            asyncio.run(self.plug.turn_off())
            self.update()

    async def _update(self):
        await self.plug.update()
        print(self.plug.alias)
        print(self.plug.is_on)

        self.mqtt.publish(f"{self.device_id}/values/is_on",
                          str(self.plug.is_on).lower(), 1)
class TpLinkHandlerSmartPlug(SmartDeviceException):
    def __init__(self, address):
        self.device = SmartPlug(address)

    def update(self):
        asyncio.run(self.device.update())

    def update_two(self):
        asyncio.create_task(self.device.update())

    def update_three(self):
        future = asyncio.run_coroutine_threadsafe(self.device.update(),
                                                  self.worker.loop)
        result = future.result()

    def shutdown_btn(self, settings):
        asyncio.create_task(self.device.turn_off())
        return "shutdown"

    def turnOn_btn(self, settings):
        asyncio.create_task(self.device.turn_on())
        return "Turning on"

    def shutdown(self):
        asyncio.run(self.device.turn_off())
        return "shutdown"

    def turnOn(self):
        asyncio.run(self.device.turn_on())
        return "Turning on"

    def get_plug_information(self):
        return self.device.hw_info

    def __repr__(self):
        pass
Esempio n. 4
0
def switch_plug():
    p = SmartPlug("192.168.0.51")

    try:
        asyncio.run(p.update())
    except SmartDeviceException as e:
        logging.error("Could not reach smart plug...")
        sys.exit(e)

    if p.is_on:
        asyncio.run(p.turn_off())
    else:
        asyncio.run(p.turn_on())

    asyncio.run(p.update())
    print("The plug is on: ",  p.is_on)
Esempio n. 5
0
def off():
    plug = SmartPlug("192.168.178.23")
    asyncio.run(plug.update())
    asyncio.run(plug.turn_off())
    return redirect("/")
Esempio n. 6
0
class LightControl:

	def __init__(self, ip=None):
		self._ip = ip
		self._plug = None
		self._args = None

	def parse_args(self):
		parser = argparse.ArgumentParser(prog="light")
		parser.add_argument('command', choices=['on', 'off', 'status'])
		parser.add_argument('--verbose', '-v', action='count', default=0)
		parser.add_argument('-ip', help='IP address of smart plug', default=None)
		self._args = parser.parse_args()
		self._debug(self._args, 3)
		self._debug('debug level only goes to 3 (-vvv)', 4)
		if hasattr(self._args, 'ip') and self._args.ip is not None:
			self._debug('loading ip from sys.argv', 3)
			self._ip = self._args.ip
		else:
			self._debug('loading ip from environment', 3)
			load_dotenv()
			self._ip = os.getenv('LIGHT_CONTROL_IP_ADDR')
		if self._ip is None:
			self._debug(f'Invalid IP address (actual {self._ip} expected XX.XX.XX.XX)')
			sys.exit()
		if verify_ip(self._ip) == False:
			self._debug(f'Invalid IP address (actual {self._ip} expected XX.XX.XX.XX)')
			sys.exit()
		self._debug(f'ip address is set to {self._ip}', 1)

	def run_command(self):
		self._initalize_device()
		if self._args.command == 'on':
			self._on()
		elif self._args.command == 'off':
			self._off()
		elif self._args.command == 'status':
			self._status()
		else:
			print(f"command not valid (actual {self._args.command} expected one of {'on', 'off', 'status'}")

	def _initalize_device(self):
		# self._ip
		self._plug = SmartPlug(self._ip)
		asyncio.run(self._plug.update())
		self._debug(f'connected to plug: {self._plug.alias}', 1)

	def _on(self):
		asyncio.run(self._plug.turn_on())
		if self._verify_state(True):
			self._debug('light is turned on', 1)
		else:
			self._debug('light failed to turn on')

	def _off(self):
		asyncio.run(self._plug.turn_off())
		if self._verify_state(False):
			self._debug('light is turned off', 1)
		else:
			self._debug('light failed to turn off')

	def _status(self):
		if self._verify_state(True):
			self._debug('light is turned on', 0)
		else:
			self._debug('light is turned off', 0)


	def _verify_state(self, state):
		asyncio.run(self._plug.update())
		return self._plug.is_on == state

	def _debug(self, msg, level=0):
		if level <= self._args.verbose:
			print("-"*level, end="")
			print(msg)