Development Guide¶
This document provides detailed instructions for developing and contributing to the Home Assistant Kubernetes integration.
Prerequisites¶
- Python 3.13 or higher
- Home Assistant 2025.7 or higher
- A Kubernetes cluster for testing (minikube, kind, or cloud-based)
- Git
Development Environment Setup¶
1. Clone the Repository¶
2. Development Container (Recommended)¶
The easiest way to get started is using the provided devcontainer which provides a complete Home Assistant development environment.
Prerequisites¶
Quick Start¶
- Open the project in VS Code:
- Start the devcontainer:
- Press
F1and select "Dev Containers: Reopen in Container" -
Or click the green "Reopen in Container" button when prompted
-
Wait for setup to complete (first run takes a few minutes)
-
Home Assistant will start automatically after setup
-
Access Home Assistant at http://localhost:8123
- Wait about a minute for Home Assistant to fully start up
What's Included¶
The devcontainer automatically provides:
- ✅ Complete Home Assistant installation
- ✅ Your integration automatically mounted and available
- ✅ All development dependencies (ruff, pytest, mypy)
- ✅ Debug logging pre-configured
- ✅ VS Code extensions for Python development
- ✅ Quick test scripts and development helpers
Development Workflow¶
- Edit your integration code directly in VS Code
- Restart Home Assistant to apply changes:
- Use the restart script:
/config/restart_ha.sh - Or in HA: Settings → System → Restart
- Check logs:
- Settings → System → Logs in HA UI
- Or view log file:
tail -f /config/logs/home-assistant.log
Testing Your Integration¶
Configure the integration by editing .devcontainer/configuration.yaml:
kubernetes:
host: "https://your-cluster-endpoint"
token: "your-service-account-token"
verify_ssl: false # for development clusters
Or use the Home Assistant UI: Settings → Integrations → Add Integration
Debugging¶
Enable Python debugging:
- Uncomment in
.devcontainer/configuration.yaml:
- Restart Home Assistant
- In VS Code: Run and Debug → "Python: Remote Attach" → localhost:5678
Code quality tools (run automatically on save):
ruff check custom_components/- lintingruff format custom_components/- formattingpytest tests/- run tests
3. Manual Setup (Alternative)¶
If you prefer to set up manually:
# Create virtual environment
python3 -m venv venv
source venv/bin/activate
# Install dependencies
pip install -e ".[dev]"
# Create Home Assistant config directory
mkdir -p config/custom_components
ln -sf "$(pwd)/custom_components/kubernetes" "config/custom_components/kubernetes"
Project Structure¶
homeassistant-kubernetes/
├── custom_components/
│ └── kubernetes/
│ ├── __init__.py # Main integration file (panel registration, WS setup)
│ ├── manifest.json # Integration metadata
│ ├── config_flow.py # Configuration flow
│ ├── const.py # Constants and configuration keys
│ ├── coordinator.py # Data coordinator (polling + Watch API)
│ ├── sensor.py # Sensor platform
│ ├── binary_sensor.py # Binary sensor platform
│ ├── switch.py # Switch platform (workload control)
│ ├── services.py # HA services (scale, start, stop)
│ ├── device.py # Device registry management
│ ├── kubernetes_client.py # Kubernetes API client
│ ├── websocket_api.py # WebSocket API for the sidebar panel
│ ├── frontend/
│ │ └── kubernetes-panel.js # Built panel JS bundle (committed)
│ └── translations/
│ └── en.json # English translations
├── frontend/ # Panel source (Lit + TypeScript + Vite)
│ ├── package.json
│ ├── tsconfig.json
│ ├── vite.config.ts
│ ├── eslint.config.js
│ ├── .prettierrc
│ └── src/
│ ├── kubernetes-panel.ts # Root panel element with tab navigation
│ ├── views/
│ │ ├── k8s-overview.ts # Overview tab
│ │ ├── k8s-nodes-table.ts # Nodes tab
│ │ ├── k8s-pods-table.ts # Pods tab
│ │ ├── k8s-workloads.ts # Workloads tab
│ │ └── k8s-settings.ts # Settings tab
│ └── utils/
│ └── load-ha-elements.ts # HA element lazy loader
├── tests/
│ ├── conftest.py
│ ├── test_init.py
│ ├── test_websocket_api.py
│ └── ...
├── docs/
├── pyproject.toml
└── README.md
Frontend Development¶
The sidebar panel is built with Lit 3 (TypeScript) and bundled with Vite into a single ES module. Minification is disabled because esbuild's variable mangling breaks Lit's tagged template literals.
Setup¶
Build¶
npm run build # One-time build → custom_components/kubernetes/frontend/kubernetes-panel.js
npm run dev # Watch mode for development
The built kubernetes-panel.js is committed to the repository so HACS users get a working panel without needing Node.js.
Lint & Format¶
npm run lint # ESLint check
npm run lint:fix # ESLint auto-fix
npm run format:check # Prettier check
npm run format # Prettier auto-fix
Testing¶
Running Tests¶
# Run all tests
pytest
# Run tests with verbose output
pytest -v
# Run specific test file
pytest tests/test_kubernetes_integration.py
# Run tests with coverage
pytest --cov=custom_components/kubernetes
Setting Up a Test Kubernetes Cluster¶
Using minikube¶
# Install minikube
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
# Start minikube
minikube start
# Create test resources
kubectl create namespace test-namespace
kubectl run nginx --image=nginx -n test-namespace
kubectl expose pod nginx --port=80 -n test-namespace
Using kind¶
# Install kind
curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.20.0/kind-linux-amd64
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind
# Create cluster
kind create cluster
# Create test resources
kubectl create namespace test-namespace
kubectl run nginx --image=nginx -n test-namespace
Creating Test ServiceAccount¶
# serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: homeassistant-test
namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: homeassistant-test
subjects:
- kind: ServiceAccount
name: homeassistant-test
namespace: default
roleRef:
kind: ClusterRole
name: view
apiGroup: rbac.authorization.k8s.io
Apply the configuration:
Get the token:
kubectl get secret $(kubectl get serviceaccount homeassistant-test -o jsonpath='{.secrets[0].name}') -o jsonpath='{.data.token}' | base64 --decode
Code Quality¶
Code Formatting¶
Type Checking¶
Pre-commit Hooks¶
Install pre-commit hooks:
Integration Testing¶
Manual Testing¶
- Start Home Assistant with your development configuration:
-
Open Home Assistant in your browser (usually http://localhost:8123)
-
Go to Settings → Devices & Services → Add Integration
-
Search for "Kubernetes" and add it
-
Configure the integration with your test cluster details
-
Verify that sensors are created and updating
Automated Testing¶
Create integration tests in tests/ directory:
import pytest
from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from custom_components.kubernetes.const import DOMAIN
async def test_load_unload_config_entry(hass: HomeAssistant) -> None:
"""Test loading and unloading the config entry."""
config_entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_CLUSTER_NAME: "test-cluster",
CONF_HOST: "test-cluster.example.com",
# ... other config
},
)
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert await hass.config_entries.async_unload(config_entry.entry_id)
await hass.async_block_till_done()
Debugging¶
Enable Debug Logging¶
Add to your configuration.yaml:
Common Issues¶
General Issues¶
- Connection refused: Check if the Kubernetes API server is accessible
- Authentication failed: Verify the API token is correct
- SSL certificate errors: Check CA certificate configuration
- Permission denied: Ensure the ServiceAccount has proper RBAC permissions
Devcontainer Issues¶
- Integration not loading:
- Check
/config/custom_components/kubernetes/exists in the container - Verify
manifest.jsonis valid -
Check Home Assistant logs:
ha logs --filter kubernetes -
Permission issues:
- Ensure Docker has permission to mount volumes
-
On Linux, you may need to adjust file ownership
-
Port conflicts:
-
If port 8123 is in use, update
forwardPortsin.devcontainer/devcontainer.json -
Container won't start:
- Try rebuilding: F1 → "Dev Containers: Rebuild Container"
- Check Docker is running and has sufficient resources
Useful devcontainer commands:
# Start Home Assistant
/config/start_ha.sh &
# Restart Home Assistant
/config/restart_ha.sh
# Check setup
/config/check_setup.sh
# Test integration
python /config/test_integration.py
# View logs
tail -f /config/logs/home-assistant.log
# Check running processes
ps aux | grep hass
Contributing¶
Pull Request Process¶
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Make your changes
- Add tests for new functionality
- Ensure all tests pass:
pytest - Format your code:
ruff check --fix . && ruff format . - Commit your changes:
git commit -m 'Add amazing feature' - Push to the branch:
git push origin feature/amazing-feature - Open a Pull Request
Code Review Checklist¶
- Code follows Home Assistant integration patterns
- All tests pass
- Code is properly formatted
- Type hints are included
- Documentation is updated
- No sensitive information is exposed