Skip to content

Commit 18a4b71

Browse files
authored
Refactor code structure for improved readability and maintainability.… (#71)
* Refactor code structure for improved readability and maintainability. Modernize the build system and HA features. Implement aditional custom card features to highlight great features. * Enhance Boilerplate Card configuration options: add card style, accent color, attribute limit, and show timestamps settings. * Add layout and display mode options to Boilerplate Card configuration; enhance editor with accordion sections for better organization * Refactor boilerplate card code for improved clarity and organization; enhance comments for better guidance on customization * chore: update custom-card-helpers to version 2.0.0 and modify package resolutions to overrides * refactor: replace deep clone with structuredClone for improved performance and remove unused loadCardHelpers method * feat: add press_action and release_action to BoilerplateCardConfig interface * refactor: enhance type safety in customFireEvent and ActionHandler properties * refactor: update attribute_limit description and change selector mode to slider * Migrate to yarn package manager * docs: add GitHub Copilot instructions and reference files * refactor: standardize environment variable setup in build and release workflows * docs: update instructions for adding custom card in Home Assistant * feat: add version validation and bumping in release workflow * refactor: update devcontainer configuration and remove Dockerfile
1 parent e0402e4 commit 18a4b71

42 files changed

Lines changed: 6639 additions & 6144 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.devcontainer/README.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Home Assistant Custom Card - Dev Container Setup
2+
3+
This directory contains the development container configuration for building and testing the boilerplate-card custom card for Home Assistant.
4+
5+
## Setup Instructions
6+
7+
1. **Install VS Code Remote Containers**
8+
- [VS Code Extension: Remote - Containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers)
9+
10+
2. **Open in Dev Container**
11+
- Open the project folder in VS Code
12+
- Press `Ctrl+Shift+P` (or `Cmd+Shift+P` on Mac)
13+
- Type "Remote-Containers: Reopen in Container"
14+
- Wait for the container to build (first time takes ~2-3 minutes)
15+
16+
3. **Build the Card**
17+
```bash
18+
yarn build # Lint and build
19+
yarn start # Start dev server with hot reload (port 5000)
20+
yarn lint # Check code quality
21+
yarn rollup # Production build
22+
```
23+
24+
4. **Access Services**
25+
- **Dev Container**: Terminal in VS Code (automatic)
26+
- **Home Assistant**: http://localhost:8123 (user: dev/pass: dev)
27+
- **Rollup Dev Server**: http://localhost:5000
28+
29+
5. **Configure Home Assistant to Use Your Card**
30+
- In Home Assistant, go to Settings > Dashboards
31+
- Create a new Dashboard
32+
- Add the card from the GUI
33+
34+
## File Structure
35+
36+
```
37+
.devcontainer/
38+
├── devcontainer.json # VS Code dev container config
39+
├── .gitignore # Ignore HA data
40+
└── README.md # This file
41+
```
42+
43+
## Base Image
44+
45+
This project uses the published image:
46+
47+
- `ghcr.io/custom-cards/custom-card-devcontainer:latest`
48+
49+
The image includes the `container` helper used by the devcontainer lifecycle commands.
50+
51+
## Development Workflow
52+
53+
### Building the Card
54+
55+
```bash
56+
# One-time setup (automatic on container creation)
57+
yarn install
58+
59+
# Development with hot reload
60+
yarn start # Runs Rollup in watch mode on port 5000
61+
62+
# Quality checks
63+
yarn lint # ESLint check
64+
yarn build # Full build pipeline (lint + rollup)
65+
66+
# Production build
67+
yarn rollup # Create optimized dist files
68+
```
69+
70+
### File Locations
71+
72+
- **Source Code**: `src/`
73+
- **Built Output**: `dist/` (inside container)
74+
- **Configuration**: Root directory (`tsconfig.json`, `rollup.config.js`, etc.)
75+
76+
## Troubleshooting
77+
78+
### Container Won't Start
79+
```bash
80+
# Rebuild the container
81+
ctrl+shift+p → "Remote: Rebuild Container"
82+
```
83+
84+
### Port Already in Use
85+
```bash
86+
# Find what's using port 5000 or 8123
87+
lsof -i :5000
88+
lsof -i :8123
89+
```
90+
91+
### Node Modules Issues
92+
```bash
93+
# Clear and reinstall dependencies
94+
rm -rf node_modules
95+
yarn install
96+
```
97+
98+
## Additional Resources
99+
100+
- [Home Assistant Custom Card Development](https://developers.home-assistant.io/docs/frontend/custom-ui/custom-card/)
101+
- [VS Code Dev Containers Docs](https://code.visualstudio.com/docs/remote/containers)
102+
- [Lit Documentation](https://lit.dev/)
103+
- [Material Design Web Components](https://github.com/material-components/material-web)
104+
105+
## Environment Details
106+
107+
- **Node.js**: 24
108+
- **TypeScript**: 5.9.3
109+
- **Build Tool**: Rollup 4.20
110+
- **Linter**: ESLint 9 + TypeScript Support
111+
- **Code Formatter**: Prettier 3.8
112+
- **Web Framework**: Lit 3.2
113+
- **Home Assistant Image**: Latest (optional)
114+
115+
## Notes
116+
117+
- The container runs as non-root user `nodejs` for security
118+
- Volume mounts use `cached` consistency mode for better performance on Mac/Windows
119+
- All Yarn commands run inside the container automatically
120+
- VS Code extensions are configured for TypeScript and YAML development
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
default_config:
2+
demo:
3+
homeassistant_alerts:
4+
enabled: false

.devcontainer/configuration.yaml

Lines changed: 0 additions & 4 deletions
This file was deleted.

.devcontainer/devcontainer.json

Lines changed: 66 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,70 @@
1-
// See https://aka.ms/vscode-remote/devcontainer.json for format details.
1+
// For format details, see https://aka.ms/devcontainer.json
2+
// Uses custom-cards/custom-card-devcontainer image for Home Assistant + Dev environment
23
{
3-
"name": "Boilerplate Card Development",
4-
"image": "ludeeus/container:monster",
5-
"context": "..",
6-
"appPort": ["5000:5000", "9123:8123"],
7-
"postCreateCommand": "npm install",
8-
"runArgs": [
9-
"-v",
10-
"${env:HOME}${env:USERPROFILE}/.ssh:/tmp/.ssh" // This is added so you can push from inside the container
4+
"name": "Home Assistant Custom Card Dev",
5+
6+
// Use published devcontainer image
7+
"image": "ghcr.io/custom-cards/custom-card-devcontainer:latest",
8+
"remoteUser": "vscode",
9+
10+
// Set working directory
11+
"workspaceFolder": "/workspaces/test",
12+
13+
// Mount project to workspace, dist to Lovelace plugins, and config directory for Home Assistant
14+
"mounts": [
15+
"source=${localWorkspaceFolder},target=/workspaces/test,type=bind",
16+
"source=${localWorkspaceFolder}/dist,target=/config/www/workspace,type=bind",
17+
"source=${localWorkspaceFolder}/.devcontainer/config,target=/config,type=bind"
1118
],
12-
"extensions": [
13-
"github.vscode-pull-request-github",
14-
"eamodio.gitlens",
15-
"dbaeumer.vscode-eslint",
16-
"esbenp.prettier-vscode",
17-
"bierner.lit-html",
18-
"runem.lit-plugin",
19-
"auchenberg.vscode-browser-preview",
20-
"davidanson.vscode-markdownlint",
21-
"redhat.vscode-yaml"
22-
],
23-
"settings": {
24-
"files.eol": "\n",
25-
"editor.tabSize": 4,
26-
"terminal.integrated.shell.linux": "/bin/bash",
27-
"editor.formatOnPaste": false,
28-
"editor.formatOnSave": true,
29-
"editor.formatOnType": true,
30-
"files.trimTrailingWhitespace": true
19+
20+
// Forward Home Assistant port
21+
"forwardPorts": [8123],
22+
"portsAttributes": {
23+
"8123": {
24+
"label": "Home Assistant",
25+
"onAutoForward": "notify"
26+
}
27+
},
28+
29+
// Container environment variables for Home Assistant setup
30+
"containerEnv": {
31+
"COREPACK_ENABLE_DOWNLOAD_PROMPT": "0",
32+
"DEVCONTAINER": "1",
33+
"HASS_USERNAME": "dev",
34+
"HASS_PASSWORD": "dev",
35+
"LOVELACE_REMOTE_FILES": "http://localhost:5000/boilerplate-card.js"
36+
},
37+
38+
// Setup Home Assistant and install dependencies
39+
"postCreateCommand": "container setup && corepack enable && yarn install",
40+
"postStartCommand": "container launch & yarn start & wait",
41+
42+
// VS Code extensions for development
43+
"customizations": {
44+
"vscode": {
45+
"extensions": [
46+
"github.vscode-github-actions",
47+
"github.vscode-pull-request-github",
48+
"bierner.lit-html",
49+
"redhat.vscode-yaml",
50+
"esbenp.prettier-vscode"
51+
],
52+
"settings": {
53+
"typescript.enablePromptUseWorkspaceTsdk": true,
54+
"[typescript]": {
55+
"editor.defaultFormatter": "esbenp.prettier-vscode",
56+
"editor.formatOnSave": true,
57+
"editor.codeActionsOnSave": {
58+
"source.fixAll.eslint": "explicit"
59+
}
60+
},
61+
"typescript.tsdk": "node_modules/typescript/lib",
62+
"typescript.tsserver.pluginPaths": [
63+
"node_modules/typescript/lib"
64+
],
65+
"files.eol": "\n",
66+
"task.allowAutomaticTasks": "on"
67+
}
68+
}
3169
}
3270
}

.devcontainer/ui-lovelace.yaml

Lines changed: 0 additions & 9 deletions
This file was deleted.

.eslintrc.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

.gitattributes

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Enforce LF line endings for all text files
2+
* text=auto eol=lf
3+
4+
# Explicitly declare common source file types
5+
*.ts text eol=lf
6+
*.js text eol=lf
7+
*.json text eol=lf
8+
*.md text eol=lf
9+
*.yml text eol=lf
10+
*.yaml text eol=lf
11+
*.css text eol=lf
12+
*.html text eol=lf
13+
14+
# Binary files — no line ending conversion
15+
*.png binary
16+
*.jpg binary
17+
*.gif binary
18+
*.ico binary
19+
*.zip binary

.github/copilot-insturctions.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# GitHub Copilot Instructions
2+
3+
You are assisting with development of this Home Assistant Lovelace custom card project.
4+
The codebase is TypeScript + Lit and builds with Rollup.
5+
6+
Use these instructions as project-specific guardrails when generating, editing, or reviewing code.
7+
8+
## Quick reference
9+
10+
### Core commands
11+
12+
```bash
13+
yarn install
14+
yarn start
15+
yarn build
16+
yarn lint
17+
```
18+
19+
### Primary files
20+
21+
- `src/boilerplate-card.ts` — main card implementation
22+
- `src/editor.ts` — visual editor (`LovelaceCardEditor`)
23+
- `src/types.ts` — card config and type definitions
24+
- `src/action-handler-directive.ts` — tap/hold/double-tap directive
25+
- `src/localize/localize.ts` — localization helper
26+
- `src/localize/languages/en.json` and `src/localize/languages/nb.json` — translation files
27+
- `rollup.config.js` and `rollup.config.dev.js` — production and dev build config
28+
29+
## Architecture and patterns
30+
31+
- The custom element is `custom:boilerplate-card`.
32+
- Prefer Lit 3 patterns and idiomatic web component structure.
33+
- Keep configuration shape centralized in `src/types.ts`.
34+
- Keep editor schema and defaults aligned with runtime card behavior.
35+
- Keep feature logic in small, readable helpers instead of long monolithic methods.
36+
37+
## TypeScript standards
38+
39+
- Use strict, explicit typing; avoid `any` unless there is no practical alternative.
40+
- Use `import type` for type-only imports where appropriate.
41+
- Validate and narrow optional config fields before use.
42+
- Keep public API names stable unless explicitly requested to change them.
43+
44+
## Lit and component guidance
45+
46+
- Use `@property` for public reactive inputs and `@state` for internal state.
47+
- Avoid direct DOM mutation when Lit reactivity can handle updates.
48+
- Preserve existing card/editor lifecycle behavior.
49+
- For card config, validate early in `setConfig` and throw actionable errors.
50+
- Keep `getCardSize` deterministic and aligned with rendered density.
51+
52+
## Home Assistant integration
53+
54+
- Use Home Assistant helpers and conventions from `custom-card-helpers`.
55+
- Ensure tap, hold, and double-tap actions are wired through existing action patterns.
56+
- Support unavailable/loading/error states gracefully.
57+
- Keep Lovelace config compatibility in mind when changing schema or defaults.
58+
59+
## Localization and copy
60+
61+
- Do not hardcode user-facing strings when a localize key should be used.
62+
- Add new translation keys to both language files currently in the repo (`en.json`, `nb.json`).
63+
- Keep copy concise, sentence case, and user-facing.
64+
- Favor consistent terminology across card UI and editor labels.
65+
66+
## Styling and UX
67+
68+
- Respect Home Assistant theme variables and CSS custom properties.
69+
- Avoid hardcoded colors when theme tokens can be used.
70+
- Keep spacing and typography consistent with existing card styles.
71+
- Ensure layouts work in both compact and wider dashboard widths.
72+
73+
## Build and quality expectations
74+
75+
- Keep `yarn lint` clean for changed code.
76+
- Ensure `yarn build` succeeds after non-trivial changes.
77+
- Do not introduce unrelated refactors in focused changes.
78+
- If updating build tooling, keep dev and prod Rollup configs consistent.
79+
80+
## Safe change workflow
81+
82+
1. Read adjacent code before editing.
83+
2. Implement the smallest viable change.
84+
3. Run relevant checks (`yarn lint`, `yarn build`, or targeted command).
85+
4. Update docs/README when behavior or config changes.
86+
5. Summarize what changed and why.
87+
88+
## Pull request guidance
89+
90+
- Keep PRs focused to one logical change.
91+
- Include screenshots or short clips for visible UI/editor changes.
92+
- Document config changes and migration notes when applicable.
93+
- Call out any follow-up work explicitly instead of bundling extra scope.
94+
95+
## Avoid these common issues
96+
97+
- Breaking editor/card config parity
98+
- Adding untyped dynamic config access
99+
- Hardcoding text instead of localization keys
100+
- Overriding theme behavior with fixed styles
101+
- Changing output filenames or card tag without explicit request

0 commit comments

Comments
 (0)