Files
HomeAssistant/AGENTS.md
T
gitea_admin a9f329ffcd Remove the MCP server and split NordPool today/tomorrow fetch decisions
- Drop the HomeAssistant.McpServer wiring from opencode.json along with the
  Cursor live-validation hook, rule and skill; update AGENTS.md and README
- Rename ShouldFetchNewPrices to ShouldFetchTodaysPrices and add a separate
  ShouldFetchTomorrowPrice that waits until the CET publish hour
  (TomorrowPricesPublishHour)
- Inject TimeProvider into NordPoolPriceService, use the CET zone for the
  current date and record fetch timestamps from it
- Rework the NordPool tests around FakeTimeProvider and cover fetching only
  today before the publish hour
2026-12-01 13:46:19 +02:00

8.2 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: say stop 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, set in HomeAssistant.Server.csproj; no Directory.Build.props). global.json pins SDK 10.0.0 with rollForward=latestMajor and allowPrerelease=true.
  • NetDaemon v5 (NuGet 26.11.0) apps run inside HomeAssistant.Server, the only host / binary-deploy target. Solution file: HomeAssistantSDK.sln.
  • Tests: xUnit + FluentAssertions + Moq. Microsoft.Extensions.TimeProvider.Testing is used for time-based tests — prefer FakeTimeProvider over real time.
  • AutoMapper 16.1.1 is used by HomeAssistant.Server. A hardcoded license key is committed in HomeAssistant.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 — entity/config interfaces, constants, enums.
    • *.Manager — services, the [NetDaemonApp] entry *Init.cs, and *Options (bound from appsettings.json via IOptions<>). Note: *Options lives in *.Manager/Models/, not in the separate *.Models project, for 6 of 7 monitors. Only LightMonitor puts LightMonitorOptions in LightMonitor.Models.
    • *.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.cs calls them explicitly and in a specific order. Adding a new monitor means: create the 3 projects, add a *ServiceRegistration.cs, reference the projects from HomeAssistant.Server, and wire Add<Name> + Configure<*Options> in program.cs. NordPool breaks the naming convention: its extension is AddNordPool, not AddNordPoolMonitor. AddNordPool and AddPowerMonitor take no IConfiguration parameter — neither registration reads options (PowerMonitor checks Enabled inside PowerMonitorInit instead).
  • *ServiceRegistration.cs honors an Enabled flag in the options and no-ops (skips AddNetDaemonApp) when disabled — for every monitor except NordPool (no Enabled option, always registers) and PowerMonitor (registers unconditionally; PowerMonitorInit reads Enabled itself). PowerMonitor ships disabled by default in appsettings.json; all others ship enabled.
  • HomeAssistant.Entities holds the generated HomeAssistantGenerated code (entity metadata sourced from HomeAssistant.Server/NetDaemonCodegen/{EntityMetaData,ServicesMetaData}.json). program.cs calls AddHomeAssistantGenerated(); the generated entities are transient. Do not hand-edit generated files.
  • program.cs calls Configure<*Options> then Add<Name>… for each monitor in this order: NordPool, Power, Boiler, Shower, Light, Battery, Leak. Monitor service registrations (except IBoilerConfigReader, IEntityManager, TimeProvider.System, which are in program.cs) live inside each *ServiceRegistration.cs, not program.cs.
  • HomeAssistant.Models is a thin shared model project (one file, EntityCreateModel.cs) referenced by HomeAssistant.Entities. NetDaemon MQTT extension lives here.
  • Mappings is a class library holding AutoMapper profiles (GlobalMappingProfile, NordPoolMappingProfile + DateTimeTypeConverter) referenced by HomeAssistant.Server; wired in program.cs via config.AddProfile<…>(). AutoMapper 16.1.1.
  • HomeAssistant.HistoryReplay is a Blazor debug web app that replays HA history through monitor business logic with no side effects. It references a subset of monitors (Boiler, NordPool, Power, Shower, Light — not Battery or Leak). See its own README. Not deployed.

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 lifetimes; re-check scoped dependencies if changing one):

  • Boiler: IBoilerMonitorEntityService transient, IBoilerControlService/IBoilerRunTimeTracker/IWaterReadyService singleton (no HA dep), IBoilerConfigReader scoped (registered directly in program.cs, the only monitor service outside its *ServiceRegistration.cs).
  • NordPool: INordPoolEntityService transient, INordPoolPriceService singleton (pure state/math, no HA dep — HA fetch call lives in NordPoolInit), INordPoolBoilerConfigProvider scoped, ICheapestIntervalsFinder singleton (no HA dep).
  • Power: IPowerMonitorEntityService + IPowerNotificationService transient, IPowerSensorDiscoveryService scoped. IOtherPowerCalculationService
    • IPowerCostCalculationService are singleton (pure calc, no HA dep).
  • Shower: IExhaustFanControlService scoped, IShowerMonitorEntityService transient, IShowerDetectionService + IHumidityRateCalculator singleton (no HA dep).
  • Light: IPresenceService + ILightControlService scoped.
  • Battery: IBatterySensorDiscoveryService + IBatteryNotificationService scoped.
  • Leak: ILeakNotificationService scoped.
  • IEntityManager is transient in program.cs.

Singletons are only safe for services that take no scoped dependency (pure calculators, trackers with no IHaContext). When unsure, register scoped.

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 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 *Options class has a SectionName const used by both program.cs and *ServiceRegistration.
  • appsettings.Development.json / appsettings.*.local.json are 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 *Options are bound twice: once in program.cs (Configure<*Options>) and once inside *ServiceRegistration (which reads them again to check Enabled and to seed stateless singletons like HumidityRateCalculator). Keep both in sync when changing option names.
  • HomeAssistant.Server references every monitor's *.Manager and *.Models projects directly; HomeAssistant.HistoryReplay references a subset (Boiler, NordPool, Power, Shower, Light). Adding a monitor to the replay tool requires a new project reference there too.
  • The repository folder name is misspelled homeassistant-app-deamon (should be daemon). This is intentional/legacy — do not "fix" it in paths.
  • Stale untracked folders NordPool.Comminication/ and HomeAssistant.Infrastructure/ may appear on disk containing only obj/ build cruft. They are not in the solution, not committed to git, and not referenced by any project. Do not treat them as real projects; safe to delete.