fix vmware, fix hass script
This commit is contained in:
@@ -1,18 +1,89 @@
|
||||
{ pkgs ? import <nixpkgs> {} }:
|
||||
{ pkgs, ... }:
|
||||
let
|
||||
homeassistant-api = pkgs.python3.pkgs.buildPythonPackage rec {
|
||||
pname = "homeassistant_api";
|
||||
version = "5.0.0";
|
||||
format = "pyproject";
|
||||
src = pkgs.fetchPypi {
|
||||
inherit pname version;
|
||||
sha256 = "sha256-UNKTtgInrVJtjHb1WVlUbcbhjBOtTX00eHmm54ww0rY=";
|
||||
};
|
||||
|
||||
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
|
||||
'';
|
||||
# do not run tests
|
||||
doCheck = false;
|
||||
nativeBuildInputs = with pkgs.python3.pkgs; [ poetry-core requests-cache ];
|
||||
dependencies = with pkgs.python3.pkgs; [
|
||||
requests-cache
|
||||
pydantic
|
||||
websockets
|
||||
];
|
||||
propagatedBuildInputs = with pkgs.python3.pkgs; [
|
||||
aiohttp
|
||||
aiohttp-client-cache
|
||||
requests
|
||||
simplejson
|
||||
|
||||
requests-cache
|
||||
pydantic
|
||||
websockets
|
||||
];
|
||||
pythonRelaxDeps = [
|
||||
"requests-cache"
|
||||
"pydantic"
|
||||
"websockets"
|
||||
];
|
||||
pythonImportsCheck = [
|
||||
"homeassistant_api"
|
||||
];
|
||||
};
|
||||
|
||||
pythonEnv = pkgs.python3.withPackages (ps: [
|
||||
homeassistant-api
|
||||
]);
|
||||
|
||||
waybar-hass = pkgs.writeScriptBin "waybar-hass" ''
|
||||
#!${pythonEnv}/bin/python
|
||||
|
||||
import os
|
||||
import argparse
|
||||
import time
|
||||
from homeassistant_api import Client
|
||||
|
||||
hass_url = 'http://homeassistant.local:8123/api'
|
||||
|
||||
parser = argparse.ArgumentParser(prog='hass python wrapper')
|
||||
parser.add_argument('--toggle_light')
|
||||
args = parser.parse_args()
|
||||
|
||||
def loadKey():
|
||||
token_path = "/run/secrets/desktop/hass_token"
|
||||
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()
|
||||
'';
|
||||
in
|
||||
{
|
||||
home.packages = [ waybar-hass ];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user