7.4 KiB
AGENTS.md
Compact orientation for OpenCode sessions working in this repo. Read this before editing; it only records things that are easy to get wrong.
User preferences
- Default assistant tone: caveman full every new chat
(see
.cursor/rules/caveman-always.mdc). Opt out: saystop caveman/normal mode. - When giving shell examples or running commands for this repo on Windows,
chain with PowerShell using
;instead of&&. On Linux/bash,&&is fine.
Stack
- .NET 10 / C# 14 (
LangVersion=14.0).global.jsonpins SDK10.0.0withrollForward=latestMajorandallowPrerelease=true. - NetDaemon v5 (NuGet
26.11.0) apps run insideHomeAssistant.Server, the only host / binary-deploy target. Solution file:HomeAssistantSDK.sln. - Tests: xUnit + FluentAssertions + Moq.
Microsoft.Extensions.TimeProvider.Testingis used for time-based tests — preferFakeTimeProviderover real time. - AutoMapper 16 is used by
HomeAssistant.Server. A hardcoded license key is committed inHomeAssistant.Server/program.cs— do not move it to settings, and do not log or echo it.
Layout (non-obvious wiring)
- Each monitor follows a fixed 3-project pattern:
*.Models— constants, enums,*Options(bound fromappsettings.jsonviaIOptions<>).*.Manager— services + the[NetDaemonApp]entry*Init.cs.*.Manager.Tests— unit tests for the manager.
- Apps are not auto-discovered by the host. Each monitor exposes a
Add<Name>Monitor(this IServiceCollection, IConfiguration)extension in*ServiceRegistration.cs;program.cscalls them explicitly and in a specific order. Adding a new monitor means: create the 3 projects, add a*ServiceRegistration.cs, reference the projects fromHomeAssistant.Server, and wireAdd<Name>+Configure<*Options>inprogram.cs. *ServiceRegistration.cshonors anEnabledflag in the options and no-ops (skipsAddNetDaemonApp) when disabled.PowerMonitorships disabled by default inappsettings.json.HomeAssistant.Entitiesholds the generatedHomeAssistantGeneratedcode (entity metadata sourced fromHomeAssistant.Server/NetDaemonCodegen/{EntityMetaData,ServicesMetaData}.json).program.cscallsAddHomeAssistantGenerated(); the generated entities are transient. Do not hand-edit generated files.HomeAssistant.McpServeris a separate stdio MCP server (ModelContextProtocol NuGet) exposing HA tools. It is not referenced by the host; it runs standalone viadotnet run --project HomeAssistant.McpServer. Cursor wires it up in.cursor/mcp.jsonas thehomeassistantMCP server.HomeAssistant.HistoryReplayis a Blazor debug web app that replays HA history through monitor business logic with no side effects. See its own README. Not deployed.NordPool.Comminication(sic — typo is intentional, keep it) is the Nord Pool HTTP client project referenced byNordPool.Manager.
Critical DI rule
NetDaemon registers IHaContext as scoped. Any service that injects
IHaContext (or IMqttEntityManager, IEntities, generated entity
collections) must be registered scoped or transient, never singleton,
or app load fails with:
Cannot resolve scoped service … from root provider.
Verified-livable registrations (do not regress these to singleton):
Boiler:IBoilerMonitorEntityServicetransient,IBoilerConfigReaderscoped (inprogram.cs).NordPool:INordPoolEntityServicetransient,INordPoolPriceServicescoped,INordPoolBoilerConfigProviderscoped,ICheapestIntervalsFindersingleton (no HA dep).Power:IPowerMonitorEntityService+IPowerNotificationServicetransient,IPowerSensorDiscoveryServicescoped.IOtherPowerCalculationServiceIPowerCostCalculationServiceare singleton (pure calc, no HA dep).
Shower:IExhaustFanControlServicescoped,IShowerMonitorEntityServicetransient,IShowerDetectionService+IHumidityRateCalculatorsingleton (no HA dep).Light:IPresenceService+ILightControlServicescoped.Battery:IBatterySensorDiscoveryService+IBatteryNotificationServicescoped.Leak:ILeakNotificationServicescoped.IEntityManageris transient inprogram.cs.
Singletons are only safe for services that take no scoped dependency
(pure calculators, trackers with no IHaContext). When unsure, register
scoped.
Live validation after monitor edits
.cursor/rules/live-validation.mdc (globs: all *.Manager/*.Models of
Boiler, Light, NordPool, Power, Shower, plus HomeAssistant.McpServer)
requires, after editing monitor runtime code:
dotnet buildthe affected project.RunSmokeTestMCP tool from thehomeassistantMCP server.ValidateEntitiesfor the changed service's critical entities.
Domain → entity mapping is in the rule file. Skip when HA is offline (check
via check_api), when changes are tests/docs/non-runtime, or when the user
says to skip. On failure, report the failing entities + states; do not
silently proceed. This rule is Cursor-specific but the intent applies to
OpenCode sessions too — if you have the homeassistant MCP server
available, follow the same flow.
Commands
Build everything:
dotnet build HomeAssistantSDK.sln
Run all tests:
dotnet test HomeAssistantSDK.sln
Single test project / class / test:
dotnet test BoilerMonitor.Manager.Tests
dotnet test BoilerMonitor.Manager.Tests --filter "FullyQualifiedName~BoilerControlServiceTests"
dotnet test BoilerMonitor.Manager.Tests --filter "FullyQualifiedName~BoilerControlServiceTests.TurnsOnWhenCheap"
Run the host (needs HA reachable + token in appsettings.Development.json):
dotnet run --project HomeAssistant.Server
Run the MCP server standalone:
dotnet run --project HomeAssistant.McpServer
Run the history replay web app:
dotnet run --project HomeAssistant.HistoryReplay
Deploy the host: publish HomeAssistant.Server self-contained and copy the
output to /config/netdaemon4 (the NetDaemon add-on folder). The csproj
prints the reminder after publish.
Configuration & secrets
- Monitor options live under per-monitor sections in
HomeAssistant.Server/appsettings.json(e.g.BoilerMonitor,ShowerMonitor,LightMonitor,BatteryMonitor,LeakMonitor,PowerMonitor,NordPool). Each*Optionsclass has aSectionNameconst used by bothprogram.csand*ServiceRegistration. appsettings.Development.json/appsettings.*.local.jsonare git-ignored across the repo — put tokens and local overrides there. Nested keys can be set via env vars using__as the separator.- Never commit HA long-lived access tokens.
Gotchas
- Monitor
*Optionsare bound twice: once inprogram.cs(Configure<*Options>) and once inside*ServiceRegistration(which reads them again to checkEnabledand to seed stateless singletons likeHumidityRateCalculator). Keep both in sync when changing option names. HomeAssistant.Serverreferences every monitor's*.Managerand*.Modelsprojects directly;HomeAssistant.HistoryReplayreferences a subset. Adding a monitor to the replay tool requires a new project reference there too.- The repository folder name is misspelled
homeassistant-app-deamon(should bedaemon). This is intentional/legacy — do not "fix" it in paths; scripts and.cursor/mcp.jsonuse${workspaceFolder}. NordPool.Comminicationis also misspelled — keep it; the project reference depends on the exact name.