diff --git a/openspec/changes/auto-detect-gap-closure/.openspec.yaml b/openspec/changes/auto-detect-gap-closure/.openspec.yaml new file mode 100644 index 0000000..1ea7e36 --- /dev/null +++ b/openspec/changes/auto-detect-gap-closure/.openspec.yaml @@ -0,0 +1,2 @@ +schema: spec-driven +created: 2026-09-09 diff --git a/openspec/changes/auto-detect-gap-closure/design.md b/openspec/changes/auto-detect-gap-closure/design.md new file mode 100644 index 0000000..f10e8c7 --- /dev/null +++ b/openspec/changes/auto-detect-gap-closure/design.md @@ -0,0 +1,99 @@ +## Context + +PR #1 established the auto-detection mechanism and the right division of responsibility between the two repos: the **integration owns product ID → beer name** (via its bundled `keg_catalog.json`), and the **card owns product ID → visual identity** (brand colours, keg photography, style metadata). That split is worth preserving — the card is not duplicating the name table, it is joining against it on product ID. + +What went wrong is entirely on the card's side of that join. The card's `kegId` coverage was built against an 86-entry snapshot of the integration catalogue; the integration has since grown to 114 entries with cleaned-up names (`3c4b9ca6`). Half the join keys are missing, and the failure modes are unhelpful: an unmatched ID silently renders as either grey "Custom Beer" or, worse, as Leffe Blonde. + +Two constraints shape the design. First, the card ships a pre-built `dist/perfectdraft-card.js` and has no test harness, so correctness has to come from a checkable data invariant rather than from unit tests. Second, the integration catalogue is upstream and will keep growing, so any fix that is a one-time hand-transcription will drift again. + +The full hand-verified mapping for all 57 unmapped IDs, plus the 12 label divergences, is in [`keg-id-mapping.md`](./keg-id-mapping.md). + +## Goals / Non-Goals + +**Goals:** + +- Every one of the 114 product IDs the integration can report resolves to a catalogue entry with sensible branding. +- No unmapped or absent keg ever causes the card to display a beer that is not in the machine. +- A user can override detection when it is wrong, without editing YAML by hand. +- Mapping drift against the upstream catalogue becomes detectable rather than silent. +- The 20 already-shipped keg photos that are currently unreachable become reachable. + +**Non-Goals:** + +- New keg photography for the 35 new catalogue entries. They use existing brand-tinted fallback art; adding photos is a separate content change. +- Any modification to the integration. Its sensors and catalogue are treated as a fixed upstream contract. +- Reinstating the in-card beer *picker* dialog. PR #1 was right that tapping the label to choose a beer is the wrong primary interaction once detection works; the override belongs in the config editor. +- Auto-fetching the integration catalogue at runtime. The card must work with no extra network access and no assumptions about the integration's file layout. + +## Decisions + +### 1. Curated name wins the label whenever the product ID resolves + +PR #1 chose `validName ?? live.name`, commented as "authoritative PerfectDraft name wins". The premise is wrong: the integration catalogue is closer to a product listing than a display name, carrying pack sizes and qualifiers ("Corona Cero (0.0% abv)", "Kopparberg Strawberry and Lime Cider"), and at the time PR #1 was written it also carried short-date and BBE strings. + +Precedence becomes: + +1. `beer_name` override, if set → resolve through the existing name/slug/custom path. +2. Product ID match → curated catalogue entry, **curated name used verbatim**. +3. No ID match but a usable reported name → generic palette carrying the reported name (existing `resolveBeer` behaviour). +4. Nothing usable → explicit no-keg state. + +The integration's name is still valuable at step 3, where it is the only information available. Rejected alternative: normalising the integration's names by stripping suffixes with regexes. That is a guessing game against upstream copy changes, and the card already has a curated name for exactly these IDs. + +### 2. Explicit no-keg state instead of a catalogue-order fallback + +`resolveBeer(undefined)` returning `getAllBeers()[0]` is a latent bug that PR #1 promoted into a reachable state. The fix is to stop calling `resolveBeer(undefined)` for the not-detected case and render a distinct state instead: neutral palette, no keg photo, and a label that says no keg is detected rather than naming a beer. + +`resolveBeer(undefined)` itself changes to return the neutral `custom` fallback rather than the first catalogue entry, so the trap cannot be re-entered from a future call site. Rejected alternative: keeping the card blank. Temperature and freshness are still meaningful with no keg tapped, and an empty card looks broken. + +### 3. `beer_name` returns as an override, not a default + +The config key comes back with inverted semantics. It was the primary source of truth; it becomes a last-resort override that wins over detection when present. This keeps three things working that PR #1 broke: users with existing YAML, the `custom_beers` feature that is currently orphaned in the types with no reachable entry point, and any keg the catalogue does not know. + +The editor gets an optional override field that is empty by default and labelled as an override, so the normal path stays zero-configuration. Rejected alternative: a `beer_overrides` map keyed by product ID. More precise, but it is a new config surface for a rare case, and it cannot help when the ID is unresolved. + +### 4. Product-ID lookup gets an index + +`getBeerByKegId` is currently `CATALOG.find(...)`, a linear scan on every render, in a module that already builds `slugIndex` and `nameIndex` at load. It becomes a third `Map` built in the same loop. Minor performance point, but the real motivation is that building the index is where duplicate `kegId` values become detectable. + +### 5. Drift detection as a build-time check, not a runtime feature + +A `npm run check:catalog` script compares the card's `kegId` set against a copy of the integration catalogue committed under `docs/` or `scripts/` as reference data, and fails on: an ID in the integration catalogue with no card entry, a `kegId` not present in the integration catalogue, or a duplicate `kegId`. + +This is the substitute for the tests the repo does not have, and it turns "the catalogues have drifted" into a failing command. Committing a snapshot of the upstream catalogue is deliberate: it keeps the check hermetic and makes each upstream sync an explicit, reviewable diff. Rejected alternative: fetching the catalogue from GitHub during the check. Non-hermetic and offline-hostile. + +### 6. Stale-integration detection reuses the existing entity resolution + +If the device resolves and a temperature sensor is found but neither keg sensor exists, the integration predates 0.4.0. That is distinguishable from "no keg tapped" (sensors exist, states are unknown/unavailable) and deserves its own message pointing at the integration update, because no amount of card configuration will fix it. + +This also forces a fix to the `_resolveEntities` early return, `if (this._entityIds.temperature) return;`. That latch predates PR #1 but is now consequential: a user who updates the card before the integration latches on temperature alone and never discovers the keg sensors for the life of the card element. Resolution instead completes when the expected set is found, and re-runs while any are missing. + +## Risks / Trade-offs + +- **The 35 new catalogue entries need brewery, style, ABV and brand colours researched by hand, and errors there are invisible without the real keg.** → Table B of the mapping reference fixes the ID and name from upstream, so only the cosmetic metadata is judgement. Wrong brand colours degrade to slightly-off tinting, not a wrong beer name. Keep the palettes conservative where the brand is unfamiliar. + +- **Table A's mappings are human judgement about which product corresponds to which catalogue entry, and a naive fuzzy match got 5 of 22 wrong.** → Those five are called out explicitly in the reference file. The residual risk is a beer rendering with a close sibling's artwork, which is visibly wrong to the owner of that keg and cheap to correct. + +- **Reinstating `beer_name` risks re-normalising manual selection and undoing the point of PR #1.** → It is absent from `getStubConfig`, empty by default in the editor, and labelled as an override. A user who never touches it never sees it. + +- **The committed upstream catalogue snapshot will itself go stale.** → It is reference data for a check, not runtime data, so a stale snapshot degrades to a weaker check rather than wrong behaviour. The check's own output names the missing IDs when it is re-synced. + +- **Changing 12 existing labels is a visible change for users who currently see the integration's names.** → Table C lists every one so the diff is reviewable. All 12 move toward the more accurate curated name. + +## Migration Plan + +1. Land the catalogue and lookup changes first (tables A and B). Coverage rises to 100% with no behaviour change beyond more beers resolving. +2. Land the detection precedence, label source and no-keg state. This is where the 12 labels from table C change and where the Leffe Blonde fallback disappears. +3. Reinstate the `beer_name` override in types, editor and detection. +4. Add the catalogue drift check and wire it alongside `npm run lint`. +5. Update `README.md`, bump `package.json` and `EDITOR_VERSION`, rebuild `dist/`. + +Rollback is per-step and low-risk: every step is additive to the merged state except step 2, which is the one behavioural change and is revertable on its own. + +Users on integration < 0.4.0 see the new stale-integration message. That is a strict improvement over today, where they silently get Leffe Blonde. + +## Open Questions + +- Should the no-keg state hide the glass-count matrix entirely, or show it at zero? Showing zero glasses against a neutral palette is probably clearer that the machine is empty rather than the card being broken, but this is a visual call best made against a real dashboard. +- `42863` "1L Stein" is merchandise, not a beer. It gets a catalogue entry so the ID resolves, but it may warrant a distinct presentation rather than being dressed up as a beer. +- Where the upstream catalogue snapshot should live — `scripts/` next to the check, or `docs/` as reference material. No strong argument either way. diff --git a/openspec/changes/auto-detect-gap-closure/keg-id-mapping.md b/openspec/changes/auto-detect-gap-closure/keg-id-mapping.md new file mode 100644 index 0000000..84e2533 --- /dev/null +++ b/openspec/changes/auto-detect-gap-closure/keg-id-mapping.md @@ -0,0 +1,111 @@ +# Reference: integration product ID → card catalogue mapping + +Source of truth: `custom_components/perfectdraft/keg_catalog.json` in +[hassio-integration-perfectdraft-pro](https://github.com/Falkvinge/hassio-integration-perfectdraft-pro), +114 entries as of integration commit `3c4b9ca6` ("Complete the keg name catalog: 86 -> 114 kegs"). + +Baseline: the card catalogue after PR #1 (`d0748e7`) maps 57 of those 114 IDs. +The three tables below cover the remaining 57 IDs plus the 12 label divergences. +Every mapping in table A was checked by hand; a naive fuzzy match got five of +them wrong (see the notes under the table). + +## A. Add `kegId` to an existing card catalogue entry — 22 IDs + +| Product ID | Integration name | Card slug | Card curated name | Ships keg art? | +|---|---|---|---|---| +| `797` | Leffe Rituel | `leffe-rituel` | Leffe Rituel 9° | yes | +| `2565` | Hoegaarden Rose | `hoegaarden-rosee` | Hoegaarden Rosée | yes | +| `3550` | Leffe Winter | `leffe-winter` | Leffe Winter | no | +| `29775` | Goose Island Midway | `goose-midway` | Goose Midway Session IPA | yes | +| `29896` | Kwak | `kwak` | Pauwel Kwak | yes | +| `31718` | Leffe Summer | `leffe-dete` | Leffe d'Été | yes | +| `32864` | Leffe Amber | `leffe-amber` | Leffe Ambrée | yes | +| `33515` | La Virgen Madrid | `la-virgen` | La Virgen Lager | yes | +| `33917` | Tiny Rebel Clwb Tropica | `tiny-rebel-clwb` | Tiny Rebel Clwb Tropicana | yes | +| `41588` | Schneider's Bayrisch Hell | `schneider` | Schneider Bayrisch Hell | yes | +| `42352` | St Feuillien Blonde | `saint-feuillien` | Saint-Feuillien Blonde | yes | +| `42527` | Castelain Grand Cru | `castelain` | Castelain Grand Cru | yes | +| `42703` | Brewdog Elvis Juice | `brewdog-elvis-juice` | BrewDog Elvis Juice | yes | +| `42920` | Adnams Ghost Ship | `adnams` | Adnams Ghost Ship | no | +| `44040` | Hertog Jan Grand Pilsener | `hertog-jan-grand` | Hertog Jan Grand Pilsener | yes | +| `44387` | Mont Blanc La Rousse | `rousse-mont-blanc` | Rousse du Mont Blanc | yes | +| `44425` | Camden IPA | `camden-ipa` | Camden IPA | yes | +| `47362` | Leffe Prestige | `leffe-prestige` | Leffe Prestige 1240 | yes | +| `47389` | Northern Monk A Little Faith | `northern-monk` | Northern Monk A Little Faith | yes | +| `47494` | Proper Job | `st-austell` | St Austell Proper Job IPA | yes | +| `47523` | Anosteké Blonde | `anoesteke` | Anosteké Blonde | yes | +| `47774` | Old Speckled Hen | `old-speckled-hen` | Old Speckled Hen | yes | +Notes on the non-obvious rows: + +- `29775` "Goose Island Midway" is the card's `goose-midway` (Goose Midway Session IPA), **not** `goose-island-ipa`. +- `31718` "Leffe Summer" is the card's `leffe-dete` (Leffe d'Été). +- `44387` "Mont Blanc La Rousse" is the card's `rousse-mont-blanc` (Rousse du Mont Blanc). +- `47494` "Proper Job" is the card's `st-austell` (St Austell Proper Job IPA). +- `3550` and `42920` map to existing entries that carry no keg photo; they will use fallback art. + +## B. Requires a new card catalogue entry — 35 IDs + +None of these have a plausible existing entry. They need `slug`, `name`, `brewery`, +`style`, `abv`, brand `colors` and a `kegId`. No keg photography is expected — the +tiered-visual path will render brand-tinted fallback art. + +`42863` "1L Stein" is merchandise rather than a beer, but the machine can report it, +so it needs an entry to avoid an unresolved-ID state. + +| Product ID | Integration name | +|---|---| +| `759` | Diekirch Grand Cru | +| `760` | Diekirch Premium | +| `1470` | Diekirch Christmas | +| `2167` | Leffe Spring | +| `2452` | Leffe Royal Whitbread Golding | +| `9156` | Leffe Royal Cascade | +| `29745` | Lowenbrau Oktoberfestbier | +| `32814` | Romola | +| `33642` | Ginette Bio Refreshing Blonde | +| `33918` | Spaten Oktoberfestbier | +| `34944` | Leffe La Légère | +| `35168` | Goose Island Golden Goose | +| `35344` | Ginette Bio White | +| `36703` | Tiny Rebel Pineapple Express IPA | +| `37005` | Tiny Rebel Cali Pale | +| `37038` | Crew Republic Drunken Sailor | +| `41832` | Mikkeller Heated Seats | +| `42415` | Chernigivske | +| `42528` | Palm | +| `42641` | Kwak Rouge | +| `42756` | Innis & Gunn Lager Beer | +| `42778` | Mikkeller Game of Thrones | +| `42863` | 1L Stein | +| `42876` | Orchard Pig Reveller Cider | +| `43033` | Hertog Jan Bockbier | +| `43806` | Magic Rock Magic Orange | +| `43807` | Brewdog Lost Lager | +| `44238` | BrewDog Hopped in Harmony | +| `44331` | Ninkasi Flower Lager | +| `44474` | Camden Marzen | +| `44536` | Tiny Rebel Stay Puft | +| `44543` | Via Roma | +| `47429` | Camden Lager Top | +| `47816` | Corona Ligera | +| `48134` | Kopparberg Crisp Apple | +## C. Label divergence on IDs the card already maps — 12 IDs + +The card recognises these product IDs but currently displays the integration's +catalogue name. Once the label prefers the curated name, these revert to the +left-hand column. Listed so the change in rendered output is reviewable. + +| Product ID | Card curated name (will be shown) | Integration name (shown today) | +|---|---|---| +| `456` | Diebels Alt | Diebels | +| `500` | Hasseröder Premium | Hasseroder | +| `43006` | Camden Hells | Camden Hells Lager | +| `43235` | Corona Cero | Corona Cero (0.0% abv) | +| `44059` | San Miguel | San Miguel Especial | +| `44090` | Kopparberg Strawberry & Lime | Kopparberg Strawberry and Lime Cider | +| `47144` | Haake-Beck | Haake Beck | +| `47440` | Ninkasi IPA | Ninkasi French IPA | +| `47629` | Camden Eazy | Camden Eazy IPA | +| `47974` | Castelain Ch'ti Blonde | Ch'ti Blonde | +| `47999` | Thatchers Gold | Thatchers Gold Cider | +| `48061` | Northern Monk A Little Faith | Northern Monk Faith | diff --git a/openspec/changes/auto-detect-gap-closure/proposal.md b/openspec/changes/auto-detect-gap-closure/proposal.md new file mode 100644 index 0000000..9121c5e --- /dev/null +++ b/openspec/changes/auto-detect-gap-closure/proposal.md @@ -0,0 +1,45 @@ +## Why + +PR #1 (merged as `d0748e7`) made the card auto-detect the tapped beer from the integration's `keg_product_id` / `keg_name` sensors, and in the same commit deleted the manual beer picker along with the `beer_name` and `beer_entity` config options. The detection mechanism is correct, but its data coverage is not, and with the picker gone there is no longer any way for a user to correct a miss. + +Concretely, against the integration's current 114-entry `keg_catalog.json`: + +- The card maps only **57 of 114** product IDs. The other 57 fall through to name matching, and most of those fail too, landing on the grey "Custom Beer" placeholder. +- **20 of those unmapped IDs correspond to beers whose keg photography already ships in this repo** — a Kwak keg renders as a grey box while `kegs/kwak.webp` sits unused, purely because the integration says "Kwak" and the card says "Pauwel Kwak". +- When detection fails entirely, `resolveBeer(undefined)` returns `getAllBeers()[0]`, so the card confidently displays **Leffe Blonde** — a specific, wrong beer with full Leffe branding. Previously this path was unreachable because the editor forced a beer selection. +- On 12 IDs the card *does* recognise, it still prefers the integration's catalogue name for the label, so curated names regress into product-listing strings: "Corona Cero" becomes "Corona Cero (0.0% abv)", "Thatchers Gold" becomes "Thatchers Gold Cider". + +The integration is the upstream source of truth and has already moved on — commit `3c4b9ca6` grew its catalogue from 86 to 114 entries and cleaned up the names *after* PR #1 was authored. The card's copy was built against the older 86-entry table and is drifting. `README.md` still documents the removed picker and both removed config keys. + +## What Changes + +- Complete the product-ID coverage to all 114 IDs in the integration catalogue: add `kegId` to 22 existing catalogue entries, and add 35 new entries for beers the card does not yet know. +- Prefer the card's own curated beer name for the label whenever the product ID resolves, falling back to the integration-reported name only for unrecognised IDs. +- Replace the Leffe Blonde fallback with an explicit, honest "no keg detected" state so the card never invents a beer. +- **BREAKING (repair)** — reinstate `beer_name` as an optional manual override for unrecognised or mis-detected kegs, restoring the escape hatch PR #1 removed and re-enabling the `custom_beers` config that is currently unreachable. +- Surface a clear message when the paired integration is too old to expose the keg sensors, instead of silently showing a wrong or empty beer. +- Fix the `_resolveEntities` latch so keg sensors appearing after first resolve are still discovered. +- Establish a repeatable way to check the card's mapping against the integration catalogue, so this drift is detectable rather than silent. +- Update `README.md` to match the shipped behaviour and bump the card version. + +## Capabilities + +Note: `openspec/specs/` is currently empty — capability specs for this project live in the unarchived `perfectdraft-card` and `card-layouts-and-imagery` changes. The modified capabilities below refer to those established capability names. + +### New Capabilities +- `keg-auto-detection`: How the card resolves the tapped beer from the integration's keg sensors — product-ID-first matching, name fallback, manual override precedence, the no-keg and stale-integration states, and entity discovery lifecycle. This behaviour shipped in PR #1 with no spec covering it. + +### Modified Capabilities +- `beer-catalog`: Catalogue entries gain a `kegId` product-ID field, coverage becomes a stated requirement tied to the integration catalogue, and lookup by product ID is added alongside lookup by name and slug. +- `beer-label`: The label's name source becomes explicitly defined (curated name preferred over integration-reported name), and the no-beer-detected label state is specified. +- `card-config`: `beer_name` returns as an optional manual override rather than a required default; the removed `beer_entity` key stays removed; the unknown-`beer_name` scenario is restated in terms of override rather than primary selection. + +## Impact + +- `src/beer-catalog.ts` — `kegId` on 57 entries (22 existing, 35 new), product-ID index instead of the current linear scan. +- `src/perfectdraft-card.ts` — detection precedence, label name source, no-keg render state, `_resolveEntities` latch. +- `src/editor.ts` / `src/types.ts` — reinstate the optional `beer_name` override field. +- `README.md` — remove the stale beer-selector and `beer_entity` documentation, document auto-detection and the override. +- `package.json` / `dist/perfectdraft-card.js` — version bump and rebuilt bundle. +- Upstream dependency: requires PerfectDraft integration **0.4.0+** (the release that added `keg_product_id` / `keg_name`). The 114-entry `custom_components/perfectdraft/keg_catalog.json` in that repo is the reference data for this change. +- No new runtime dependencies. Artwork for the 35 new catalogue entries is out of scope; they get brand-tinted fallback art via the existing tiered-visual path. diff --git a/openspec/changes/auto-detect-gap-closure/specs/beer-catalog/spec.md b/openspec/changes/auto-detect-gap-closure/specs/beer-catalog/spec.md new file mode 100644 index 0000000..b64802e --- /dev/null +++ b/openspec/changes/auto-detect-gap-closure/specs/beer-catalog/spec.md @@ -0,0 +1,66 @@ +## ADDED Requirements + +### Requirement: Product ID mapping to the integration catalog +Each catalog entry SHALL be able to carry the PerfectDraft product ID of its keg, so the card can join against the product ID reported by the integration. + +#### Scenario: Entry carries a product ID +- **WHEN** a catalog entry corresponds to a keg listed in the integration's `keg_catalog.json` +- **THEN** the entry SHALL carry a `kegId` field holding that product ID as a string + +#### Scenario: Full coverage of the integration catalog +- **WHEN** the card is loaded +- **THEN** every product ID present in the integration's bundled keg catalog SHALL be carried by exactly one card catalog entry +- **THEN** no `kegId` SHALL appear on more than one entry +- **THEN** no `kegId` SHALL reference a product ID absent from the integration catalog + +#### Scenario: Lookup by product ID +- **WHEN** the card looks up a beer by product ID +- **THEN** the lookup SHALL resolve without scanning the catalog linearly, using an index built once at module load alongside the existing slug and name indexes + +### Requirement: Catalog coverage is verifiable +The project SHALL provide a repeatable check that the card's product ID mapping agrees with the integration's keg catalog, so drift is detected rather than discovered by users. + +#### Scenario: Check detects a missing mapping +- **WHEN** the integration catalog contains a product ID that no card catalog entry carries +- **THEN** the check SHALL fail and SHALL name the unmapped product ID and its integration-reported name + +#### Scenario: Check detects a stale or duplicate mapping +- **WHEN** a card catalog entry carries a `kegId` that is absent from the integration catalog, or two entries carry the same `kegId` +- **THEN** the check SHALL fail and SHALL name the offending entries + +#### Scenario: Check is offline +- **WHEN** the check runs +- **THEN** it SHALL compare against a committed snapshot of the integration catalog and SHALL NOT require network access + +## MODIFIED Requirements + +### Requirement: Built-in PerfectDraft beer database +The card SHALL include a built-in catalog of PerfectDraft beers with metadata for display purposes. + +#### Scenario: Catalog data structure +- **WHEN** the card is loaded +- **THEN** each beer entry in the catalog SHALL contain: a unique slug (kebab-case), display name, brewery name, style, ABV (number), and a brand color palette (primary color, secondary color, text color) +- **THEN** each entry MAY additionally carry a `kegId` product ID, a keg image path, and a brewery logo reference + +#### Scenario: Catalog coverage +- **WHEN** the card is loaded +- **THEN** the catalog SHALL include an entry for every keg the paired integration can identify, and MAY include further entries for beers the integration does not list + +### Requirement: Custom beer entries via configuration +The card SHALL support user-defined beer entries that extend or override the built-in catalog, reachable through the `beer_name` override. + +#### Scenario: Custom beer defined in config +- **WHEN** the card config includes a `custom_beers` array with entries containing at minimum a `name` field +- **THEN** those beers SHALL be resolvable by the `beer_name` override alongside built-in entries +- **THEN** custom entries SHALL support optional fields: `color_primary`, `color_secondary`, `color_text`, `image_url`, `brewery`, `style`, `abv` + +#### Scenario: Custom beer overrides built-in +- **WHEN** a custom beer entry has the same slug as a built-in beer +- **THEN** the custom entry SHALL override the built-in entry's fields + +## REMOVED Requirements + +### Requirement: Future automatic beer identification support +**Reason**: This requirement described automatic identification as a future possibility driven by a user-supplied `beer_entity` pointing at an arbitrary sensor. Automatic identification is now the card's primary behaviour, driven by entities the card resolves itself from the configured device, and the `beer_entity` config key no longer exists. The replacement requirements live in the `keg-auto-detection` capability. + +**Migration**: Users who set `beer_entity` should remove it; detection now happens without configuration. Users who relied on it to point at a non-standard entity should use the `beer_name` override instead. The `keg_id` lookup this requirement anticipated is now specified as the `kegId` product ID mapping above. diff --git a/openspec/changes/auto-detect-gap-closure/specs/beer-label/spec.md b/openspec/changes/auto-detect-gap-closure/specs/beer-label/spec.md new file mode 100644 index 0000000..d39f65c --- /dev/null +++ b/openspec/changes/auto-detect-gap-closure/specs/beer-label/spec.md @@ -0,0 +1,50 @@ +## MODIFIED Requirements + +### Requirement: Beer name display +The card SHALL display the detected beer's name in the label zone below the temperature, using the card's own curated name where it has one. + +#### Scenario: Beer detected +- **WHEN** a beer has been determined by detection or by the `beer_name` override +- **THEN** the card SHALL display that beer's full name as text in the label zone + +#### Scenario: Detected by product ID +- **WHEN** the beer was matched by product ID +- **THEN** the displayed name SHALL be the card catalog entry's curated name, not the name reported by the integration + +#### Scenario: No beer detected +- **WHEN** no beer could be determined +- **THEN** the label zone SHALL indicate that no keg is detected +- **THEN** the label zone SHALL NOT display the name of any catalog beer + +## ADDED Requirements + +### Requirement: Label zone reflects detection state +The label zone's styling SHALL follow the detection state so a user can tell a detected beer from an undetected one at a glance. + +#### Scenario: Beer detected +- **WHEN** a beer has been determined +- **THEN** the label zone SHALL use that beer's brand color palette as its background gradient + +#### Scenario: No beer detected +- **WHEN** no beer could be determined +- **THEN** the label zone SHALL use a neutral palette rather than any brand palette +- **THEN** the card SHALL NOT render a keg photograph or brewery logo + +### Requirement: Label zone is not a beer picker +The label zone SHALL NOT act as a control for choosing a beer, since the beer is detected rather than chosen. + +#### Scenario: Tapping the label zone +- **WHEN** the user taps the label zone in any layout +- **THEN** no beer selection dialog SHALL open + +## REMOVED Requirements + +### Requirement: Beer selection dialog +**Reason**: The beer is now detected from the integration's keg sensors rather than chosen by the user, so a modal picker on the label zone is redundant and misleading — a selection made there would be overwritten on the next update. The dialog, its search field, and its custom-beer grouping were removed when auto-detection landed. + +**Migration**: Users who need to correct or force the displayed beer should set the `beer_name` override in the card's visual editor, which takes precedence over detection. Users whose keg is not recognised should expect this to be fixed by extending the catalog's product ID coverage rather than by selecting manually. + +### Requirement: Beer selection persistence +**Reason**: There is no longer a user selection to persist. Persisting a beer choice in `localStorage` would also actively conflict with detection, since a stale stored value could contradict the keg actually in the machine. The `beer_entity` precedence scenario this requirement described no longer applies, as that config key was removed. + +**Migration**: The `localStorage` beer key is no longer read or written and can be ignored; it is orphaned data on existing installations. Glass size persistence is unaffected and continues to work as before. A durable preference for a specific beer is expressed as the `beer_name` override in config, which persists as part of the dashboard configuration rather than per-browser. diff --git a/openspec/changes/auto-detect-gap-closure/specs/card-config/spec.md b/openspec/changes/auto-detect-gap-closure/specs/card-config/spec.md new file mode 100644 index 0000000..a2d10e6 --- /dev/null +++ b/openspec/changes/auto-detect-gap-closure/specs/card-config/spec.md @@ -0,0 +1,89 @@ +## ADDED Requirements + +### Requirement: Optional manual beer override +The card SHALL accept an optional `beer_name` config value that overrides auto-detection, so a user is never stuck with a wrong or unrecognised beer. + +#### Scenario: Override absent +- **WHEN** the card config has no `beer_name` +- **THEN** the card SHALL rely entirely on auto-detection +- **THEN** `getStubConfig()` SHALL NOT populate `beer_name`, so newly created cards start with detection only + +#### Scenario: Override set +- **WHEN** the card config sets `beer_name` +- **THEN** the card SHALL display the beer resolved from that value regardless of what the keg sensors report + +#### Scenario: Override presented as optional in the editor +- **WHEN** the visual editor is displayed +- **THEN** the beer override field SHALL be empty by default and SHALL be labelled as an override for when detection is wrong +- **THEN** clearing the field SHALL remove `beer_name` from the config and return the card to detection + +## MODIFIED Requirements + +### Requirement: Device-based entity resolution +The card SHALL derive its sensor entity IDs from the configured device, not from manually entered entity IDs or prefixes. + +#### Scenario: Entity resolution at runtime +- **WHEN** the card has a `device_id` in its config +- **THEN** the card SHALL resolve the temperature, keg_remaining, keg_freshness, keg_product_id, and keg_name sensor entities associated with that device +- **THEN** the card SHALL use those resolved entities for all display values + +#### Scenario: Device removed or unavailable +- **WHEN** the configured `device_id` no longer exists in HA or has no associated entities +- **THEN** the card SHALL display an error indicating the device is not found +- **THEN** the error SHALL suggest re-configuring the card via the editor + +#### Scenario: Keg sensors missing from an otherwise valid device +- **WHEN** the device resolves and other sensors are found, but the keg_product_id and keg_name sensors are absent +- **THEN** the card SHALL indicate that the PerfectDraft integration needs updating rather than reporting a device error + +### Requirement: Configuration validation +The card SHALL validate its configuration and display clear errors for invalid config. + +#### Scenario: Missing device configuration +- **WHEN** the card config has no `device_id` +- **THEN** the card SHALL throw an error during `setConfig()` that triggers HA's built-in error card display +- **THEN** the error message SHALL instruct the user to configure the card via the visual editor + +#### Scenario: Invalid glass_size value +- **WHEN** the card config specifies a `glass_size` that is not one of the supported values (250, 330, 500, 568, 473) +- **THEN** the card SHALL fall back to the default of 330 mL + +#### Scenario: Unknown beer_name override +- **WHEN** the card config specifies a `beer_name` that does not match any beer in the catalog (built-in or custom) +- **THEN** the card SHALL treat it as a custom beer with that name and use a generic color palette +- **THEN** the card SHALL NOT fall back to auto-detection, since an explicit override signals the user's intent + +#### Scenario: Removed configuration keys ignored +- **WHEN** the card config contains `beer_entity`, which is no longer supported +- **THEN** the card SHALL ignore it without throwing + +### Requirement: Visual card editor +The card SHALL provide a graphical configuration editor as the primary means of configuration. + +#### Scenario: Editor registration +- **WHEN** `getConfigElement()` is called on the card class +- **THEN** it SHALL return a custom element that renders a configuration form +- **THEN** `getStubConfig()` SHALL return a minimal valid configuration for new card creation (auto-selecting the sole device if only one exists) + +#### Scenario: Editor fields +- **WHEN** the visual editor is displayed +- **THEN** it SHALL provide: a device picker (dropdown of discovered PerfectDraft devices), glass size (dropdown of five options), layout selection, and an optional beer override +- **THEN** all fields SHALL be UI controls — no freeform text input for entity IDs +- **THEN** the editor SHALL NOT present beer selection as a required or default setting + +#### Scenario: Editor fires config-changed events +- **WHEN** the user changes any editor field +- **THEN** the editor SHALL fire a `config-changed` custom event with the updated config +- **THEN** the card preview SHALL update to reflect the change + +### Requirement: Custom beers configuration +The card SHALL support extending the beer catalog via the `custom_beers` config array, reachable through the `beer_name` override. + +#### Scenario: Custom beer entry structure +- **WHEN** a `custom_beers` array entry is provided in config +- **THEN** it SHALL require at minimum a `name` field (string) +- **THEN** it SHALL accept optional fields: `color_primary` (CSS color), `color_secondary` (CSS color), `color_text` (CSS color), `image_url` (URL string), `brewery` (string), `style` (string), `abv` (number) + +#### Scenario: Custom beer selected by override +- **WHEN** `beer_name` matches the `name` of a `custom_beers` entry +- **THEN** the card SHALL display that custom beer using its configured colors and image diff --git a/openspec/changes/auto-detect-gap-closure/specs/keg-auto-detection/spec.md b/openspec/changes/auto-detect-gap-closure/specs/keg-auto-detection/spec.md new file mode 100644 index 0000000..3b33675 --- /dev/null +++ b/openspec/changes/auto-detect-gap-closure/specs/keg-auto-detection/spec.md @@ -0,0 +1,83 @@ +## ADDED Requirements + +### Requirement: Tapped beer detected from integration sensors +The card SHALL determine the tapped beer from the PerfectDraft integration's keg sensors without requiring the user to select it. + +#### Scenario: Keg sensors resolved for the configured device +- **WHEN** the card has a `device_id` and the device exposes sensors with translation keys `keg_product_id` and `keg_name` +- **THEN** the card SHALL resolve both entity IDs and read their states on every `hass` update +- **THEN** the card SHALL NOT require any beer-related configuration for detection to work + +#### Scenario: Product ID matches a catalog entry +- **WHEN** the `keg_product_id` sensor reports a state consisting only of digits that matches the `kegId` of a catalog entry +- **THEN** the card SHALL use that catalog entry for the beer's name, brand colors, style, ABV, and keg artwork + +#### Scenario: Product ID does not match but a name is reported +- **WHEN** the `keg_product_id` state is absent, non-numeric, or matches no catalog entry, AND the `keg_name` sensor reports a state other than `unavailable` or `unknown` +- **THEN** the card SHALL resolve the beer by that reported name against the catalog +- **THEN** if the reported name matches no catalog entry, the card SHALL display that name against the generic color palette + +### Requirement: Detection precedence +The card SHALL apply a single, defined precedence order when determining which beer to display. + +#### Scenario: Precedence order +- **WHEN** the card determines the beer to display +- **THEN** it SHALL apply the first applicable source in this order: (1) the `beer_name` config override, (2) a catalog entry matched by reported product ID, (3) a beer resolved from the reported keg name, (4) the no-keg-detected state +- **THEN** no later source SHALL override an earlier one + +#### Scenario: Override wins over successful detection +- **WHEN** `beer_name` is set in config AND the product ID resolves to a different catalog entry +- **THEN** the card SHALL display the beer named by `beer_name` +- **THEN** the card SHALL NOT silently substitute the detected beer + +### Requirement: Curated name preferred for the label +When the card recognises the reported product ID, it SHALL use its own catalog name for display rather than the name reported by the integration. + +#### Scenario: Product ID resolves and names differ +- **WHEN** the product ID matches a catalog entry AND the `keg_name` sensor reports a different string for the same keg +- **THEN** the card SHALL display the catalog entry's curated name +- **THEN** the card SHALL NOT display the integration-reported name + +#### Scenario: Product ID does not resolve +- **WHEN** the product ID matches no catalog entry AND a usable `keg_name` state is reported +- **THEN** the card SHALL display the integration-reported name, as it is the only name available + +### Requirement: No-keg-detected state +The card SHALL never display a specific beer that it has not detected, and SHALL make the absence of a detected beer visible. + +#### Scenario: Sensors present but no keg reported +- **WHEN** both keg sensors are resolved but neither yields a beer, for example because both report `unknown` or `unavailable` +- **THEN** the card SHALL render a neutral palette with no keg photograph and no brewery logo +- **THEN** the card SHALL display a label indicating that no keg is detected +- **THEN** the card SHALL NOT display the name or branding of any catalog beer + +#### Scenario: Detection never falls back to an arbitrary catalog entry +- **WHEN** no beer can be determined from any source in the precedence order +- **THEN** the card SHALL NOT select a beer by catalog position, insertion order, or any other arbitrary rule + +#### Scenario: Other sensor values still shown +- **WHEN** the card is in the no-keg-detected state AND the temperature or freshness sensors report usable values +- **THEN** the card SHALL continue to display those values + +### Requirement: Integration version guidance +The card SHALL distinguish an out-of-date integration from an empty machine and guide the user accordingly. + +#### Scenario: Device resolved but keg sensors absent +- **WHEN** the configured device resolves and a temperature sensor is found, but no sensor with translation key `keg_product_id` or `keg_name` exists for that device +- **THEN** the card SHALL display a message stating that the PerfectDraft integration must be updated to a version that provides the keg sensors +- **THEN** that message SHALL be distinct from the no-keg-detected state + +#### Scenario: Keg sensors present but idle +- **WHEN** the keg sensors exist for the device but report no usable value +- **THEN** the card SHALL show the no-keg-detected state and SHALL NOT suggest updating the integration + +### Requirement: Entity resolution completes as entities appear +The card SHALL continue attempting entity resolution until the expected sensors are found, so that sensors registered after the card first renders are still discovered. + +#### Scenario: Keg sensors appear after initial resolution +- **WHEN** the card has already resolved a temperature entity for its device AND the keg sensors are subsequently added to that device, for example because the user updated the integration +- **THEN** the card SHALL resolve the keg entities on a following `hass` update without requiring a browser reload + +#### Scenario: Resolution stops once complete +- **WHEN** all expected sensor entities for the device have been resolved +- **THEN** the card SHALL NOT repeat the registry scan on subsequent `hass` updates diff --git a/openspec/changes/auto-detect-gap-closure/tasks.md b/openspec/changes/auto-detect-gap-closure/tasks.md new file mode 100644 index 0000000..b61faee --- /dev/null +++ b/openspec/changes/auto-detect-gap-closure/tasks.md @@ -0,0 +1,53 @@ +## 1. Catalog coverage + +- [ ] 1.1 Commit a snapshot of the integration's 114-entry `keg_catalog.json` as reference data for the drift check, and note the upstream commit it came from +- [ ] 1.2 Add `kegId` to the 22 existing catalog entries listed in table A of `keg-id-mapping.md`, taking care with the five hand-corrected rows called out beneath that table +- [ ] 1.3 Add the 35 new catalog entries from table B, each with slug, curated name, brewery, style, ABV, brand colors and `kegId`; no `imagePath`, so they fall through to brand-tinted fallback art +- [ ] 1.4 Give `42863` "1L Stein" an entry so the ID resolves, and decide whether it presents as merchandise rather than as a beer +- [ ] 1.5 Replace the linear scan in `getBeerByKegId` with a `kegIdIndex` map built at module load alongside `slugIndex` and `nameIndex` + +## 2. Catalog drift check + +- [ ] 2.1 Add a `scripts/check-catalog.mjs` that loads the catalog snapshot and the card catalog and reports unmapped integration IDs, card `kegId` values absent upstream, and duplicate `kegId` values +- [ ] 2.2 Wire it as `npm run check:catalog` with a non-zero exit on any finding, and confirm it passes against the completed catalog from section 1 +- [ ] 2.3 Verify the check actually fails when a `kegId` is temporarily removed, so it is not silently passing + +## 3. Detection precedence and label source + +- [ ] 3.1 Change `resolveBeer(undefined)` to return the neutral `custom` fallback instead of `getAllBeers()[0]`, removing the Leffe Blonde trap at its source +- [ ] 3.2 Rework the detection block in `render()` to apply the precedence order from the `keg-auto-detection` spec: override, then product ID, then reported name, then no-keg +- [ ] 3.3 Use the curated catalog name for the label when the product ID resolves, keeping the integration-reported name only for unresolved IDs; this changes the 12 labels in table C +- [ ] 3.4 Move the detection assignment out of `render()` so `_beer` is not mutated during rendering, and confirm no Lit change-in-update warning appears in the console + +## 4. No-keg and stale-integration states + +- [ ] 4.1 Add an explicit no-keg-detected render state with a neutral palette, no keg photo and no brewery logo, and make sure temperature and freshness still display +- [ ] 4.2 Resolve the open question on whether the glass matrix hides or shows zero in the no-keg state, against a real dashboard +- [ ] 4.3 Detect the case where the device resolves and a temperature sensor exists but neither keg sensor does, and show a message telling the user to update the integration +- [ ] 4.4 Confirm the stale-integration message is visually and textually distinct from the no-keg-detected state + +## 5. Manual override + +- [ ] 5.1 Reinstate `beer_name` as an optional field in `PerfectDraftCardConfig`, leaving `beer_entity` removed +- [ ] 5.2 Wire the override into detection as the highest-precedence source, including resolution against `custom_beers` +- [ ] 5.3 Add an optional, empty-by-default override field to the editor, labelled as a correction for when detection is wrong, and confirm clearing it removes the key from config +- [ ] 5.4 Confirm `getStubConfig()` does not populate `beer_name`, so new cards start on detection only + +## 6. Entity resolution lifecycle + +- [ ] 6.1 Replace the `if (this._entityIds.temperature) return;` latch in `_resolveEntities` with a condition that only stops once the expected sensor set is resolved +- [ ] 6.2 Verify that keg sensors added after first render are picked up on a later `hass` update without a browser reload, and that the scan stops once complete + +## 7. Documentation and release + +- [ ] 7.1 Rewrite the README's beer sections: drop the beer-selector feature bullet, the "select your default beer" setup step and the `beer_entity` YAML line; document auto-detection and the `beer_name` override +- [ ] 7.2 State the PerfectDraft integration 0.4.0+ requirement in the README's Requirements section +- [ ] 7.3 Bump `package.json` and `EDITOR_VERSION`, and refresh the lockfile with the project's normal npm version to avoid unrelated churn +- [ ] 7.4 Run `npm run lint`, `npm run check:catalog` and `npm run build`, and commit the rebuilt `dist/perfectdraft-card.js` +- [ ] 7.5 Confirm the committed bundle is reproducible from source with a clean `npm ci && npm run build` + +## 8. Verification against real hardware + +- [ ] 8.1 Confirm a recognised keg shows the curated name and correct artwork, checking at least one of the 20 table A entries whose photo was previously unreachable +- [ ] 8.2 Confirm an empty machine shows the no-keg state and never names a beer +- [ ] 8.3 Confirm the override wins over a detected keg, and that removing it returns the card to detection