add bedroom light control to waybar

This commit is contained in:
mjallen18
2024-09-04 16:09:05 -05:00
parent 0b6074e7dd
commit 81f9d46d9b
9 changed files with 93 additions and 9 deletions

View File

@@ -0,0 +1,18 @@
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
# The Nix packages provided in the environment
packages = [
pkgs.python312
pkgs.python3Packages.pip
# Whatever other packages are required
];
shellHook = ''
export TMPDIR=/tmp
export VENV_DIR=/tmp/lights
mkdir $VENV_DIR
python -m venv $VENV_DIR/.venv
source $VENV_DIR/.venv/bin/activate
pip install homeassistant-api
'';
}

View File

@@ -0,0 +1,42 @@
import os
import argparse
import time
from homeassistant_api import Client
hass_url = 'https://hass.mjallen.dev/api'
parser = argparse.ArgumentParser(prog='hass python wrapper')
parser.add_argument('--toggle_light')
args = parser.parse_args()
def loadKey():
token_path = ""
for folder in range(1,100):
if os.path.exists("/run/secrets.d/" + str(folder) + "/desktop/hass_token"):
token_path = "/run/secrets.d/" + str(folder) + "/desktop/hass_token"
break
with open(token_path, "r") as key_file:
key = key_file.readline()
return key
def toggle_light(client, light):
lights = client.get_domain("light")
lights.toggle(entity_id=light)
time.sleep(0.5)
light_entity = client.get_entity(entity_id=light)
state = light_entity.get_state()
if state.state == 'on':
return "󰛨"
else:
return "󰹏"
def main():
token = loadKey()
with Client(hass_url, token) as client:
if args.toggle_light is not None:
status = toggle_light(client=client, light=args.toggle_light)
print("{ text: \"" + status + "\" }")
main()

View File

@@ -1,4 +1,6 @@
#!/usr/bin/env python
#!/usr/bin/env nix-shell
#! nix-shell -i python3 --pure
#! nix-shell -p python3
import os
import json

View File

@@ -1,4 +1,6 @@
#!/usr/bin/env python
#!/usr/bin/env nix-shell
#! nix-shell -i python3 --pure
#! nix-shell -p python3 python3Packages.requests
import json
from datetime import datetime