|
| 1 | +# CLAUDE.md -- AI Assistant Guide for sections-grid-layout |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +**sections-grid-layout** is a Home Assistant Lovelace custom **view** plugin that places native HA `hui-section` elements into a CSS Grid with Jinja template evaluation and responsive design support. |
| 8 | + |
| 9 | +- **Version**: 0.1.0-alpha |
| 10 | +- **License**: MIT |
| 11 | +- **Author**: Stormsys |
| 12 | +- **HA minimum version**: 2024.2.0 (native Sections view required) |
| 13 | +- **Distribution**: HACS |
| 14 | +- **Compiled output**: `sections-grid-layout.js` |
| 15 | + |
| 16 | +The project registers one custom element (`sections-grid-layout`) and two patches. It coexists safely with the stock `layout-card`. |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +## Repository Layout |
| 21 | + |
| 22 | +``` |
| 23 | +/ |
| 24 | +├── src/ |
| 25 | +│ ├── main.ts # Entry point |
| 26 | +│ ├── types.ts # Shared TypeScript interfaces |
| 27 | +│ ├── helpers.ts # Selector options constant |
| 28 | +│ ├── template.ts # Template evaluation (pure functions) |
| 29 | +│ ├── grid-utils.ts # Grid area parsing and section helpers (pure functions) |
| 30 | +│ ├── yaml.ts # YAML serializer/parser (pure functions) |
| 31 | +│ ├── styles.ts # CSS generation, section/overlay style computation (pure functions) |
| 32 | +│ ├── layouts/ |
| 33 | +│ │ └── grid.ts # <sections-grid-layout> -- main file |
| 34 | +│ └── patches/ |
| 35 | +│ ├── hui-view-editor.ts # Adds "Sections Grid" to view type dropdown |
| 36 | +│ └── hui-card-element-editor.ts # Preserves view_layout on card save |
| 37 | +├── tests/ |
| 38 | +│ ├── template-evaluation.test.ts |
| 39 | +│ ├── grid-utils.test.ts |
| 40 | +│ ├── yaml.test.ts |
| 41 | +│ ├── styles.test.ts |
| 42 | +│ └── build-output.test.ts |
| 43 | +├── rollup.config.js |
| 44 | +├── tsconfig.json |
| 45 | +├── vitest.config.ts |
| 46 | +├── package.json |
| 47 | +├── hacs.json |
| 48 | +├── sections-grid-layout.js # Compiled output -- DO NOT edit |
| 49 | +├── CHANGELOG.md |
| 50 | +└── README.md |
| 51 | +``` |
| 52 | + |
| 53 | +--- |
| 54 | + |
| 55 | +## Build & Test |
| 56 | + |
| 57 | +```bash |
| 58 | +npm install |
| 59 | +npm run build # Production build -> sections-grid-layout.js |
| 60 | +npm run watch # Watch mode (no minification) |
| 61 | +npm test # Run unit tests (vitest) |
| 62 | +npm run test:watch # Watch mode for tests |
| 63 | +``` |
| 64 | + |
| 65 | +--- |
| 66 | + |
| 67 | +## Architecture |
| 68 | + |
| 69 | +``` |
| 70 | +LitElement |
| 71 | +└── GridLayout (src/layouts/grid.ts) |
| 72 | +``` |
| 73 | + |
| 74 | +`GridLayout` extends `LitElement` directly (no base class). |
| 75 | + |
| 76 | +### `grid.ts` |
| 77 | +- **CSS Grid rendering**: Applies `grid-template-areas/columns/rows` from `layout` config |
| 78 | +- **Section management**: Creates/destroys `hui-section` wrappers; maintains `_sectionsCache`; auto-saves new sections to lovelace config |
| 79 | +- **Jinja template evaluation**: Calls pure functions from `template.ts` for `{{ states() }}`, `{{ state_attr() }}`, `{% if %}` in `custom_css`; tracks entities in `_trackedEntities`; debounced via `requestAnimationFrame` |
| 80 | +- **Background images**: Fixed positioning, blur, opacity, template support |
| 81 | +- **Kiosk mode**: `layout.kiosk: true` makes `#root` fixed-position, filling viewport below HA header; edit-mode offset adjusts for tab bar |
| 82 | +- **Layout zoom**: `layout.zoom` applies CSS `zoom` to `#root` |
| 83 | +- **Per-section styling**: `scrollable`, `background`, `backdrop_blur`, `zoom`, `overflow`, `padding`, `tint`, `variables`, `mediaquery` on each section |
| 84 | +- **Edit mode UI**: Section labels with grid-area name; loose-cards container for orphan cards; overlay tester panel |
| 85 | +- **`_updateSectionsLovelace()`**: Pushes fresh `lovelace` AND fresh `config` to each `hui-section` |
| 86 | + |
| 87 | +### Extracted utility modules (pure functions, testable without DOM) |
| 88 | +- **`template.ts`**: `evaluateCssTemplates()`, `evaluateOverlayContent()`, `extractEntitiesFromTemplate()` |
| 89 | +- **`grid-utils.ts`**: `detectAllGridAreas()`, `formatAreaName()`, `ensureSectionsForAllAreas()` |
| 90 | +- **`yaml.ts`**: `sectionConfigToYaml()`, `yamlScalar()`, `parseYaml()` |
| 91 | +- **`styles.ts`**: CSS generators (kiosk, tint, blur, zoom, variables, mediaquery), section/overlay style computation, breakpoint resolution |
| 92 | + |
| 93 | +### `patches/hui-view-editor.ts` |
| 94 | +Adds `sections-grid-layout` to the view type dropdown. Guard flag `_sectionsGridLayoutPatched` prevents double-patching when layout-card is also loaded. |
| 95 | + |
| 96 | +### `patches/hui-card-element-editor.ts` |
| 97 | +Preserves `view_layout` when the card element editor saves changes. |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +## Code Conventions |
| 102 | + |
| 103 | +- Private methods/props: `_` prefix |
| 104 | +- Constants: `UPPER_SNAKE_CASE` |
| 105 | +- `@property()` for external props, `@state()` for internal state |
| 106 | +- Clean up observers in `disconnectedCallback()` |
| 107 | +- Debounce heavy ops with `requestAnimationFrame` |
| 108 | +- Lit 3 only |
| 109 | + |
| 110 | +--- |
| 111 | + |
| 112 | +## Template System (`custom_css`) |
| 113 | + |
| 114 | +| Syntax | Example | |
| 115 | +|---|---| |
| 116 | +| State value | `{{ states('sensor.temperature') }}` | |
| 117 | +| Attribute value | `{{ state_attr('climate.living', 'temperature') }}` | |
| 118 | +| Conditional | `{% if is_state('binary_sensor.dark', 'on') %}...{% endif %}` | |
| 119 | +| Negative | `{% if not is_state('binary_sensor.dark', 'on') %}...{% endif %}` | |
| 120 | + |
| 121 | +--- |
| 122 | + |
| 123 | +## Key Files for Common Tasks |
| 124 | + |
| 125 | +| Task | File | |
| 126 | +|---|---| |
| 127 | +| Grid rendering / section logic | `src/layouts/grid.ts` | |
| 128 | +| Template evaluation | `src/template.ts` | |
| 129 | +| Grid area parsing | `src/grid-utils.ts` | |
| 130 | +| YAML serializer/parser | `src/yaml.ts` | |
| 131 | +| CSS generation / styling | `src/styles.ts` | |
| 132 | +| Shared types | `src/types.ts` | |
| 133 | +| View type dropdown | `src/patches/hui-view-editor.ts` | |
| 134 | +| view_layout preservation | `src/patches/hui-card-element-editor.ts` | |
| 135 | + |
| 136 | +--- |
| 137 | + |
| 138 | +## Compatibility with layout-card |
| 139 | + |
| 140 | +- Only `sections-grid-layout` is registered -- masonry/horizontal/vertical are untouched |
| 141 | +- Patch guard is `_sectionsGridLayoutPatched` (distinct from layout-card's guard) |
| 142 | +- Both can be loaded simultaneously without conflict |
| 143 | + |
| 144 | +--- |
| 145 | + |
| 146 | +## Constraints |
| 147 | + |
| 148 | +- **Never edit `sections-grid-layout.js` directly** -- overwritten on build |
| 149 | +- **HA 2024.2+ only** -- `hui-section` did not exist before that release |
| 150 | + |
| 151 | +--- |
| 152 | + |
| 153 | +## Known Gaps |
| 154 | + |
| 155 | +- `background_blur` is NOT handled in layout-level `mediaquery` overrides (only works at base level) |
0 commit comments