as flask without docker

This commit is contained in:
2026-09-06 15:53:58 +02:00
parent b7f98c2ccf
commit 1e18920ec6
8 changed files with 164 additions and 165 deletions
+69 -44
View File
@@ -1,24 +1,29 @@
# Lightweight Hue history
# Hue RRD Flask dashboard
A small service for Raspberry Pi that reads the Philips Hue v2 API, stores history in
fixed-size [RRDtool](https://oss.oetiker.ch/rrdtool/) databases, and serves its own web
dashboard. It does not require Grafana, VictoriaMetrics, Node.js, or a browser-side
charting library.
A lightweight Flask application for Raspberry Pi that reads the Philips Hue v2 API,
stores history in fixed-size RRDtool databases, and serves a local web dashboard.
The dashboard contains:
- One graph for every room and zone. Unavailable, off, and on lights form a stacked
percentage area, with average intensity of shining lights drawn as a line.
- Temperature and illuminance graphs for every available Hue sensor.
- Temperature and illuminance graphs for every Hue sensor.
- 6-hour, 24-hour, 7-day, and 30-day time ranges.
RRD storage is bounded automatically: raw samples are retained for 7 days,
RRD storage remains bounded automatically: raw samples are retained for 7 days,
approximately five-minute averages for 90 days, and hourly averages for two years.
## Configure Hue
## Install on Raspberry Pi OS or Debian
Find the bridge IP in the Hue app under **Settings → My Hue system → System
information**. Then create an application key while physically near the bridge:
Install the native RRDtool binding and Python dependencies:
```bash
sudo apt update
sudo apt install python3 python3-flask python3-requests python3-rrdtool fonts-dejavu-core
```
Create the Hue application key if you do not have one yet. Press the bridge link
button immediately before running:
```bash
curl -k -X POST https://BRIDGE_IP/api \
@@ -26,52 +31,72 @@ curl -k -X POST https://BRIDGE_IP/api \
-d '{"devicetype":"local-hue-collector"}'
```
Press the bridge link button immediately before running the command. The returned
`username` is the application key.
The returned `username` is the application key.
## Run with Docker Compose
Configure and run the Flask application from the project directory:
```bash
cp .env.example .env
# Edit .env with the bridge IP and application key.
docker compose up --build -d --remove-orphans
```
Open `http://PI_ADDRESS:8000`. RRD data persists in the `hue-rrd-data` Docker volume.
The `--remove-orphans` option removes containers from the previous
VictoriaMetrics/Grafana version; their old named volumes are not deleted.
By default, port 8000 binds to all interfaces. To bind only to the Pi's LAN interface,
put its exact address in `.env`, for example:
```env
PUBLISH_ADDRESS=192.168.178.42
```
## Run directly on Raspberry Pi OS/Debian
This avoids Docker overhead entirely:
```bash
sudo apt update
sudo apt install python3 python3-requests python3-rrdtool fonts-dejavu-core
mkdir -p "$HOME/.local/share/hue-collector"
export HUE_BRIDGE_HOST=192.168.178.2
export HUE_APPLICATION_KEY=your-key
export DATA_DIR="$HOME/.local/share/hue-collector"
set -a
. ./.env
set +a
python3 -m hue_collector
```
The page is served on port 8000. For continuous operation, run it with systemd or use
the Compose setup with `restart: unless-stopped`.
Open `http://PI_ADDRESS:8000`. The RRD files are written to `DATA_DIR`, which defaults
to the project's `data/` directory.
TLS verification is disabled by default because Hue bridges normally use a
self-signed certificate. Set `HUE_VERIFY_TLS=true` if the bridge has a trusted
certificate. The health check is available at `/healthz`.
## Run continuously with systemd
Install the project and environment file:
```bash
sudo install -d /opt/hue-collector
sudo cp -r hue_collector /opt/hue-collector/
sudo cp .env /etc/hue-collector
sudo chmod 600 /etc/hue-collector
sudo cp hue-collector.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable --now hue-collector
```
The included unit uses a restricted dynamic service account and stores RRD files in
`/var/lib/hue-collector`. Inspect it with:
```bash
systemctl status hue-collector
journalctl -u hue-collector -f
```
After updating the application files, restart it with:
```bash
sudo systemctl restart hue-collector
```
## Configuration
- `HUE_BRIDGE_HOST`: Hue bridge IP or hostname; required.
- `HUE_APPLICATION_KEY`: Hue v2 application key; required.
- `HUE_VERIFY_TLS`: verify the bridge certificate; defaults to `false` because Hue
bridges normally use a self-signed certificate.
- `COLLECT_INTERVAL_SECONDS`: sampling interval; defaults to 30 seconds. Changing it
after RRD files have been created does not change those existing files' step.
- `LISTEN_HOST`: Flask bind address; defaults to `0.0.0.0` for LAN access.
- `LISTEN_PORT`: dashboard port; defaults to `8000`.
- `DATA_DIR`: RRD directory; defaults to `data`.
The application is intended for a trusted home network and does not provide
authentication or TLS. Restrict access with the Pi firewall if the network is not
trusted.
## Development
The collector tests do not require RRDtool:
On a Debian-based machine, install `python3-rrdtool` through apt. Flask and Requests
can alternatively be installed from `requirements.txt`. When using a virtual
environment, create it with `--system-site-packages` so it can see the apt-installed
RRDtool module.
```bash
python3 -m unittest discover -s tests