How bramstroker/homeassistant-powercalc works
1. Repository shape
- This is a Home Assistant custom integration centered in custom_components/powercalc.
- The repo also contains a large bundled device profile library under profile_library/, documentation under docs/, measurement tooling under utils/measure/, and CI/validation automation in .github/.
2. Runtime boundary: Home Assistant integration
- The main runtime lives inside Home Assistant and follows the standard custom integration lifecycle: manifest, config flow, config entries, entity platforms, services, diagnostics, and repairs.
- There is no standalone server; PowerCalc is loaded into HA and creates entities there.
3. User-facing entry points
- config_flow.py and flow_helper/ implement the setup and editing UI for different sensor types and power-modeling modes.
- service/ exposes HA services for operations like reloading, switching profiles, recalibrating, and GUI configuration changes.
- sensor.py, select.py, and services.yaml are the main HA-facing entity/service registration points.
4. Core domain model: estimated power and derived energy
- The integration’s job is to estimate power consumption and expose it as HA sensors.
- sensors/ contains the entity implementations for power, energy, daily energy, utility meter integration, and group sensors.
- Energy-related flows are derived from power estimates plus HA’s energy/utility-meter concepts, rather than direct metering hardware.
5. Power modeling subsystem
- strategy/ contains the algorithms used to compute virtual power: fixed, linear, LUT-based, multi-switch, playbook, WLED, and composite strategies.
- strategy/factory.py and selector.py choose the correct strategy based on configuration and entity state.
- This is the main calculation boundary: raw HA state in, watt estimate out.
6. Device profile subsystem
- power_profile/ resolves manufacturer/model-specific power profiles and their sub-profiles.
- Profiles can come from local bundled data, local filesystem content, or remote sources via the loader layer in power_profile/loader/.
- The profile format is data-driven: JSON model descriptors plus LUT CSVs (brightness, color_temp, hs, effect, etc.), often gzipped.
7. Profile library as a data product
- profile_library/ is effectively a structured database of supported devices.
- Its hierarchy is manufacturer → model → optional sub-profile, with manufacturer.json, library.json, and schema files defining the catalog structure.
- This library is consumed by the integration and validated by scripts/tests/CI; it is a key architectural boundary between code and device knowledge.
8. Discovery and automatic binding
- discovery.py and device_binding.py connect HA entities to known profiles and supported device types.
- The integration can infer capabilities from entity attributes, registry metadata, or discovered integrations, reducing manual configuration.
9. Group power aggregation
- sensors/group/ and group_include/ implement aggregation and inclusion rules for group sensors.
- Different group modes handle custom groups, domain-based groups, subtractive groups, standby-aware groups, and tracked/untracked entity sets.
- This is the main “many entities to one virtual sensor” boundary.
10. Data quality and correction
- filter/ contains outlier filtering used to smooth noisy measurements or profile data.
- analytics/, repairs.py, diagnostics.py, and errors.py provide observability, user-facing fixes, and troubleshooting support around bad config or unexpected data.
11. Offline tooling and measurement pipeline
- utils/measure/ is a separate Python application used to gather real-world device measurements and generate the profile data that powers the library.
- It includes controllers for lights, fans, media players, chargers, and power meters, plus OCR/manual capture paths and runners that execute measurement flows.
- This tooling is outside the HA runtime but directly feeds the profile library and model quality.
12. Library validation and release automation
- .github/scripts/, utils/library/, and CI workflows validate LUT files, JSON model schemas, translations, and HACS metadata.
- Release, docs, plot generation, and profile-library update workflows indicate the repo ships both code and curated data as a maintained package.
- For an architecture diagram, this repo should be shown as a code-and-data system: Home Assistant integration runtime on one side, curated device model library and measurement/validation toolchain on the other.
13. Supporting concerns
- translations/ and Crowdin workflows support multi-language UI strings.
- migrate.py, configuration/, and migrate/update scripts handle config evolution and library maintenance.
- Tests mirror the major subsystems, especially strategies, power profile loading, config flows, sensors, and utility scripts, which reflects the repo’s modular architecture.
Main entry points: power_profile Profiles · __init__.py PowerCalc init · profile_library Profile library · sensors Sensors
Components 22
HA runtime
Home Assistant custom integration that creates virtual power and energy entities.
- PowerCalc init · Integration bootstrap and config entry lifecycle inside Home Assistant.
custom_components/powercalc/__init__.py - Manifest · Home Assistant manifest and package identity.
custom_components/powercalc/manifest.json - Setup flow · User-facing setup, edit, and sensor-creation flows.
custom_components/powercalc/config_flow.py - Flow helper · Shared flow builders, schemas, previews, and step orchestration.
custom_components/powercalc/flow_helper - Services · Service registration and GUI configuration actions exposed to Home Assistant.
custom_components/powercalc/service - Sensor entry · Top-level entity/platform registration for sensors and selects.
custom_components/powercalc/sensor.py - Select entry · Select entity/platform registration for user controls.
custom_components/powercalc/select.py - Sensors · Power, energy, daily-energy, utility meter, and aggregation entities.
custom_components/powercalc/sensors - Strategies · Virtual power algorithms that turn HA state into watt estimates.
custom_components/powercalc/strategy - Discovery · Matches HA entities and integrations to supported device profiles.
custom_components/powercalc/discovery.py - Device binding · Infers device capabilities and profile matches from registry and entity metadata.
custom_components/powercalc/device_binding.py - Outlier filter · Smoothing and outlier rejection for noisy measurements and profile data.
custom_components/powercalc/filter - Analytics · Observability and usage analytics hooks.
custom_components/powercalc/analytics - Repairs · User-facing repair flows for broken config or unexpected state.
custom_components/powercalc/repairs.py - Diagnostics · Diagnostic payload generation for support and debugging.
custom_components/powercalc/diagnostics.py
Device knowledge
Bundled profile library and the loader/resolution layer that turns device data into models.
- Profiles · Loads and resolves device profiles and sub-profiles.
custom_components/powercalc/power_profile - Profile library · Bundled manufacturer/model/sub-profile database of curated power models.
profile_library - Library schema · Catalog and model schemas plus top-level library metadata.
profile_library/library_schema.json
Tooling
Measurement, validation, documentation, and CI automation around the library and integration.
- Measure app · Offline measurement tooling that captures device behavior and generates profiles.
utils/measure - Library tools · Scripts for scanning LUT quality, validating model JSON, and building library reports.
utils/library - CI scripts · GitHub workflows and scripts that validate LUTs, translations, HACS metadata, and releases.
.github - Docs · Repository documentation for setup, strategies, library structure, and measurement workflows.
docs
Connections 27
- PowerCalc init → Manifest: declares · Integration metadata used by Home Assistant.
- PowerCalc init → Setup flow: starts setup · Config entry setup and edit flow entry point.
- PowerCalc init → Services: registers · Exposes integration services.
- PowerCalc init → Sensor entry: sets up · Registers entity platforms and config entry runtime.
- Sensor entry → Sensors: creates · Instantiates power, energy, and group sensors.
- Sensor entry → Select entry: creates · Instantiates select entities for controls.
- Setup flow → Flow helper: uses · Shares schemas, helpers, and previews across flows.
- Services → Flow helper: orchestrates · Uses shared flow/UI logic for service-driven changes.
- Sensors → Strategies: consumes · Power sensors depend on selected modeling strategies.
- Sensors → Profiles: consumes · Library-backed sensors derive power from device profiles.
- Strategies → Profiles: consults · Some strategies rely on profile data or sub-profile selection.
- Discovery → Device binding: coordinates · Discovery feeds binding decisions and supported-device inference.
- Discovery → Profiles: matches · Maps discovered entities to catalog profiles.
- Device binding → Profiles: resolves · Binds entities to the best-fit profile or sub-profile.
- Outlier filter → Sensors: supports · Used to smooth measurements before entity exposure.
- Analytics → Diagnostics: feeds · Support and troubleshooting data complement analytics.
- Repairs → Diagnostics: explains · Repairs help users resolve diagnosed problems.
- Profile library → Library schema: defined by · Catalog structure and model format are schema-driven.
- Profiles → Profile library: loads · Runtime resolves bundled device knowledge from the library.
- Measure app → Profile library: produces · Measurement runs generate model data for the library.
- Library tools → Profile library: validates · Checks LUTs, model JSON, and catalog consistency.
- CI scripts → Library tools: runs · CI invokes validation and release helper scripts.
- CI scripts → Profile library: checks · Workflows validate the bundled library before release.
- CI scripts → PowerCalc init: tests · CI verifies integration behavior and packaging.
- Docs → Strategies: documents · Explains the virtual power modeling approaches.
- Docs → Profiles: documents · Describes the curated device profile system.
- Docs → Measure app: documents · Covers offline measurement and profile generation workflows.