# # Manage the light devices within the home. # # Three types of devices controlled via HTTP endpoints: # 1. I2C via Arduino ethernet proxy # 2. Pico # 3. Shelly # import sys import logging import quart import aiohttp from easydict import EasyDict as edict import quart import util import shelly APP = sys.modules['__main__'].APP LOGGER = logging.getLogger(__name__) class Light: # Subclasses should implement: turn_on, turn_off, toggle pass class I2CLight(Light): ### deprecated, moving to Pico and/or Shelly pass class PicoLight(Light): ### we might skip this, and go full-Shelly pass class LightingSystem: "All lighting devices in the home." def __init__(self, cfg): self.devices = { } ### read YAML and define all lights ### for now, hand-wire the shelly for idx, ip in enumerate(cfg.lights.split(',')): dev = f'stairs-{idx}' LOGGER.debug(f'ADD: dev="{dev}" at {ip}') self.devices[dev] = shelly.Device(ip) async def turn_on(self, dev, color='white'): await self.devices[dev].turn_on(color=color) async def turn_off(self, dev): await self.devices[dev].turn_off() async def toggle(self, dev): await self.devices[dev].toggle() async def all_on(self, color='white'): for d in self.devices.values(): await d.turn_on(color=color) async def all_off(self): for d in self.devices.values(): await d.turn_off() @APP.route('/l/all-on') async def all_on(): print('ARGS:', quart.request.args) color = quart.request.args.get('color', 'white') await APP.lights.all_on(color=color) return quart.Response('', status=204) @APP.route('/l/all-off') async def all_off(): await APP.lights.all_off() return quart.Response('', status=204) @APP.route('/l/on/') async def light_on(dev): await APP.lights.turn_on(dev) return quart.Response('', status=204) @APP.route('/l/off/') async def light_off(dev): await APP.lights.turn_off(dev) return quart.Response('', status=204) @APP.route('/lights') @APP.use_template('templates/lights.ezt') async def lights_page(): data = edict( message='message goes here', user='gstein', title='Lighting', leftnav=util.build_active_leftnav('lights'), ) return data