Initial commit
commit
d79162b86f
|
@ -0,0 +1,2 @@
|
||||||
|
/tags
|
||||||
|
/.direnv
|
|
@ -0,0 +1,64 @@
|
||||||
|
{
|
||||||
|
"nodes": {
|
||||||
|
"flake-parts": {
|
||||||
|
"inputs": {
|
||||||
|
"nixpkgs-lib": "nixpkgs-lib"
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1709336216,
|
||||||
|
"narHash": "sha256-Dt/wOWeW6Sqm11Yh+2+t0dfEWxoMxGBvv3JpIocFl9E=",
|
||||||
|
"owner": "hercules-ci",
|
||||||
|
"repo": "flake-parts",
|
||||||
|
"rev": "f7b3c975cf067e56e7cda6cb098ebe3fb4d74ca2",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "hercules-ci",
|
||||||
|
"repo": "flake-parts",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1710695816,
|
||||||
|
"narHash": "sha256-3Eh7fhEID17pv9ZxrPwCLfqXnYP006RKzSs0JptsN84=",
|
||||||
|
"owner": "nixos",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "614b4613980a522ba49f0d194531beddbb7220d3",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "nixos",
|
||||||
|
"ref": "nixos-23.11",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nixpkgs-lib": {
|
||||||
|
"locked": {
|
||||||
|
"dir": "lib",
|
||||||
|
"lastModified": 1709237383,
|
||||||
|
"narHash": "sha256-cy6ArO4k5qTx+l5o+0mL9f5fa86tYUX3ozE1S+Txlds=",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "1536926ef5621b09bba54035ae2bb6d806d72ac8",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"dir": "lib",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"ref": "nixos-unstable",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"inputs": {
|
||||||
|
"flake-parts": "flake-parts",
|
||||||
|
"nixpkgs": "nixpkgs"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": "root",
|
||||||
|
"version": 7
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
{
|
||||||
|
inputs = {
|
||||||
|
flake-parts.url = "github:hercules-ci/flake-parts";
|
||||||
|
nixpkgs.url = "github:nixos/nixpkgs/nixos-23.11";
|
||||||
|
};
|
||||||
|
|
||||||
|
outputs = inputs:
|
||||||
|
inputs.flake-parts.lib.mkFlake { inherit inputs; } {
|
||||||
|
systems = [ "x86_64-linux" ];
|
||||||
|
perSystem = { self', system, lib, config, pkgs, ... }: {
|
||||||
|
packages.default = pkgs.python311Packages.buildPythonApplication {
|
||||||
|
pname = "ha-kiosk-agent";
|
||||||
|
version = "0.0.1";
|
||||||
|
src = ./.;
|
||||||
|
};
|
||||||
|
|
||||||
|
devShells.default = pkgs.mkShell {
|
||||||
|
name = "development shell";
|
||||||
|
nativeBuildInputs = with pkgs.python311Packages; [
|
||||||
|
coloredlogs
|
||||||
|
configargparse
|
||||||
|
ha-mqtt-discoverable
|
||||||
|
];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
|
@ -0,0 +1,102 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import coloredlogs
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import socket
|
||||||
|
import subprocess
|
||||||
|
import time
|
||||||
|
|
||||||
|
from ha_mqtt_discoverable import Settings, DeviceInfo
|
||||||
|
from ha_mqtt_discoverable.sensors import Number, NumberInfo
|
||||||
|
from paho.mqtt.client import Client, MQTTMessage
|
||||||
|
|
||||||
|
coloredlogs.install()
|
||||||
|
logger = logging.getLogger("HA kiosk agent")
|
||||||
|
logger.setLevel(logging.INFO)
|
||||||
|
|
||||||
|
def get_display_brightness():
|
||||||
|
res = subprocess.run(['brightnessctl', '-m', 'info'], capture_output=True)
|
||||||
|
if res.returncode != 0:
|
||||||
|
logger.warn(f'failed to get brightness: exit code { res.returncode }')
|
||||||
|
return -1
|
||||||
|
|
||||||
|
print(res.stdout)
|
||||||
|
stdout = res.stdout.decode("utf-8")
|
||||||
|
brightPerc = stdout.split(',')[3].split('%')[0]
|
||||||
|
return float(brightPerc)
|
||||||
|
|
||||||
|
def set_display_brightness(number: float) -> float:
|
||||||
|
res = subprocess.run(['brightnessctl', '-m', 'set', f'{ number }%'], capture_output=True)
|
||||||
|
if res.returncode != 0:
|
||||||
|
logger.warn(f'failed to set brightness: exit code { res.returncode }')
|
||||||
|
return -1
|
||||||
|
|
||||||
|
stdout = res.stdout.decode("utf-8")
|
||||||
|
brightPerc = stdout.split(',')[3].split('%')[0]
|
||||||
|
return float(brightPerc)
|
||||||
|
|
||||||
|
# To receive number updates from HA, define a callback function:
|
||||||
|
def display_brightness_callback(client: Client, user_data, message: MQTTMessage):
|
||||||
|
number = int(message.payload.decode())
|
||||||
|
logging.info(f"received new value {number} for display brightness")
|
||||||
|
new_brightness = set_display_brightness(number)
|
||||||
|
display_brightness.set_value(new_brightness)
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument("--debug", action='store_true', default=False)
|
||||||
|
parser.add_argument("--device-id", dest='device_id', default=os.environ.get('DEVICE_ID'), required=True)
|
||||||
|
parser.add_argument("--device-name", dest='device_name', default=os.environ.get('DEVICE_NAME'), required=True)
|
||||||
|
parser.add_argument("--mqtt-host", dest='mqtt_host', default=os.environ.get('MQTT_HOST', 'localhost'))
|
||||||
|
parser.add_argument("--mqtt-port", dest='mqtt_port', default=os.environ.get('MQTT_PORT', 1883))
|
||||||
|
parser.add_argument("--mqtt-user", dest='mqtt_user', default=os.environ.get('MQTT_USER'))
|
||||||
|
parser.add_argument("--mqtt-pass", dest='mqtt_pass', default=os.environ.get('MQTT_PASS'))
|
||||||
|
parser.add_argument("--mqtt-client", dest='mqtt_client', default=os.environ.get('MQTT_CLIENT', socket.gethostname()))
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
mqtt_args = {'host': args.mqtt_host}
|
||||||
|
if args.debug:
|
||||||
|
mqtt_args['debug'] = args.debug
|
||||||
|
|
||||||
|
if args.mqtt_port:
|
||||||
|
mqtt_args['port'] = args.mqtt_port
|
||||||
|
|
||||||
|
if args.mqtt_user:
|
||||||
|
mqtt_args['username'] = args.mqtt_user
|
||||||
|
|
||||||
|
if args.mqtt_pass:
|
||||||
|
mqtt_args['password'] = args.mqtt_pass
|
||||||
|
|
||||||
|
if args.mqtt_client:
|
||||||
|
mqtt_args['client_name'] = args.mqtt_client
|
||||||
|
|
||||||
|
# Configure the required parameters for the MQTT host
|
||||||
|
mqtt_settings = Settings.MQTT(**mqtt_args)
|
||||||
|
|
||||||
|
# Define the device. At least one of `identifiers` or `connections` must be supplied
|
||||||
|
device_info = DeviceInfo(name=args.device_name, identifiers=args.device_id)
|
||||||
|
|
||||||
|
# Information about the `number` entity.
|
||||||
|
number_info = NumberInfo(
|
||||||
|
name="Display Brightness",
|
||||||
|
min=0,
|
||||||
|
max=100,
|
||||||
|
mode="slider",
|
||||||
|
device=device_info,
|
||||||
|
unique_id=f'{args.device_id}_display_brightness')
|
||||||
|
|
||||||
|
settings = Settings(mqtt=mqtt_settings, entity=number_info)
|
||||||
|
|
||||||
|
# Instantiate the number
|
||||||
|
display_brightness = Number(settings, display_brightness_callback)
|
||||||
|
|
||||||
|
logger.info('finished setup')
|
||||||
|
|
||||||
|
brightness = get_display_brightness()
|
||||||
|
logger.info(f'current brightness: { brightness }')
|
||||||
|
display_brightness.set_value(brightness)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
time.sleep(30)
|
Loading…
Reference in New Issue