bluewind/api.py

46 lines
1.4 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
from flask import Flask
from headwind import Headwind as Headwind
app = Flask(__name__)
app.config.from_prefixed_env()
@app.route("/on")
async def on():
fan = Headwind(app, app.config["ADDRESS"])
fan_status = await fan.on()
if not fan_status:
return "<p>Failed to turn headwind on</p>", 503
return "<p>Turning headwind on</p>", 200
@app.route("/sleep")
async def sleep():
fan = Headwind(app, app.config["ADDRESS"])
fan_status = await fan.sleep()
if not fan_status:
return "<p>Failed to put headwind to sleep</p>", 503
return "<p>Putting headwind to sleep</p>", 200
@app.route("/speed/<int:speed>")
async def speed(speed):
fan = Headwind(app, app.config["ADDRESS"])
fan_status = await fan.manualSpeed(speed)
if not fan_status:
return f"<p>Failed to set headwind speed to {speed}</p>", 503
return f"<p>Setting headwind speed to {speed}</p>", 200
@app.route("/hr")
async def hr():
fan = Headwind(app, app.config["ADDRESS"])
fan_status = await fan.hr()
if not fan_status:
return "<p>Failed to set headwind to HR mode</p>", 503
return "<p>Setting headwind to HR mode</p>", 200
@app.route("/off")
async def off():
fan = Headwind(app, app.config["ADDRESS"])
fan_status = await fan.off()
if not fan_status:
return "<p>Failed to turn headwind off</p>", 503
return "<p>Turning headwind off</p>", 200