[PR #294] [MERGED] [Feature]: Configure home assistant via environment variables #990

Closed
opened 2026-05-06 12:34:23 +02:00 by BreizhHardware · 0 comments

📋 Pull Request Information

Original PR: https://github.com/maziggy/bambuddy/pull/294
Author: @bambuman
Created: 2/7/2026
Status: Merged
Merged: 2/8/2026
Merged by: @maziggy

Base: 0.1.9bHead: feature/home-assistant-env-vars


📝 Commits (6)

  • 6e3cfae Merge pull request #288 from maziggy/0.1.8.1
  • b27f198 Merge branch 'main' into 0.1.8.1
  • 833a0c6 Merge pull request #289 from maziggy/0.1.8.1
  • eda1f5a Home Assistant: add environment variable configuration support
  • 07d9db4 Localize Home Assistant environment variable UI text
  • 2cff40f Merge branch '0.1.9b' into feature/home-assistant-env-vars

📊 Changes

13 files changed (+635 additions, -43 deletions)

View changed files

📝 .env.example (+6 -0)
📝 backend/app/api/routes/settings.py (+41 -0)
📝 backend/app/api/routes/smart_plugs.py (+14 -7)
📝 backend/app/main.py (+3 -4)
📝 backend/app/schemas/settings.py (+7 -0)
📝 backend/app/services/smart_plug_manager.py (+5 -13)
📝 backend/tests/integration/test_settings_api.py (+248 -0)
backend/tests/unit/test_homeassistant_settings.py (+229 -0)
📝 frontend/src/api/client.ts (+3 -0)
📝 frontend/src/i18n/locales/de.ts (+4 -0)
📝 frontend/src/i18n/locales/en.ts (+4 -0)
📝 frontend/src/i18n/locales/ja.ts (+4 -0)
📝 frontend/src/pages/SettingsPage.tsx (+67 -19)

📄 Description

Description

Adds support for configuring Home Assistant integration via environment variables (HA_URL and HA_TOKEN), enabling automatic zero-configuration setup for Home Assistant add-on deployments. When these environment variables are set, the integration is automatically enabled and the UI shows read-only fields with visual indicators.

Fixes #283

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Code refactoring
  • Performance improvement
  • Test addition or update

Changes Made

Backend:

  • Added centralized get_homeassistant_settings() function following Spoolman settings pattern
  • Environment variables (HA_URL, HA_TOKEN) always override database settings with non-negotiable precedence
  • Added three new metadata fields to AppSettings: ha_url_from_env, ha_token_from_env, ha_env_managed
  • Auto-enable integration when both environment variables are set
  • Updated all HA configuration points (main.py, smart_plug_manager.py, smart_plugs.py) to use centralized function
  • Database values are preserved for seamless fallback when env vars are removed

Frontend:

  • Input fields become read-only with disabled state when controlled by environment variables
  • Added lock icons and "(Environment Managed)" labels to indicate env-controlled fields
  • Toggle displays "Automatically enabled via environment variables" badge when fully env-managed
  • Visual feedback with reduced opacity and cursor-not-allowed styling

Testing:

  • 9 new integration tests covering env var precedence, auto-enable logic, and database fallback
  • 8 new unit tests for get_homeassistant_settings() function
  • Tests cover partial configuration, empty env vars, and database update behavior

Documentation:

  • Added environment variables to .env.example with usage instructions
  • Updated wiki with automatic configuration section for HA add-on users
  • Documented HA_URL and HA_TOKEN in Docker installation guide

Screenshots

home-assistant-env-vars

Testing

  • I have tested this on my local machine
  • I have tested with my printer model: Testing environment (no physical printer required for this feature)
  • Tested with environment variables set (automatic configuration)
  • Tested without environment variables (manual configuration)
  • Tested partial configuration (only one env var set)
  • Tested database fallback after removing env vars
  • All 17 new tests pass successfully

Checklist

  • My code follows the project's coding style
  • I have commented my code where necessary
  • I have updated the documentation (if needed)
  • My changes generate no new warnings
  • I have tested my changes thoroughly

Additional Notes

WIKI update:
https://github.com/maziggy/bambuddy-wiki/pull/4

For Home Assistant Add-on Users:
This feature enables zero-configuration setup. The add-on automatically provides HA_URL=http://supervisor/core and a supervisor-generated token, eliminating the need for manual access token creation and URL configuration.

Backward Compatibility:
Fully backward compatible. Users without environment variables can continue using manual configuration through the UI. Existing database settings are preserved and used when env vars are not present.

Configuration Priority:
Environment variables → Database values. This ensures Home Assistant add-on deployments have reliable, supervisor-managed configuration that cannot be accidentally changed through the UI.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/maziggy/bambuddy/pull/294 **Author:** [@bambuman](https://github.com/bambuman) **Created:** 2/7/2026 **Status:** ✅ Merged **Merged:** 2/8/2026 **Merged by:** [@maziggy](https://github.com/maziggy) **Base:** `0.1.9b` ← **Head:** `feature/home-assistant-env-vars` --- ### 📝 Commits (6) - [`6e3cfae`](https://github.com/maziggy/bambuddy/commit/6e3cfae9e28362345e76dbbc00552cbac56c535b) Merge pull request #288 from maziggy/0.1.8.1 - [`b27f198`](https://github.com/maziggy/bambuddy/commit/b27f198386a62c474f799cc6d2cb6829eef67c41) Merge branch 'main' into 0.1.8.1 - [`833a0c6`](https://github.com/maziggy/bambuddy/commit/833a0c62d217daa37a3b45cead33c30bdf7b5759) Merge pull request #289 from maziggy/0.1.8.1 - [`eda1f5a`](https://github.com/maziggy/bambuddy/commit/eda1f5a9e7cdb396969b63ff981e10ba1e02edd8) Home Assistant: add environment variable configuration support - [`07d9db4`](https://github.com/maziggy/bambuddy/commit/07d9db43cb72af2de046200b1e24830e7989fdca) Localize Home Assistant environment variable UI text - [`2cff40f`](https://github.com/maziggy/bambuddy/commit/2cff40f2f1a1655691c6c00e18afd7907bc15f03) Merge branch '0.1.9b' into feature/home-assistant-env-vars ### 📊 Changes **13 files changed** (+635 additions, -43 deletions) <details> <summary>View changed files</summary> 📝 `.env.example` (+6 -0) 📝 `backend/app/api/routes/settings.py` (+41 -0) 📝 `backend/app/api/routes/smart_plugs.py` (+14 -7) 📝 `backend/app/main.py` (+3 -4) 📝 `backend/app/schemas/settings.py` (+7 -0) 📝 `backend/app/services/smart_plug_manager.py` (+5 -13) 📝 `backend/tests/integration/test_settings_api.py` (+248 -0) ➕ `backend/tests/unit/test_homeassistant_settings.py` (+229 -0) 📝 `frontend/src/api/client.ts` (+3 -0) 📝 `frontend/src/i18n/locales/de.ts` (+4 -0) 📝 `frontend/src/i18n/locales/en.ts` (+4 -0) 📝 `frontend/src/i18n/locales/ja.ts` (+4 -0) 📝 `frontend/src/pages/SettingsPage.tsx` (+67 -19) </details> ### 📄 Description ## Description Adds support for configuring Home Assistant integration via environment variables (`HA_URL` and `HA_TOKEN`), enabling automatic zero-configuration setup for Home Assistant add-on deployments. When these environment variables are set, the integration is automatically enabled and the UI shows read-only fields with visual indicators. ## Related Issue Fixes #283 ## Type of Change - [ ] Bug fix (non-breaking change that fixes an issue) - [x] New feature (non-breaking change that adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [x] Documentation update - [ ] Code refactoring - [ ] Performance improvement - [x] Test addition or update ## Changes Made **Backend:** - Added centralized `get_homeassistant_settings()` function following Spoolman settings pattern - Environment variables (`HA_URL`, `HA_TOKEN`) always override database settings with non-negotiable precedence - Added three new metadata fields to `AppSettings`: `ha_url_from_env`, `ha_token_from_env`, `ha_env_managed` - Auto-enable integration when both environment variables are set - Updated all HA configuration points (main.py, smart_plug_manager.py, smart_plugs.py) to use centralized function - Database values are preserved for seamless fallback when env vars are removed **Frontend:** - Input fields become read-only with disabled state when controlled by environment variables - Added lock icons and "(Environment Managed)" labels to indicate env-controlled fields - Toggle displays "Automatically enabled via environment variables" badge when fully env-managed - Visual feedback with reduced opacity and cursor-not-allowed styling **Testing:** - 9 new integration tests covering env var precedence, auto-enable logic, and database fallback - 8 new unit tests for `get_homeassistant_settings()` function - Tests cover partial configuration, empty env vars, and database update behavior **Documentation:** - Added environment variables to `.env.example` with usage instructions - Updated wiki with automatic configuration section for HA add-on users - Documented HA_URL and HA_TOKEN in Docker installation guide ## Screenshots <img width="1940" height="1091" alt="home-assistant-env-vars" src="https://github.com/user-attachments/assets/4bb08e56-567b-417f-8bdb-def27b7ec368" /> ## Testing - [x] I have tested this on my local machine - [ ] I have tested with my printer model: Testing environment (no physical printer required for this feature) - [x] Tested with environment variables set (automatic configuration) - [x] Tested without environment variables (manual configuration) - [x] Tested partial configuration (only one env var set) - [x] Tested database fallback after removing env vars - [x] All 17 new tests pass successfully ## Checklist - [x] My code follows the project's coding style - [x] I have commented my code where necessary - [x] I have updated the documentation (if needed) - [x] My changes generate no new warnings - [x] I have tested my changes thoroughly ## Additional Notes **WIKI update:** https://github.com/maziggy/bambuddy-wiki/pull/4 **For Home Assistant Add-on Users:** This feature enables zero-configuration setup. The add-on automatically provides `HA_URL=http://supervisor/core` and a supervisor-generated token, eliminating the need for manual access token creation and URL configuration. **Backward Compatibility:** Fully backward compatible. Users without environment variables can continue using manual configuration through the UI. Existing database settings are preserved and used when env vars are not present. **Configuration Priority:** Environment variables → Database values. This ensures Home Assistant add-on deployments have reliable, supervisor-managed configuration that cannot be accidentally changed through the UI. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
BreizhHardware 2026-05-06 12:34:23 +02:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/bambuddy#990
No description provided.