Skip to content

Fix: resolve CustomElementRegistry duplicate constructor error#568

Merged
raman325 merged 3 commits intoFutureTense:mainfrom
tykeal:issue_567
Feb 27, 2026
Merged

Fix: resolve CustomElementRegistry duplicate constructor error#568
raman325 merged 3 commits intoFutureTense:mainfrom
tykeal:issue_567

Conversation

@tykeal
Copy link
Copy Markdown
Collaborator

@tykeal tykeal commented Feb 26, 2026

Summary

CustomElementRegistry.define() requires a unique constructor per element
name. The alias registrations reused the same class constructors as the
primary registrations, causing a runtime error in browsers. Create thin
subclasses for the aliases so each define() call gets a unique constructor.

Also exclude generated JS from codespell checks since minified variable
names trigger false positives.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (which adds functionality)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

CustomElementRegistry.define() requires a unique constructor per element
name. The alias registrations reused the same class constructors as the
primary registrations, causing a runtime error in browsers. Create thin
subclasses for the aliases so each define() call gets a unique constructor.

Also exclude generated JS from codespell checks since minified variable
names trigger false positives.

Co-Authored-By: GitHub Copilot <copilot@github.com>
Signed-off-by: Andrew Grimberg <tykeal@bardicgrove.org>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a critical runtime error in the Home Assistant frontend where the CustomElementRegistry was throwing "this constructor has already been used with this registry" errors. The fix creates thin subclasses for alias registrations since customElements.define() requires a unique constructor per element name. Additionally, the PR excludes the generated/minified JavaScript from codespell checks to prevent false positives from minified variable names.

Changes:

  • Created unique subclasses (KeymasterDashboardStrategyAlias, KeymasterViewStrategyAlias, KeymasterSectionStrategyAlias) for alias element registrations
  • Added guards using customElements.get() to prevent duplicate registration errors when the script is loaded multiple times
  • Excluded custom_components/keymaster/www/generated/ from codespell checks in both pyproject.toml and .pre-commit-config.yaml

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.

File Description
lovelace_strategy/main.ts Added thin subclasses for alias registrations and guards to prevent duplicate registrations
custom_components/keymaster/www/generated/keymaster.js Regenerated minified JavaScript reflecting the TypeScript changes with new subclasses
pyproject.toml Added skip configuration for codespell to exclude generated files
.pre-commit-config.yaml Added exclude pattern for codespell pre-commit hook to skip generated files

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lovelace_strategy/main.ts Outdated
Add customElements.get() guards to the primary registrations for
consistency with the alias registrations. This prevents errors if the
script is loaded more than once.

Co-Authored-By: GitHub Copilot <copilot@github.com>
Signed-off-by: Andrew Grimberg <tykeal@bardicgrove.org>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 4 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Feb 27, 2026

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.44%. Comparing base (cdb4922) to head (5732367).
⚠️ Report is 37 commits behind head on main.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #568      +/-   ##
==========================================
+ Coverage   84.14%   84.44%   +0.29%     
==========================================
  Files          10       32      +22     
  Lines         801     2822    +2021     
  Branches        0       30      +30     
==========================================
+ Hits          674     2383    +1709     
- Misses        127      439     +312     
Flag Coverage Δ
python 84.60% <ø> (?)
typescript 79.31% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Collaborator

@raman325 raman325 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for fixing this - does tha mean tha liases were broken?

@tykeal
Copy link
Copy Markdown
Collaborator Author

tykeal commented Feb 27, 2026

Honestly, I'm not certain. I happened across the problem while working on #569 and also saw the issue after I was about to raise the PR so and then broke it apart as I thought I had introduced it at first!

I do notice that Codecov is complaining about no coverage, so to appease @firstof9 I'll get some tests added to this fix ;)

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 5 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Add tests for main.ts verifying:
- All six custom elements are registered on first load
- Registration is skipped when elements are already defined
- Alias constructors are distinct from primary constructors

Co-Authored-By: GitHub Copilot <copilot@github.com>
Signed-off-by: Andrew Grimberg <tykeal@bardicgrove.org>
@firstof9
Copy link
Copy Markdown
Collaborator

I do notice that Codecov is complaining about no coverage, so to appease @firstof9 I'll get some tests added to this fix ;)

lol when it comes to the frontend side of things, I have no idea how any of that would work, I only care about the python side myself 😝

@tykeal
Copy link
Copy Markdown
Collaborator Author

tykeal commented Feb 27, 2026

I do notice that Codecov is complaining about no coverage, so to appease @firstof9 I'll get some tests added to this fix ;)

lol when it comes to the frontend side of things, I have no idea how any of that would work, I only care about the python side myself 😝

You can still rightly complain that my first PR didn't even include tests!

@raman325 raman325 merged commit bee51ab into FutureTense:main Feb 27, 2026
8 checks passed
@tykeal tykeal deleted the issue_567 branch February 28, 2026 18:59
@firstof9 firstof9 added the bugfix Fixes a bug label Mar 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ISSUE: frontend error related to keymaster.js

5 participants