Compare commits

..
Author SHA1 Message Date
Petar PetrovandClaude Opus 5 eebc57d89a Open the battery entity from the device page header (#55734)
Co-authored-by: Claude Opus 5 <[email protected]>
2026-11-07 13:09:55 +01:00
renovate[bot] 63b474f43c Update dependency fullcalendar to v7.1.4 (#55730)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2026-11-07 10:18:04 +01:00
renovate[bot] d5cc973c22 Update dependency @versatiles/style to v8.1.1 (#55715)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2026-11-07 11:17:46 +02:00
renovate[bot] 00e76021ce Update dependency temporal-polyfill to v1.0.7 (#55731)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2026-11-07 10:08:44 +02:00
Maarten Lakerveld fb88d66421 Add My redirect for the action history page (#55705)
Lets my.home-assistant.io link to Settings > Tools > Action history
via tools_action_history.
2026-11-06 16:36:31 +01:00
renovate[bot] 23b161d257 Update dependency @rslib/core to v1.0.5 (#55710)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2026-11-06 17:28:14 +02:00
123a32dd81 Add experimental @home-assistant/components package (#55652)
* Add hac-card and hac-header as first components of the hac library

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Move hac components into a publishable workspace package built with Rslib

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Rework hac package: tree-shakable entry points, safe registration, standalone theming

- Separate component classes from registration: the root entry exports
  classes only, ./card, ./header and ./all register elements. Named
  imports from the root are now tree-shaken, and sideEffects lists only
  the registering modules (dist and src, as the repo resolves to src).
- Skip registration when a tag is already defined, so Home Assistant and
  custom cards can ship the library side by side.
- Fix hac-header empty-slot handling: :has(::slotted()) is not supported
  by browsers, so empty icon and action slots added gaps.
- Expose the header title as a heading with a configurable level.
- Add fallbacks for all theme variables so components render outside
  Home Assistant, lay out footer actions, drop overflow clipping.
- Resolve the package to its sources via a hac-source export condition
  instead of hand-written aliases, add it as a workspace dependency and
  include it in lit-analyzer.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Add hac as a workspace dependency of the frontend

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Add global, scoped and dynamic loading modes to hac components

Each component now lives in components/<name>/ with its class, a
version.ts, a CHANGELOG.md and three entry points:

- @home-assistant/hac/<name>: registers globally, as Home Assistant does
- <name>/scoped: registers the bundled copy in a scoped registry
- <name>/dynamic: uses Home Assistant's copy when its version is
  compatible, the bundled copy otherwise

ScopedElementsMixin (@home-assistant/hac/scoped) gives a Lit element its
own registry and supports both the standard API and the older API of the
scoped registry polyfill that Home Assistant loads. Without either, it
falls back to global registration.

Home Assistant publishes its components with versions and lazy loaders
at startup. The HTML creates a pending slot first, so extra modules that
load in parallel with the app wait for it instead of missing it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Support plain custom elements in scoped mode and require scoped elements

- Add createScopedElementRegistry, attachScopedShadow and
  createScopedElement, so plain custom elements can use scoped and
  dynamic components, not just Lit elements.
- ScopedElementsMixin builds on them and now takes the elements as a
  required argument: forgetting them is a type error instead of silently
  rendering unknown elements. Plain JavaScript gets a clear runtime error.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Add button group and close option for hac-header

- Add the button group with hac-button and hac-button-close, a plain
  icon button with a close icon.
- hac-header gets show-close and close-label; the close button fires
  hac-close.
- Components declare the hac elements they render as static
  dependencies. global registers them globally, so Home Assistant uses
  no scoped registries; scoped and dynamic use a variant with its own
  registry of bundled dependencies, so a component and its dependencies
  always come from the same copy.
- Exclude generated package output from the root type check.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Show a dismissable greeting card on the profile page using hac

Trial use of the hac components inside Home Assistant: a hac-card with
a hac-header that has show-close enabled, localized title and close
label. Dismissing hides it until the page is reloaded.

Using it surfaced two things:
- A card with only a header still rendered the padding of its empty
  content area, so hac-card now omits it.
- lit-analyzer only follows imports one level into node_modules, where
  the workspace package resolves, so files using hac entry points failed
  its missing-import check when linted alone, as the pre-commit hook
  does. Allow one more level.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Keep the patches comment next to PATCHES_DIR

The hac alias block was inserted between the comment and the constant it
describes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Remove the hac pages from the gallery

The library is a concept for now and should get its own documentation
later.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Give every hac element its own entry points and drop the all export

Groups now only organize elements: a group's directory holds its default
element and variants live in subdirectories, mirroring the import paths.
@home-assistant/hac/button registers hac-button only, and
@home-assistant/hac/button/close registers hac-button-close, each with
its own version, changelog, scoped and dynamic entry points.

The all export registered every bundled copy globally, which a custom
card could use to replace Home Assistant's own components, and nothing
used it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Rename the package to @home-assistant/components

The package name says what it is; hac- stays the prefix of the element
tags and classes. The export condition that resolves workspace packages
to their sources is renamed from hac-source to the neutral ha-source, so
future packages can use it too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Load bundled components in dynamic mode only when they are needed

dynamic entry points now import only the element's version up front and
load the bundled component with a dynamic import when Home Assistant
cannot provide a compatible copy, so bundlers split it into a chunk that
is not downloaded otherwise.

The static version on the component classes is removed: the host
publishes its versions in the provider list, so version.ts is the only
source of truth.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Fall back to the scoped entry point in dynamic mode

Each dynamic entry point loads its bundled element through a named
loadScoped function that imports the element's scoped entry point, so
dynamic uses exactly what scoped mode registers and createDynamicLoader
no longer builds the scoped variant itself.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Drop the registration note from the component docs

How elements are registered is the same for every component and is
documented once in the package README.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Move the slot checks of hac-card and hac-header into a shared helper

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Group the utils of the components package by purpose

registration/ holds what defines elements (global, scoped, dynamic),
element/ the helpers components use themselves.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Define each element once with its tag name next to its version

- version.ts becomes info.ts with TAG_NAME and VERSION, the cheap module
  that dynamic, global, HA's provider and dependent components read the
  tag name from. Only HTMLElementTagNameMap and Lit templates keep the
  literal, as the tooling needs it there.
- scoped and dynamic entry points export one definition object, e.g.
  hacCard = { tagName, element }, instead of a single-entry map.
  ScopedElementsMixin, createScopedElementRegistry and static
  dependencies take lists of definitions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Drop the pending provider slot from the HTML

Only extra modules can run before the app publishes its components, and
then they simply use their bundled copies. Not worth the inline script
while the library is a concept: the host now just publishes the list.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Remove the root entry point of the components package

Nothing used it and it exposed the registration internals as public API.
Every element, the scoped helpers and the provider have their own entry
points; class types come from an element's scoped entry point with
import type.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Clean up the components package API and naming

- Drop the Hac prefix from library-level names, which are not elements:
  ElementDefinition, ElementLoader, ComponentClass, ProvidedComponent(s),
  PROVIDER_KEY. HA's provider file becomes components-provider.ts.
- /scoped no longer exports scopedRegistrySupport, and the mixin no
  longer adds a static scopedRegistry; both were internals.
- scopedVariant uses the mixin's subclass instead of wrapping it again.
- Rename utils/registration/version.ts to compatibility.ts, as elements
  keep their version in info.ts, and keep ComponentClass with the other
  definition types.
- Update the outdated Rslib config comment: only global.ts registers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Clarify where to create the scoped registry

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Remove the per-element changelogs from the concept

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Remove the dynamic loading mode from the components package

The concept starts with global and scoped only. Dynamic mode, with the
provider list HA publishes, the version compatibility check and loader
support in the registry helper, can be added later.

Elements keep their VERSION in info.ts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Regenerate the lockfile on top of dev for the components package

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Use the semantic color tokens in the components

The components use --ha-color-* tokens (surface, on-surface, text,
border, primary fill, focus) instead of the legacy theme variables, with
the light values as fallback outside Home Assistant. User theme colors
still apply because HA derives the --ha-color-primary-* palette from
them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Reduce the components package to the card for the concept

Remove the button group and the header. The profile greeting is a plain
hac-card now, as it had no close button to dismiss it anymore. The
dependency mechanism stays, documented with a generic example, for the
first element that renders other elements.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Remove the dependency mechanism from the components package

Nothing uses it since the header and buttons are gone. The card now
registers itself with define, and its scoped entry point exports the
class directly. It can come back with the first element that renders
other elements.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* Use hac-card for the long-lived access tokens in the profile

The card header is a heading in the header slot, the content needs no
wrapper, and the create button sits in the footer slot.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CV5NJdqS5t7iCSfyK7Xsux

* refactor: update pnpm workspace settings and improve profile dashboard layout

- Excluded 'rs-module-lexer' from allowed builds in pnpm workspace configuration.
- Adjusted footer alignment in the long-lived access tokens card for better layout.
- Removed unnecessary 'hac-card' from the profile dashboard, simplifying the structure.
- Updated CSS to target only 'ha-card' for styling, enhancing consistency.
- Removed greeting title translation to streamline user experience.

* fix: update caching paths and improve long-lived tokens card styles

* fix: update theming fallback order and improve hac-card styles

* fix: replace `replace` with `replaceAll` for consistent wildcard handling in component source aliases

* copilot

---------

Co-authored-by: Claude <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
2026-11-06 16:04:12 +01:00
WendelinandClaude Opus 5.5 4dc713abe6 Add categories and descriptions to the create helper dialog (#55701)
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Claude <[email protected]>
2026-11-06 13:55:47 +01:00
WendelinandClaude Opus 5.5 b9182e3ea6 Fix bright card outlines on Shelly Wall Display (#55704)
* Keep rgba() colors in the legacy CSS build

Raising the legacy floor to Chrome 67 lets lightningcss rewrite rgba()
to 8-digit hex (#rrggbbaa), since Chrome 62 supports it. Kiosk webviews
like the Shelly Wall Display get the legacy build but can't parse that,
so var(--divider-color) borders on cards, toggles and buttons fell back
to currentColor and showed as bright outlines, the same symptom as
#52986. Always compile hex alpha colors to rgba() in legacy builds, for
both css`` templates and imported stylesheets. Modern builds are
unchanged.

Also drop the loader's redundant /* global */ header, which ESLint now
rejects because module and require are already built-in globals in
.cjs files.

Fixes #55699

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018Kv7eGHbhPo6Q2xNuKcJen

* Also keep rgba() colors in the legacy service worker build

The template loader runs with the legacy-sw browserslist env for the
service worker layer, which an exact match on legacy missed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018Kv7eGHbhPo6Q2xNuKcJen

---------

Co-authored-by: Claude <[email protected]>
2026-11-06 12:06:46 +01:00
Christian Wiedel 9bd57fa122 Fix grouped data tables stuttering when scrolling in Safari (#55702)
* Fix grouped data tables stuttering when scrolling in Safari

Group header actions are rendered by a callback instead of a slot per group
header. The virtualizer adds and removes header rows while scrolling, and
WebKit is slow to add and remove slots.

Fixes #55636

* Keep group header actions when the data or the pinned group changes

Group header rows are keyed by their group, so an open category menu stays open when the data refreshes. The sticky header is keyed by the pinned group, so its actions don't act on the next group's category.

* Keep group header keys apart from row keys

A group named like a row's id gave the header and the row the same virtualizer key. Prefix the keys so they can't collide.

* Don't call the group header actions slotted in the styles
2026-11-06 11:31:48 +01:00
renovate[bot] 4af7503112 Update dependency @rspack/core to v2.3.1 (#55703) 2026-11-06 10:02:58 +00:00
Jump-333 984e63dcd0 Individual devices to gas in energy dashboard (#55533)
* Add individual devices to gas in energy dashboard

* Fix copilot issues

* Fix conflicts

* Fix issues

* Fix issues

* Fix more issues

* Centralize gas and water dialogs and sankey cards

* Merge conflicts in energy-overview-view-strategy

* Merge conflicts

* Merge conflicts

* Add name and description

* Fix issues
2026-11-06 12:00:52 +02:00
Paul Bottein f90ed06080 Fix form field visibility depending on field order (#55681) 2026-11-06 10:52:25 +01:00
b176b8226b Update agents file to mention how to handle CSS on onboarding screens (#55631)
Co-authored-by: Wendelin <[email protected]>
Co-authored-by: Maarten Lakerveld <[email protected]>
Co-authored-by: bgoncal <[email protected]>
Co-authored-by: maartenla <[email protected]>
Co-authored-by: Bruno Pantaleão Gonçalves <[email protected]>
2026-11-06 09:24:58 +00:00
Zubir Jamal 3f3e9297f7 Add Malay (Bahasa Melayu) to the frontend (#55693) 2026-11-06 10:11:02 +01:00
Markus TuominenandClaude Fable 5.1 83028028e8 Add icons to the lists on the area page (#55700)
Co-authored-by: Claude Fable 5.1 <[email protected]>
2026-11-06 09:02:03 +00:00
Petar Petrov 37d7c6144c Add expand_legend option to the distribution card (#55698) 2026-11-06 08:31:22 +00:00
Petar Petrov f469f3eeae Fit narrow chart legends to three rows instead of two (#55697) 2026-11-06 08:30:51 +00:00
WendelinandClaude Opus 5.5 069e87c913 Don't show the via device as area in the devices table (#55674)
* Don't show via device as area in devices table

The Area and Floor columns fell back to the via device's area or name
when a device had no area. For protocol integrations like ZHA this put
the coordinator's name in the Area column of every unassigned end
device. Only use the device's own area or the one inherited from its
parent device. The device picker keeps the via device hint.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018Kv7eGXydVGvZF1ETnrXih

* Add 'connected via' translation and update device dashboard to display via device information

* remove comment

---------

Co-authored-by: Claude <[email protected]>
2026-11-06 08:16:18 +02:00
renovate[bot] 46bdbee8f3 Update dependency marked to v18.1.3 (#55684)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2026-11-06 06:50:46 +01:00
renovate[bot] 68def911a6 Update tsparticles to v4.5.1 (#55685)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2026-11-06 06:50:30 +01:00
renovate[bot] a8fdb99317 Update pnpm to v12.17.0 (#55695)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2026-11-06 06:50:06 +01:00
renovate[bot] 3d1bf61683 Update dependency @bundle-stats/plugin-webpack-filter to v4.23.1 (#55694)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2026-11-06 06:49:45 +01:00
Simon Lamon 5cfdff0e21 Google Assistant => Google Home (#55683) 2026-11-05 16:17:46 -05:00
Christian Wiedel 54cefeb816 Fix wrapped flow dialog titles overlapping the subtitle (#55661) 2026-11-05 21:09:50 +01:00
renovate[bot] 4458205273 Update vitest monorepo to v5.1.0 (#55680)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2026-11-05 19:28:39 +01:00
Paul Bottein 0c2de46f11 Fix truncated buttons in the voice assistant setup (#55677) 2026-11-05 18:36:57 +01:00
Aidan Timson 6bef900455 Fix the max-age of the demo and cast cache headers (#55676) 2026-11-05 18:36:36 +01:00
97 changed files with 3609 additions and 1565 deletions

No files matched your search

@@ -10,6 +10,7 @@ Use this skill when editing CSS, layout, visual hierarchy, theme integration, re
## Theme And Layout Basics
- Use Home Assistant CSS custom properties instead of hardcoded colors.
- CSS in the static HTML templates under `src/html/` must give custom properties a fallback value (for example `var(--ha-space-4, 16px)`), because the global tokens only exist once the JS-appended global stylesheet from `append-ha-style` has loaded.
- Use `--ha-space-*` spacing tokens instead of hardcoded spacing where possible.
- Keep components mobile-first and enhance for desktop.
- Keep layouts RTL-safe. Prefer logical properties when they fit.
@@ -16,6 +16,7 @@ runs:
with:
path: |
node_modules
packages/*/node_modules
~/.local/share/pnpm/store
key: >-
${{ inputs.node-modules-cache-key }}-${{ runner.os }}-${{ runner.arch }}-${{
@@ -35,6 +36,7 @@ runs:
with:
path: |
node_modules
packages/*/node_modules
~/.local/share/pnpm/store
key: >-
${{ inputs.node-modules-cache-key }}-${{ runner.os }}-${{ runner.arch }}-${{
+1
View File
@@ -28,6 +28,7 @@ runs:
with:
path: |
node_modules
packages/*/node_modules
~/.local/share/pnpm/store
key: >-
${{ inputs.node-modules-cache-key }}-${{ runner.os }}-${{ runner.arch }}-${{
@@ -1,4 +1,3 @@
/* global module, require */
// rspack/webpack loader that minifies the HTML, SVG, and CSS inside lit
// tagged template literals using `minify-literals` (html-minifier-next +
// lightningcss). Replaces the unmaintained babel-plugin-template-html-minifier.
@@ -36,6 +35,14 @@ const initLoader = (browserslistEnv) => {
return {
minifyHTMLLiterals: minifyModule.minifyHTMLLiterals,
lightningcssTargets,
// With the legacy floor at Chrome 67, lightningcss rewrites rgba()
// to 8-digit hex (#rrggbbaa). Some kiosk webviews served the legacy
// build (e.g. Shelly Wall Display) can't parse that, so any
// var(--divider-color) border falls back to currentColor. Always
// compile hex alpha colors to rgba() in legacy builds.
lightningcssInclude: browserslistEnv.startsWith("legacy")
? lightningcssModule.Features.HexAlphaColors
: 0,
};
})
);
@@ -67,12 +74,13 @@ module.exports = function minifyTemplateLiteralsLoader(source, map, meta) {
const { browserslistEnv } = this.getOptions();
initLoader(browserslistEnv)
.then(({ minifyHTMLLiterals, lightningcssTargets }) =>
.then(({ minifyHTMLLiterals, lightningcssTargets, lightningcssInclude }) =>
minifyHTMLLiterals(source, {
fileName: this.resourcePath,
html: htmlOptions,
css: {
targets: lightningcssTargets,
include: lightningcssInclude,
},
})
)
+38
View File
@@ -68,8 +68,42 @@ const CONFIG_FILES = [
path.join(__dirname, "lit-disable-dev-mode-loader.cjs"),
path.join(__dirname, "babel-plugins", "custom-polyfill-plugin.js"),
path.join(__dirname, "babel-plugins", "inline-constants-plugin.cjs"),
// Its exports define the components source aliases below
path.join(__dirname, "..", "packages", "components", "package.json"),
];
// Resolve @home-assistant/components entry points to their sources, so the package
// needs no build step during development. The entry points are read from the
// "ha-source" export condition of the package; subpath patterns are expanded
// for every element, i.e. every directory with a global.ts, like "card".
const COMPONENTS_PACKAGE_DIR = path.join(
paths.root_dir,
"packages",
"components"
);
const COMPONENTS_SRC_DIR = path.join(
COMPONENTS_PACKAGE_DIR,
"src",
"components"
);
const componentElements = fs
.readdirSync(COMPONENTS_SRC_DIR, { recursive: true })
.filter((file) => path.basename(file) === "global.ts")
.map((file) => path.dirname(file).split(path.sep).join("/"));
const componentsSourceAliases = Object.fromEntries(
Object.entries(require("../packages/components/package.json").exports)
.filter(([, target]) => target["ha-source"])
.flatMap(([subpath, target]) =>
(subpath.includes("*") ? componentElements : [undefined]).map((name) => [
`${path.posix.join("@home-assistant/components", name ? subpath.replaceAll("*", name) : subpath)}$`,
path.join(
COMPONENTS_PACKAGE_DIR,
name ? target["ha-source"].replaceAll("*", name) : target["ha-source"]
),
])
)
);
// Patches change a package's files but not its version, which is all the
// node_modules snapshot checks, so their contents go into the version too.
const PATCHES_DIR = path.join(paths.root_dir, "patches");
@@ -244,6 +278,9 @@ const createRspackConfig = ({
path: paths.root_dir,
env: "legacy",
}),
// Keep rgba() instead of #rrggbbaa, see
// minify-template-literals-loader.cjs
include: { hexAlphaColors: true },
},
},
],
@@ -428,6 +465,7 @@ const createRspackConfig = ({
"src/resources/polyfills/bigdecimal.ts"
),
}),
...componentsSourceAliases,
"lit/static-html$": "lit/static-html.js",
"lit/decorators$": "lit/decorators.js",
"lit/directive$": "lit/directive.js",
+5 -5
View File
@@ -1,5 +1,5 @@
/*
Cache-Control: public, max-age: 0, s-maxage=3600, must-revalidate
Cache-Control: public, max-age=0, s-maxage=3600, must-revalidate
Content-Security-Policy: form-action https:
Feature-Policy: vibrate 'none'; geolocation 'none'; midi 'none'; microphone 'none'; camera 'none'; magnetometer 'none'; gyroscope 'none'; speaker 'none'; vibrate 'none'; payment 'none'
Referrer-Policy: no-referrer-when-downgrade
@@ -8,13 +8,13 @@
X-XSS-Protection: 1; mode=block
/images/*
Cache-Control: public, max-age: 604800, s-maxage=604800
Cache-Control: public, max-age=604800, s-maxage=604800
/manifest.json
Cache-Control: public, max-age: 3600, s-maxage=3600
Cache-Control: public, max-age=3600, s-maxage=3600
/frontend_es5/*
Cache-Control: public, max-age: 604800, s-maxage=604800
Cache-Control: public, max-age=604800, s-maxage=604800
/frontend_latest/*
Cache-Control: public, max-age: 604800, s-maxage=604800
Cache-Control: public, max-age=604800, s-maxage=604800
+1 -1
View File
@@ -1,6 +1,6 @@
{
"background_color": "#FFFFFF",
"description": "Show Home Assistant on your Chromecast or Google Assistant devices with a screen.",
"description": "Show Home Assistant on your Chromecast or Google Home devices with a screen.",
"dir": "ltr",
"display": "standalone",
"icons": [
+2 -2
View File
@@ -5,7 +5,7 @@
<meta property="og:type" content="website" />
<meta
property="og:description"
content="Show Home Assistant on your Chromecast or Google Assistant devices with a screen."
content="Show Home Assistant on your Chromecast or Google Home devices with a screen."
/>
<meta
property="og:image"
@@ -16,7 +16,7 @@
<meta name="twitter:title" content="Home Assistant Cast" />
<meta
name="twitter:description"
content="Show Home Assistant on your Chromecast or Google Assistant devices with a screen."
content="Show Home Assistant on your Chromecast or Google Home devices with a screen."
/>
<meta
name="twitter:image"
+1 -1
View File
@@ -52,7 +52,7 @@
<p>
Home Assistant Cast allows you to show your Home Assistant data on a
Chromecast device and allows you to interact with Home Assistant on
Google Assistant devices with a screen.
Google Home devices with a screen.
</p>
</div>
+1 -1
View File
@@ -48,7 +48,7 @@ const translateErr = (err) =>
const INTRO = html`
<p>
Home Assistant Cast allows you to cast your Home Assistant installation to
Chromecast video devices and to Google Assistant devices with a screen.
Chromecast video devices and to Google Home devices with a screen.
</p>
<p>
For more information, see the
+1 -1
View File
@@ -6,7 +6,7 @@ const playerManager = castContext.getPlayerManager();
playerManager.setMessageInterceptor("LOAD", (loadRequestData) => {
const media = loadRequestData.media;
// Special handling if it came from Google Assistant
// Special handling if it came from Google Home
if (media.entity) {
media.contentId = media.entity;
media.streamType = "LIVE";
+1 -1
View File
@@ -13,7 +13,7 @@ export const castDemoLovelace: () => LovelaceConfig = () => {
{
type: "markdown",
title: "Home Assistant Cast",
content: `With Home Assistant you can easily create interfaces (just like this one) which can be shown on Chromecast devices connected to TVs or Google Assistant devices with a screen.${
content: `With Home Assistant you can easily create interfaces (just like this one) which can be shown on Chromecast devices connected to TVs or Google Home devices with a screen.${
touchSupported
? "\n\nYou are able to interact with this demo using the touch screen."
: "\n\nOn a Google Nest Hub you are able to interact with Home Assistant Cast via the touch screen."
+1 -1
View File
@@ -117,7 +117,7 @@ playerManager.setMessageInterceptor("LOAD", (loadRequestData) => {
// We received a play media command, hide Lovelace and show media player
showMediaPlayer();
const media = loadRequestData.media;
// Special handling if it came from Google Assistant
// Special handling if it came from Google Home
if (media.entity) {
media.contentId = media.entity;
media.streamType = "LIVE";
+5 -5
View File
@@ -1,21 +1,21 @@
/*
Cache-Control: public, max-age: 0, s-maxage=3600, must-revalidate
Cache-Control: public, max-age=0, s-maxage=3600, must-revalidate
Content-Security-Policy: form-action https:
Referrer-Policy: no-referrer-when-downgrade
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
/api/*
Cache-Control: public, max-age: 604800, s-maxage=604800
Cache-Control: public, max-age=604800, s-maxage=604800
/assets/*
Cache-Control: public, max-age: 604800, s-maxage=604800
Cache-Control: public, max-age=604800, s-maxage=604800
/frontend_es5/*
Cache-Control: public, max-age: 604800, s-maxage=604800
Cache-Control: public, max-age=604800, s-maxage=604800
/frontend_latest/*
Cache-Control: public, max-age: 604800, s-maxage=604800
Cache-Control: public, max-age=604800, s-maxage=604800
Access-Control-Allow-Origin: *
/embed.js
+9
View File
@@ -97,6 +97,14 @@ export const mockEnergy = (hass: MockHomeAssistant) => {
stat_consumption: "sensor.water_garden",
},
],
device_consumption_gas: [
{
stat_consumption: "sensor.gas_kitchen",
},
{
stat_consumption: "sensor.gas_heat_pump",
},
],
}));
hass.mockWS("energy/info", (): EnergyInfo => ({
cost_sensors: {},
@@ -106,6 +114,7 @@ export const mockEnergy = (hass: MockHomeAssistant) => {
energy_sources: Array.from({ length: 6 }, () => []),
device_consumption: Array.from({ length: 6 }, () => []),
device_consumption_water: Array.from({ length: 2 }, () => []),
device_consumption_gas: Array.from({ length: 2 }, () => []),
}));
hass.mockWS(
"energy/fossil_energy_consumption",
+20
View File
@@ -160,6 +160,26 @@ export const energyEntities = () =>
unit_of_measurement: "L",
},
},
"sensor.gas_kitchen": {
entity_id: "sensor.gas_kitchen",
state: "100",
attributes: {
friendly_name: "Gas kitchen",
unit_of_measurement: "m³",
device_class: "gas",
state_class: "total_increasing",
},
},
"sensor.gas_heat_pump": {
entity_id: "sensor.gas_heat_pump",
state: "500",
attributes: {
friendly_name: "Gas heat pump",
unit_of_measurement: "m³",
device_class: "gas",
state_class: "total_increasing",
},
},
"sensor.energy_car": {
entity_id: "sensor.energy_car",
state: "4",
+18
View File
@@ -158,6 +158,24 @@ const statisticsFunctions: Record<
(bucketStart, bucketEnd) =>
(20 * (bucketEnd.getTime() - bucketStart.getTime())) / 3600000
),
"sensor.gas_kitchen": (_id, start, end, period = "hour") =>
generateSumStatistics(
start,
end,
period,
100,
(bucketStart, bucketEnd) =>
(0.1 * (bucketEnd.getTime() - bucketStart.getTime())) / 3600000
),
"sensor.gas_heat_pump": (_id, start, end, period = "hour") =>
generateSumStatistics(
start,
end,
period,
500,
(bucketStart, bucketEnd) =>
(0.6 * (bucketEnd.getTime() - bucketStart.getTime())) / 3600000
),
"sensor.energy_consumption_tarif_1": (
_id: string,
start: Date,
+8
View File
@@ -301,6 +301,14 @@ export default tseslint.config(
globals: globals.node,
},
},
{
// Published as native ESM, where package subpath imports such as
// "lit/decorators.js" need their extension.
files: ["packages/*/src/**/*.ts"],
rules: {
"import-x/extensions": ["error", "ignorePackages", { ts: "never" }],
},
},
{
plugins: {
html,
+2 -2
View File
@@ -4,7 +4,7 @@ title: "Our story"
## Open source home automation that puts local control and privacy first
Home Assistant is a free and open-source software for home automation that is designed to be the central control system for smart home devices with a focus on local control and privacy. It can be accessed via a web-based user interface, via apps for Android and iOS, or using voice commands via a supported virtual assistant like Google Assistant and Amazon Alexa.
Home Assistant is a free and open-source software for home automation that is designed to be the central control system for smart home devices with a focus on local control and privacy. It can be accessed via a web-based user interface, via apps for Android and iOS, or using voice commands via a supported virtual assistant like Google Home and Amazon Alexa.
IoT devices and services are supported by modular support for controlling proprietary ecosystems if they provide public access via an Open API for third-party integrations and protocols like Bluetooth, MQTT, Zigbee, and Z-Wave, After the Home Assistant software application is installed as a computer appliance it will act as a central control system for home automation. Information from all entities it sees can be used and controlled from within scripts trigger automations using scheduling and "blueprint" subroutines, e.g. for controlling lighting, climate, entertainment systems, and appliances.
@@ -36,6 +36,6 @@ Durability for the Open Home means that devices are designed and built to keep w
The project was started as a Python application by Paulus Schoutsen in September 2013 and first published publicly on GitHub in November 2013. In July 2017, a managed operating system called Hass.io was initially introduced to make it easier use to use Home Assistant on single-board computers like the Raspberry Pi series. Its bundled "supervisor" management system allowed users to manage, backup, and update the local installation and introduced the option to extend the functionality of the software with add-ons.
An optional subscription service was introduced in December 2017 for $5/month to solve the complexities associated with secured remote access, as well as linking to Amazon Alexa and Google Assistant. Nabu Casa, Inc. was formed in September 2018 to take over the subscription service. The company's funding is based solely on revenue from the subscription service. It is used to finance the project's infrastructure and to pay for full-time employees contributing to the project.
An optional subscription service was introduced in December 2017 for $5/month to solve the complexities associated with secured remote access, as well as linking to Amazon Alexa and Google Home. Nabu Casa, Inc. was formed in September 2018 to take over the subscription service. The company's funding is based solely on revenue from the subscription service. It is used to finance the project's infrastructure and to pay for full-time employees contributing to the project.
In January 2020, branding was adjusted to make it easier to refer to different parts of the project. The main piece of software was renamed to Home Assistant Core, while the full suite of software with the embedded operating system and bundled "supervisor" management system was renamed to Home Assistant.
+1 -1
View File
@@ -121,7 +121,7 @@ const brandEntries: ConfigEntryExtended[] = (
["tuya", "Tuya", "setup_error"],
["unifi", "UniFi Network", "not_loaded"],
["met", "Meteorologisk institutt (Met.no)", "setup_error"],
["google_assistant", "Google Assistant", "not_loaded"],
["google_assistant", "Google Home", "not_loaded"],
["homekit_controller", "HomeKit Device", "setup_retry"],
] as [string, string, ConfigEntryExtended["state"]][]
).map(([domain, name, entryState]) =>
+13 -12
View File
@@ -13,7 +13,7 @@
"lint:prettier": "prettier . --cache --check",
"format:prettier": "prettier . --cache --write",
"lint:types": "node ./node_modules/@typescript/native/bin/tsc && node ./node_modules/@typescript/native/bin/tsc -p .github/scripts",
"lint:lit": "lit-analyzer \"{.,*}/src/**/*.ts\"",
"lint:lit": "lit-analyzer \"{.,*,packages/*}/src/**/*.ts\"",
"lint:element-imports": "eslint \"**/src/**/*.{js,ts,html}\" --ignore-pattern=.gitignore --rule '{\"ha/no-unused-element-import\": \"warn\"}'",
"lint:licenses": "node --no-deprecation script/check-licenses",
"lint": "pnpm run lint:eslint && pnpm run lint:prettier && pnpm run lint:types && pnpm run lint:lit",
@@ -78,9 +78,9 @@
"@mdi/svg": "7.4.47",
"@replit/codemirror-indentation-markers": "6.5.3",
"@swc/helpers": "0.5.23",
"@tsparticles/engine": "4.5.0",
"@tsparticles/preset-links": "4.5.0",
"@versatiles/style": "8.0.0",
"@tsparticles/engine": "4.5.1",
"@tsparticles/preset-links": "4.5.1",
"@versatiles/style": "8.1.1",
"@vibrant/color": "4.0.4",
"@vvo/tzdb": "6.202.0",
"@webcomponents/scoped-custom-element-registry": "0.0.10",
@@ -98,7 +98,7 @@
"echarts": "6.1.0",
"echarts-extension-chart2music": "0.1.1",
"element-internals-polyfill": "3.0.2",
"fullcalendar": "7.1.3",
"fullcalendar": "7.1.4",
"fuse.js": "7.5.0",
"hls.js": "1.7.4",
"home-assistant-js-websocket": "9.8.1",
@@ -110,7 +110,7 @@
"lit": "3.3.3",
"lit-html": "3.3.3",
"maplibre-gl": "6.16.0",
"marked": "18.1.2",
"marked": "18.1.3",
"memoize-one": "6.0.0",
"nanoid": "6.0.2",
"node-vibrant": "4.0.4",
@@ -123,7 +123,7 @@
"sortablejs": "1.15.7",
"stacktrace-js": "2.0.2",
"superstruct": "2.0.2",
"temporal-polyfill": "1.0.6",
"temporal-polyfill": "1.0.7",
"tinykeys": "4.0.1",
"weekstart": "2.0.0",
"workbox-cacheable-response": "7.4.1",
@@ -144,16 +144,17 @@
"@babel/plugin-transform-private-methods": "8.0.1",
"@babel/plugin-transform-runtime": "8.0.6",
"@babel/preset-env": "8.0.7",
"@bundle-stats/plugin-webpack-filter": "4.23.0",
"@bundle-stats/plugin-webpack-filter": "4.23.1",
"@eslint/js": "10.0.1",
"@gfx/zopfli": "1.0.15",
"@home-assistant/components": "workspace:*",
"@html-eslint/eslint-plugin": "0.67.0",
"@lokalise/node-api": "17.0.1",
"@octokit/auth-oauth-device": "8.0.5",
"@octokit/rest": "22.0.1",
"@playwright/test": "1.64.1",
"@rsdoctor/rspack-plugin": "1.6.5",
"@rspack/core": "2.3.0",
"@rspack/core": "2.3.1",
"@rspack/dev-server": "2.2.2",
"@types/babel__plugin-transform-runtime": "7.9.5",
"@types/chromecast-caf-receiver": "7.0.1",
@@ -170,7 +171,7 @@
"@types/sortablejs": "1.15.9",
"@types/tar": "7.0.87",
"@typescript/native": "npm:[email protected]",
"@vitest/coverage-v8": "5.0.5",
"@vitest/coverage-v8": "5.1.0",
"babel-loader": "10.1.1",
"babel-plugin-polyfill-corejs3": "1.0.0",
"browserslist": "4.29.5",
@@ -217,12 +218,12 @@
"typescript-eslint": "8.72.1",
"vite": "8.3.5",
"vite-tsconfig-paths": "6.1.1",
"vitest": "5.0.5",
"vitest": "5.1.0",
"webpack-stats-plugin": "1.1.3",
"webpackbar": "7.0.0",
"workbox-build": "7.4.1"
},
"packageManager": "[email protected]6.1",
"packageManager": "[email protected]7.0",
"volta": {
"node": "24.22.2"
}
+156
View File
@@ -0,0 +1,156 @@
# @home-assistant/components
Home Assistant Components (`hac-*`): framework-agnostic web components built
with [Lit](https://lit.dev).
> Work in progress, not published yet.
## Registration
Every element is imported on its own, in one of two ways:
| Import | Registers in | For |
| --------------------------------------------- | -------------------- | ----------------------------------------------------------- |
| `@home-assistant/components/<element>` | Global registry | Home Assistant itself |
| `@home-assistant/components/<element>/scoped` | Your scoped registry | Custom cards and other code that runs inside Home Assistant |
`<element>` is the tag name without the `hac-` prefix, like `card` for
`hac-card`.
Both use the copy bundled with your code. A scoped copy is isolated from the
one Home Assistant registers globally, so a custom card keeps the version it
was built with.
Global registration skips tag names that are already registered, so the first
registration wins and nothing throws.
### Global
```ts
import "@home-assistant/components/card";
```
### Scoped
Pass the component's elements to `ScopedElementsMixin` for Lit, or to
`createScopedElementRegistry` for plain custom elements. Forgetting the
elements is a TypeScript error, and a runtime error in plain JavaScript.
Lit:
```ts
import { LitElement, html } from "lit";
import { ScopedElementsMixin } from "@home-assistant/components/scoped";
import { hacCard } from "@home-assistant/components/card/scoped";
class MyCard extends ScopedElementsMixin(LitElement, [hacCard]) {
render() {
return html`<hac-card>…</hac-card>`;
}
}
```
Plain custom elements:
```js
import {
attachScopedShadow,
createScopedElement,
createScopedElementRegistry,
} from "@home-assistant/components/scoped";
import { hacCard } from "@home-assistant/components/card/scoped";
// Create the registry once, outside the class: all instances of the card share it
const registry = createScopedElementRegistry([hacCard]);
class MyCard extends HTMLElement {
constructor() {
super();
const root = attachScopedShadow(this, { mode: "open" }, registry);
root.innerHTML = "<hac-card>…</hac-card>";
// or: root.append(createScopedElement(root, "hac-card"));
}
}
```
`innerHTML`, `insertAdjacentHTML` and `createScopedElement` use the scoped
registry. `document.createElement` uses the global one, so avoid it for
scoped elements.
Scoped registries need native support (Chrome 146+, Safari 26) or
`@webcomponents/scoped-custom-element-registry`, which Home Assistant loads.
Without either, elements are registered globally instead, and an already
registered tag name keeps its existing class.
Only elements rendered inside the shadow root use the scoped registry. Light
DOM content, such as slotted elements, uses the registry of the tree it is
written in.
`lit` is a peer dependency.
### TypeScript
Each element's entry points declare its tag in `HTMLElementTagNameMap`, so
`document.querySelector("hac-card")` and templates are typed once the element
is imported. For the class itself, import its type from the `scoped` entry
point; `import type` loads nothing at runtime:
```ts
import type { HacCard } from "@home-assistant/components/card/scoped";
```
The package has no root entry point: each element and the scoped helpers
(`@home-assistant/components/scoped`) have their own.
## Theming
Each component exposes its own `--hac-*` custom properties, documented in its
source, which take precedence. Next come the theme variables of the existing
Home Assistant element it replaces, so current themes keep working: `hac-card`
reads the variables of `ha-card` (`--ha-card-background`,
`--card-background-color`, `--ha-card-border-radius`, …). Last come the
semantic color tokens (`--ha-color-*`, like `--ha-color-surface-default`) and
their light values, so the components also render outside Home Assistant.
The legacy theme variables are a temporary bridge for this experiment, not the
target design. Home Assistant always defines some of them, like
`--card-background-color`, so inside Home Assistant they currently win over
the semantic tokens. Once themes move to the semantic tokens, the legacy
variables will be removed from the fallbacks.
## Structure
Each element has its own directory, named like its import path:
```
src/components/card/ <hac-card> -> @home-assistant/components/card
hac-card.ts class, does not register itself
info.ts tag name and version
global.ts registers globally
scoped.ts element for a scoped registry -> …/card/scoped
```
Shared code lives in `src/utils/`: `registration/` for defining elements
(global and scoped) and `element/` for helpers that components use
themselves, like slot checks.
Every element has its own version in `info.ts`.
Entry points are mapped with subpath patterns in `package.json`, so a new
element directory needs no `package.json` change. Only `global.ts` files have
side effects, which `sideEffects` relies on for tree shaking.
## Development
```bash
pnpm --filter @home-assistant/components build # ESM, type declarations and custom-elements.json to dist/
pnpm --filter @home-assistant/components dev # rebuild on change
```
Inside this repository the app uses the sources directly through
the `ha-source` export condition, so no build is needed during development.
The package is built with [Rslib](https://rslib.rs) in bundleless mode: every
source file becomes its own ES module. The build then generates a
[custom elements manifest](https://custom-elements-manifest.open-wc.org) from
the JSDoc of each element, so document new slots, parts, CSS properties and
events there.
@@ -0,0 +1,84 @@
// Generates dist/custom-elements.json from the JSDoc of the hac elements, for
// editors and documentation tools. Only hac-*.ts files declare elements; the
// entry points just re-export them.
const forEachDeclaration = (customElementsManifest, callback) => {
for (const module of customElementsManifest.modules) {
for (const declaration of module.declarations ?? []) {
callback(declaration);
}
}
};
// The analyzer leaves the readme empty. Paths in the manifest are relative to
// the package root, like the module paths.
const packageReadme = () => ({
name: "package-readme",
packageLinkPhase({ customElementsManifest }) {
customElementsManifest.readme = "README.md";
},
});
// Elements are documented with @summary, which the analyzer only puts into
// `summary`. Most tools show `description`, so fall back to the summary.
const summaryAsDescription = () => ({
name: "summary-as-description",
packageLinkPhase({ customElementsManifest }) {
forEachDeclaration(customElementsManifest, (declaration) => {
if (!declaration.description && declaration.summary) {
declaration.description = declaration.summary;
}
});
},
});
// Private and protected members are internal, so keep them out of the
// documented API.
const publicApiOnly = () => ({
name: "public-api-only",
packageLinkPhase({ customElementsManifest }) {
forEachDeclaration(customElementsManifest, (declaration) => {
declaration.members = declaration.members?.filter(
(member) => !member.privacy || member.privacy === "public"
);
});
},
});
const distPaths = () => {
const toDist = (value) => value.replace(/^src\/(.+)\.ts$/, "dist/$1.js");
const rewrite = (node) => {
if (Array.isArray(node)) {
node.forEach(rewrite);
return;
}
if (!node || typeof node !== "object") {
return;
}
for (const [key, value] of Object.entries(node)) {
if ((key === "path" || key === "module") && typeof value === "string") {
node[key] = toDist(value);
} else {
rewrite(value);
}
}
};
return {
name: "dist-paths",
packageLinkPhase({ customElementsManifest }) {
rewrite(customElementsManifest.modules);
},
};
};
export default {
globs: ["src/components/**/hac-*.ts"],
outdir: "dist",
litelement: true,
plugins: [
packageReadme(),
summaryAsDescription(),
publicApiOnly(),
distPaths(),
],
};
+60
View File
@@ -0,0 +1,60 @@
{
"name": "@home-assistant/components",
"version": "0.0.0",
"description": "Home Assistant web components (hac-* elements) built with Lit",
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "git+https://github.com/home-assistant/frontend.git",
"directory": "packages/components"
},
"type": "module",
"sideEffects": [
"./dist/components/**/global.js",
"./src/components/**/global.ts"
],
"customElements": "dist/custom-elements.json",
"exports": {
"./scoped": {
"ha-source": "./src/scoped.ts",
"types": "./dist/scoped.d.ts",
"import": "./dist/scoped.js"
},
"./package.json": "./package.json",
"./*/scoped": {
"ha-source": "./src/components/*/scoped.ts",
"types": "./dist/components/*/scoped.d.ts",
"import": "./dist/components/*/scoped.js"
},
"./*/info": {
"ha-source": "./src/components/*/info.ts",
"types": "./dist/components/*/info.d.ts",
"import": "./dist/components/*/info.js"
},
"./*": {
"ha-source": "./src/components/*/global.ts",
"types": "./dist/components/*/global.d.ts",
"import": "./dist/components/*/global.js"
}
},
"files": [
"dist"
],
"publishConfig": {
"access": "public"
},
"scripts": {
"build": "rslib build && cem analyze",
"dev": "rslib build --watch",
"prepublishOnly": "pnpm run build"
},
"peerDependencies": {
"lit": "^3.3.0"
},
"devDependencies": {
"@custom-elements-manifest/analyzer": "0.11.0",
"@rslib/core": "1.0.5",
"lit": "3.3.3",
"typescript": "6.0.3"
}
}
+24
View File
@@ -0,0 +1,24 @@
import { defineConfig } from "@rslib/core";
// Bundleless build: every source file becomes its own ESM file, so consumers'
// bundlers only include the entry points they import. Only global.ts files
// register elements; everything else is free of side effects.
export default defineConfig({
source: {
entry: { index: ["./src/**/*.ts"] },
decorators: { version: "legacy" },
tsconfigPath: "./tsconfig.json",
},
tools: {
swc: { jsc: { transform: { decoratorMetadata: false } } },
},
lib: [
{
format: "esm",
bundle: false,
syntax: "es2021",
dts: true,
output: { target: "web", distPath: { root: "./dist" } },
},
],
});
@@ -0,0 +1,8 @@
import { define } from "../../utils/registration/define";
import { HacCard } from "./hac-card";
import { TAG_NAME } from "./info";
// Registers <hac-card> in the global registry, as used by Home Assistant
define(TAG_NAME, HacCard);
export { HacCard };
@@ -0,0 +1,165 @@
import { css, html, LitElement } from "lit";
import { property, state } from "lit/decorators.js";
import {
hasAssignedContent,
hasAssignedElements,
} from "../../utils/element/slot";
/**
* @element hac-card
*
* @summary
* Card that groups related content, with an optional header and footer.
*
* @slot header - Header of the card. Its spacing replaces the top padding of
* the content.
* @slot - Content of the card. Its padding is omitted when empty.
* @slot footer - Footer of the card. Hidden when empty.
*
* @csspart header - Wrapper of the header slot.
* @csspart content - Wrapper of the default slot.
* @csspart footer - Wrapper of the footer slot.
*
* @cssprop --hac-card-background - Background of the card.
* @cssprop --hac-card-color - Text color of the card.
* @cssprop --hac-card-border-color - Border color of the card and the footer divider.
* @cssprop --hac-card-border-width - Border width of the card.
* @cssprop --hac-card-border-radius - Border radius of the card.
* @cssprop --hac-card-box-shadow - Box shadow of the card.
* @cssprop --hac-card-content-padding - Padding of the content.
* @cssprop --hac-card-footer-padding - Padding of the footer.
*/
export class HacCard extends LitElement {
/**
* Visual style of the card. `outlined` draws a border without a shadow.
* `raised` hides the border and lifts the card with a shadow instead.
*/
@property({ reflect: true }) public appearance: "outlined" | "raised" =
"outlined";
@state() private _hasHeader = false;
@state() private _hasFooter = false;
@state() private _hasContent = false;
protected render() {
return html`
<div class="header" part="header" ?hidden=${!this._hasHeader}>
<slot name="header" @slotchange=${this._headerChanged}></slot>
</div>
<div
class="content ${this._hasHeader ? "below-header" : ""}"
part="content"
?hidden=${!this._hasContent}
>
<slot @slotchange=${this._contentChanged}></slot>
</div>
<div class="footer" part="footer" ?hidden=${!this._hasFooter}>
<slot name="footer" @slotchange=${this._footerChanged}></slot>
</div>
`;
}
private _headerChanged(ev: Event) {
this._hasHeader = hasAssignedElements(ev);
}
private _contentChanged(ev: Event) {
this._hasContent = hasAssignedContent(ev);
}
private _footerChanged(ev: Event) {
this._hasFooter = hasAssignedElements(ev);
}
/*
* Fallback order: --hac-card-*, then the theme variables of ha-card
* (--ha-card-*, --card-background-color, --divider-color,
* --primary-text-color) so existing themes keep working, then the semantic
* tokens and their light values for use outside Home Assistant.
* The ha-card variables are temporary, see "Theming" in the README.
*/
static styles = css`
:host {
display: block;
position: relative;
box-sizing: border-box;
background: var(
--hac-card-background,
var(
--ha-card-background,
var(--card-background-color, var(--ha-color-surface-default, #fff))
)
);
backdrop-filter: var(--ha-card-backdrop-filter, none);
color: var(
--hac-card-color,
var(--primary-text-color, var(--ha-color-on-surface-default, #141414))
);
border: var(--hac-card-border-width, var(--ha-card-border-width, 1px))
solid
var(
--hac-card-border-color,
var(
--ha-card-border-color,
var(--divider-color, var(--ha-color-border-neutral-quiet, #e6e6e6))
)
);
border-radius: var(
--hac-card-border-radius,
var(--ha-card-border-radius, var(--ha-border-radius-lg, 12px))
);
box-shadow: var(--hac-card-box-shadow, var(--ha-card-box-shadow, none));
}
:host([appearance="raised"]) {
border-color: transparent;
box-shadow: var(
--hac-card-box-shadow,
var(
--ha-card-box-shadow,
var(
--ha-box-shadow-s,
0 1px 2px 0 rgba(0, 0, 0, 0.08),
0 1px 3px 0 rgba(0, 0, 0, 0.12)
)
)
);
}
.content {
padding: var(--hac-card-content-padding, var(--ha-space-4, 16px));
}
/* The header already provides the spacing above the content */
.content.below-header {
padding-block-start: 0;
}
.footer {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: var(--ha-space-2, 8px);
padding: var(--hac-card-footer-padding, var(--ha-space-2, 8px));
border-block-start: 1px solid
var(
--hac-card-border-color,
var(--divider-color, var(--ha-color-border-neutral-quiet, #e6e6e6))
);
}
.header[hidden],
.content[hidden],
.footer[hidden] {
display: none;
}
`;
}
declare global {
interface HTMLElementTagNameMap {
"hac-card": HacCard;
}
}
@@ -0,0 +1,5 @@
/** Tag name of the element. */
export const TAG_NAME = "hac-card";
/** Version of <hac-card>. */
export const VERSION = "0.1.0";
@@ -0,0 +1,15 @@
import type { ElementDefinition } from "../../utils/registration/definition";
import { HacCard } from "./hac-card";
import { TAG_NAME } from "./info";
export { HacCard };
/**
* The bundled <hac-card> for a scoped registry:
* `ScopedElementsMixin(LitElement, [hacCard])` or
* `createScopedElementRegistry([hacCard])`.
*/
export const hacCard: ElementDefinition<typeof HacCard> = {
tagName: TAG_NAME,
element: HacCard,
};
+132
View File
@@ -0,0 +1,132 @@
import type { LitElement } from "lit";
import { define } from "./utils/registration/define";
import type { ElementDefinition } from "./utils/registration/definition";
import {
createScopedRegistry,
scopedCreationScope,
scopedRegistrySupport,
scopedShadowRootInit,
} from "./utils/registration/scoped-registry";
export type { ElementDefinition } from "./utils/registration/definition";
/**
* Creates a registry with the given elements, for `attachScopedShadow`:
* definitions from `scoped` entry points, e.g. `[hacCard]`.
* Create it once, outside the class, so all instances of a card share it.
*
* Without scoped registry support (native or polyfill), the elements are
* registered in the global registry instead, unless their tag name is already
* taken there, and the global registry is returned.
*/
export const createScopedElementRegistry = (
elements: readonly ElementDefinition[]
): CustomElementRegistry => {
// Checked as unknown: Array.isArray would narrow the list to any[]
if (!Array.isArray(elements as unknown)) {
throw new TypeError(
"@home-assistant/components: pass the elements to register, e.g. [hacCard]"
);
}
const registry = createScopedRegistry() ?? customElements;
for (const { tagName, element } of elements) {
define(tagName, element, registry);
}
return registry;
};
const shadowRootInit = (
init: ShadowRootInit,
registry: CustomElementRegistry
): ShadowRootInit =>
registry === customElements ? init : scopedShadowRootInit(init, registry);
const registries = new WeakMap<ShadowRoot, CustomElementRegistry>();
/**
* Attaches a shadow root that uses `registry` for custom elements in it.
*
* Content added with `innerHTML` or `insertAdjacentHTML` uses the registry.
* Create single elements with `createScopedElement`; `document.createElement`
* would use the global registry.
*
* Light DOM content, like slotted elements, keeps using the registry of the
* tree it is written in.
*/
export const attachScopedShadow = (
host: HTMLElement,
init: ShadowRootInit,
registry: CustomElementRegistry
): ShadowRoot => {
const root = host.attachShadow(shadowRootInit(init, registry));
registries.set(root, registry);
return root;
};
/** Creates an element from the registry of a shadow root. */
export const createScopedElement = <K extends keyof HTMLElementTagNameMap>(
root: ShadowRoot,
tagName: K
): HTMLElementTagNameMap[K] => {
const registry = registries.get(root);
if (!registry || registry === customElements) {
return document.createElement(tagName);
}
if (scopedRegistrySupport() === "polyfill") {
// The polyfill adds createElement to shadow roots
return (
root as unknown as {
createElement(tagName: K): HTMLElementTagNameMap[K];
}
).createElement(tagName);
}
return (
document.createElement as (
tagName: K,
options: { customElementRegistry: CustomElementRegistry }
) => HTMLElementTagNameMap[K]
)(tagName, { customElementRegistry: registry });
};
type LitElementConstructor = abstract new (...args: any[]) => LitElement;
/**
* Renders a Lit element into a shadow root with its own custom element
* registry, containing `elements`:
*
* ```ts
* class MyCard extends ScopedElementsMixin(LitElement, [hacCard]) {}
* ```
*
* See `createScopedElementRegistry` and `attachScopedShadow` for the details.
*/
export const ScopedElementsMixin = <T extends LitElementConstructor>(
superClass: T,
elements: readonly ElementDefinition[]
) => {
// Created on first use, so defining the class has no side effects
let registry: CustomElementRegistry | undefined;
const getRegistry = () => {
registry ??= createScopedElementRegistry(elements);
return registry;
};
abstract class ScopedElementsHost extends superClass {
public attachShadow(init: ShadowRootInit): ShadowRoot {
const root = super.attachShadow(shadowRootInit(init, getRegistry()));
registries.set(root, getRegistry());
return root;
}
protected createRenderRoot() {
const root = super.createRenderRoot();
const scoped = getRegistry();
if (scoped !== customElements && root instanceof ShadowRoot) {
this.renderOptions.creationScope = scopedCreationScope(root, scoped);
}
return root;
}
}
// Typed as the base class: the overrides add no public API
return ScopedElementsHost as unknown as T;
};
@@ -0,0 +1,15 @@
/** Whether the slot of a `slotchange` event has elements assigned. */
export const hasAssignedElements = (ev: Event): boolean =>
(ev.target as HTMLSlotElement).assignedElements().length > 0;
/**
* Whether the slot of a `slotchange` event has content assigned: elements or
* text. Whitespace between tags is not content.
*/
export const hasAssignedContent = (ev: Event): boolean =>
(ev.target as HTMLSlotElement)
.assignedNodes()
.some(
(node) =>
node.nodeType === Node.ELEMENT_NODE || Boolean(node.textContent?.trim())
);
@@ -0,0 +1,17 @@
/**
* Registers a custom element unless the tag name is already taken in the
* registry.
*
* Home Assistant and custom cards can each ship a copy of this library, so
* registering the same tag name twice must not throw. The first registration
* wins.
*/
export const define = (
tagName: string,
elementClass: CustomElementConstructor,
registry: CustomElementRegistry = customElements
): void => {
if (!registry.get(tagName)) {
registry.define(tagName, elementClass);
}
};
@@ -0,0 +1,10 @@
/**
* An element to register: its tag name and its class. The `scoped` entry
* point of an element exports one, e.g. `hacCard`.
*/
export interface ElementDefinition<
E extends CustomElementConstructor = CustomElementConstructor,
> {
readonly tagName: string;
readonly element: E;
}
@@ -0,0 +1,67 @@
/**
* Scoped custom element registries come in two flavors: the standard API
* (Chrome 146+, Safari 26) and the older API of
* `@webcomponents/scoped-custom-element-registry`, which Home Assistant loads
* and which replaces the native implementation when present.
*/
export type ScopedRegistrySupport = "polyfill" | "native" | "none";
let support: ScopedRegistrySupport | undefined;
export const scopedRegistrySupport = (): ScopedRegistrySupport => {
if (support) {
return support;
}
if ("CustomElementRegistryPolyfill" in globalThis) {
support = "polyfill";
} else {
try {
new CustomElementRegistry();
support = "native";
} catch (_err) {
support = "none";
}
}
return support;
};
/** A new scoped registry, or `undefined` if the browser has no support. */
export const createScopedRegistry = (): CustomElementRegistry | undefined =>
scopedRegistrySupport() === "none" ? undefined : new CustomElementRegistry();
/** Shadow root options that make the shadow root use `registry`. */
export const scopedShadowRootInit = (
init: ShadowRootInit,
registry: CustomElementRegistry
): ShadowRootInit =>
scopedRegistrySupport() === "polyfill"
? ({ ...init, registry } as ShadowRootInit)
: ({ ...init, customElementRegistry: registry } as ShadowRootInit);
interface CreationScope {
importNode(node: Node, deep?: boolean): Node;
}
/**
* The object Lit should create template content with, so custom elements in
* it are created from the scoped registry (Lit's `creationScope`).
*/
export const scopedCreationScope = (
root: ShadowRoot,
registry: CustomElementRegistry
): CreationScope => {
if (scopedRegistrySupport() === "polyfill") {
// The polyfill adds importNode to shadow roots
return root as unknown as CreationScope;
}
// Standard API: the registry is passed to importNode
return {
importNode: (node, deep) =>
(
document.importNode as (
node: Node,
options: { selfOnly: boolean; customElementRegistry: unknown }
) => Node
)(node, { selfOnly: !deep, customElementRegistry: registry }),
};
};
+20
View File
@@ -0,0 +1,20 @@
import { describe, expect, it } from "vitest";
import { define } from "../src/utils/registration/define";
// Home Assistant and custom cards can each ship a copy of the library, so a
// second registration of the same tag must be ignored instead of throwing.
describe("define", () => {
it("registers an element that is not registered yet", () => {
class First extends HTMLElement {}
define("hac-test-first", First);
expect(customElements.get("hac-test-first")).toBe(First);
});
it("keeps the first registration of a tag name", () => {
class Original extends HTMLElement {}
class Duplicate extends HTMLElement {}
define("hac-test-duplicate", Original);
expect(() => define("hac-test-duplicate", Duplicate)).not.toThrow();
expect(customElements.get("hac-test-duplicate")).toBe(Original);
});
});
+40
View File
@@ -0,0 +1,40 @@
import { describe, expect, it } from "vitest";
import {
hasAssignedContent,
hasAssignedElements,
} from "../src/utils/element/slot";
// Components hide wrappers of empty slots, e.g. the padding of an empty card.
// Whitespace between tags must not count as content.
const slotEvent = (light: string, name?: string) => {
const host = document.createElement("div");
host.innerHTML = light;
const slot = document.createElement("slot");
if (name) {
slot.name = name;
}
host.attachShadow({ mode: "open" }).append(slot);
return { target: slot } as unknown as Event;
};
describe("hasAssignedContent", () => {
it("ignores whitespace", () => {
expect(hasAssignedContent(slotEvent("\n \n"))).toBe(false);
});
it("counts text", () => {
expect(hasAssignedContent(slotEvent(" Hello "))).toBe(true);
});
it("counts elements", () => {
expect(hasAssignedContent(slotEvent("\n <p></p>\n"))).toBe(true);
});
});
describe("hasAssignedElements", () => {
it("only counts elements assigned to the slot", () => {
const light = '<span slot="icon"></span> text';
expect(hasAssignedElements(slotEvent(light, "icon"))).toBe(true);
expect(hasAssignedElements(slotEvent(light, "actions"))).toBe(false);
});
});
+20
View File
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "ES2021",
"lib": ["ES2021", "DOM", "DOM.Iterable"],
"module": "ESNext",
"moduleResolution": "bundler",
"experimentalDecorators": true,
"useDefineForClassFields": false,
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"isolatedModules": true,
"skipLibCheck": true,
"declaration": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
}
+1010 -351
View File
File diff suppressed because it is too large. Load diff
+5
View File
@@ -1,3 +1,7 @@
packages:
- "."
- "packages/*"
savePrefix: ""
autoInstallPeers: false
@@ -19,6 +23,7 @@ trustPolicyExclude:
allowBuilds:
"@tsparticles/engine": false
core-js: false
rs-module-lexer: false
unrs-resolver: false
overrides:
@@ -3,6 +3,12 @@ import { ref } from "lit/directives/ref";
const MAX_ROWS = 2;
// A narrow list, as on a phone or a half width card, fits about one item per
// row, so two rows hide most of the legend. 450px is about the widest list a
// phone viewport gives.
const NARROW_MAX_ROWS = 3;
const NARROW_WIDTH = 450;
// How many items fit in maxRows of a wrapping row, leaving room for the
// "more" item after them when not all of them fit.
export const fitInRows = (
@@ -34,9 +40,10 @@ export const fitInRows = (
return widths.length;
};
// Fits the items of a wrapping legend list into two rows. The list keeps its
// "more" item rendered with the legend-more class, and items past the fit
// stay rendered out of the flow, so both can always be measured.
// Fits the items of a wrapping legend list into two rows, three when it is
// narrow. The list keeps its "more" item rendered with the legend-more class,
// and items past the fit stay rendered out of the flow, so both can always be
// measured.
export class LegendFitController {
// Items to show while collapsed, undefined until the list is measured
public fit?: number;
@@ -118,7 +125,7 @@ export class LegendFitController {
more?.getBoundingClientRect().width ?? 0,
rowWidth,
parseFloat(getComputedStyle(list).columnGap) || 0,
MAX_ROWS
rowWidth < NARROW_WIDTH ? NARROW_MAX_ROWS : MAX_ROWS
);
if (fit !== this.fit) {
this.fit = fit;
+40 -22
View File
@@ -17,6 +17,7 @@ import {
import { classMap } from "lit/directives/class-map";
import { ifDefined } from "lit/directives/if-defined";
import { join } from "lit/directives/join";
import { keyed } from "lit/directives/keyed";
import { styleMap } from "lit/directives/style-map";
import memoizeOne from "memoize-one";
import { consume } from "../../common/decorators/consume";
@@ -124,16 +125,16 @@ export interface DataTableRowData {
export type SortableColumnContainer = Record<string, ClonedDataTableColumnData>;
// Slot for the actions at the end of a group's header, they don't toggle the
// group. Put `ha-icon-button`s with a label in it. They get the rows' icon
// Renders the actions at the end of a group's header, they don't toggle the
// group. Return `ha-icon-button`s with a label. They get the rows' icon
// colour and size by default, which `--data-table-group-actions-color`,
// `--data-table-group-actions-button-size` and
// `--data-table-group-actions-icon-size` change for all of them; style a
// button itself for its own colour or size.
export const GROUP_ACTIONS_SLOT_PREFIX = "group-actions-";
export const groupActionsSlot = (group: string) =>
`${GROUP_ACTIONS_SLOT_PREFIX}${group}`;
// button itself for its own colour or size. Not a slot per group, as WebKit
// is slow to add and remove slots while the rows scroll.
export type DataTableGroupActions = (
group: string
) => TemplateResult | typeof nothing;
const UNDEFINED_GROUP_KEY = "zzzzz_undefined";
@@ -197,6 +198,9 @@ export class HaDataTable extends LitElement {
@property({ attribute: false }) public groupOrder?: string[];
@property({ attribute: false })
public renderGroupActions?: DataTableGroupActions;
@property({ attribute: false }) public sortColumn?: string;
@property({ attribute: false }) public sortDirection: SortingDirection = null;
@@ -506,7 +510,8 @@ export class HaDataTable extends LitElement {
this.groupOrder,
this._collapsedGroups,
this.sortColumn,
this.sortDirection
this.sortDirection,
this.renderGroupActions
)
: [];
@@ -759,8 +764,6 @@ export class HaDataTable extends LitElement {
`
: html`
${
// Rendered before the rows, so the group's actions slot
// shows here and not in its scrolled out header row
this.groupColumn &&
!this.autoHeight &&
this._stickyGroup !== undefined
@@ -775,11 +778,15 @@ export class HaDataTable extends LitElement {
.group=${this._stickyGroup}
@click=${this._collapseStickyGroup}
>
${this._renderGroupHeader(
${keyed(
this._stickyGroup,
this._collapsedGroups.includes(this._stickyGroup),
this._i18n?.localize,
true
this._renderGroupHeader(
this._stickyGroup,
this._collapsedGroups.includes(this._stickyGroup),
this._i18n?.localize,
this.renderGroupActions,
true
)
)}
</div>`
: nothing
@@ -803,7 +810,14 @@ export class HaDataTable extends LitElement {
`;
}
private _keyFunction = (row: DataTableRowData) => row?.[this.id] || row;
// Group headers are keyed by their group, so they keep their actions' state
// when the data changes. The prefixes keep them apart from row ids.
private _keyFunction = (row: DataTableRowData) =>
row?.[this.id]
? `row-${row[this.id]}`
: row?.groupHeader !== undefined
? `group-${row.groupHeader}`
: row;
private _renderMainValue(value: unknown) {
return this.clickable
@@ -1063,7 +1077,8 @@ export class HaDataTable extends LitElement {
groupOrder: string[] | undefined,
collapsedGroups: string[],
sortColumn: string | undefined,
sortDirection: SortingDirection
sortDirection: SortingDirection,
renderGroupActions: DataTableGroupActions | undefined
) => {
if (appendRow || groupColumn) {
let items = [...data];
@@ -1122,7 +1137,8 @@ export class HaDataTable extends LitElement {
content: this._renderGroupHeader(
groupName,
collapsedGroups.includes(groupName),
localize
localize,
renderGroupActions
),
});
if (!collapsedGroups.includes(groupName)) {
@@ -1148,6 +1164,7 @@ export class HaDataTable extends LitElement {
groupName: string,
collapsed: boolean,
localize: LocalizeFunc | undefined,
renderGroupActions: DataTableGroupActions | undefined,
sticky = false
) {
// The sticky copy's button and name are hidden from assistive technology,
@@ -1183,7 +1200,7 @@ export class HaDataTable extends LitElement {
}
</span>
<div class="group-actions" @click=${this._stopGroupToggle}>
<slot name=${groupActionsSlot(groupName)}></slot>
${renderGroupActions?.(groupName)}
</div>
</div>`;
}
@@ -1264,7 +1281,8 @@ export class HaDataTable extends LitElement {
this.groupOrder,
this._collapsedGroups,
this.sortColumn,
this.sortDirection
this.sortDirection,
this.renderGroupActions
);
if (
@@ -1961,7 +1979,7 @@ export class HaDataTable extends LitElement {
align-self: stretch;
margin-top: -12px;
margin-inline-start: auto;
/* The rows' icon button size, inherited by the slotted ones */
/* The rows' icon button size, inherited by the actions */
--_button-size: var(--data-table-group-actions-button-size, 48px);
--ha-icon-button-size: var(--_button-size);
--mdc-icon-size: var(--data-table-group-actions-icon-size, 24px);
@@ -1974,9 +1992,9 @@ export class HaDataTable extends LitElement {
position: sticky;
inset-inline-end: 0;
cursor: default;
/* Don't pass the group name's weight on to slotted menus */
/* Don't pass the group name's weight on to the actions */
font-weight: var(--ha-font-weight-normal);
/* Inherited by the slotted icon buttons, like the rows' icon cells */
/* Inherited by the actions, like the rows' icon cells */
color: var(
--data-table-group-actions-color,
var(--secondary-text-color)
+24 -11
View File
@@ -1,3 +1,4 @@
import { ensureArray } from "../../common/array/ensure-array";
import type {
HaFormCondition,
HaFormDataContainer,
@@ -87,26 +88,38 @@ export const combineVisible = (
return conditions;
};
const getConditionFields = (visible: HaFormVisible | undefined): string[] =>
visible === undefined || typeof visible === "boolean"
? []
: ensureArray(visible).flatMap((condition) =>
"condition" in condition
? condition.conditions.flatMap((sub) => getConditionFields(sub))
: [condition.field]
);
// Hiding a field drops its value, which can flip another field's condition, so
// resolve the set to a fixpoint. Fields are given in order as name and visible.
// a field is evaluated after the fields its condition depends on.
export const getHiddenFields = (
fields: readonly (readonly [string, HaFormVisible | undefined])[],
data: HaFormDataContainer | undefined
): Set<string> => {
const hidden = new Set<string>();
const conditions = new Map(fields);
const evalData: HaFormDataContainer = { ...(data ?? {}) };
let changed = true;
while (changed) {
changed = false;
for (const [name, visible] of fields) {
if (hidden.has(name) || isVisible(visible, evalData)) {
continue;
}
const hidden = new Set<string>();
const resolved = new Set<string>();
const resolve = (name: string) => {
if (resolved.has(name) || !conditions.has(name)) {
return;
}
resolved.add(name);
const visible = conditions.get(name);
getConditionFields(visible).forEach(resolve);
if (!isVisible(visible, evalData)) {
hidden.add(name);
delete evalData[name];
changed = true;
}
}
};
fields.forEach(([name]) => resolve(name));
return hidden;
};
+7 -2
View File
@@ -219,6 +219,7 @@ export class HaPickerComboBox extends ScrollableFadeMixin(LitElement) {
if (this._searchFieldElement) {
this._searchFieldElement.value = value;
}
this._applySearch(value);
}
protected get scrollableElement(): HTMLElement | null {
@@ -691,7 +692,11 @@ export class HaPickerComboBox extends ScrollableFadeMixin(LitElement) {
private _filterChanged = (ev: InputEvent) => {
const textfield = ev.target as HaInputSearch;
const searchString = (textfield.value ?? "").trim();
this._applySearch(textfield.value ?? "");
};
private _applySearch(value: string) {
const searchString = value.trim();
this._search = searchString;
if (this.sections?.length) {
@@ -736,7 +741,7 @@ export class HaPickerComboBox extends ScrollableFadeMixin(LitElement) {
this._resetSelectedItem();
this._resetListScroll();
};
}
// Keep focus in the search field, so a focus change doesn't scroll the
// list to the active row and make the click miss.
+18 -116
View File
@@ -5,7 +5,6 @@ import { css, html, LitElement, nothing } from "lit";
import { customElement, property, query, state } from "lit/decorators";
import memoizeOne from "memoize-one";
import { ensureArray } from "../common/array/ensure-array";
import { deepEqual } from "../common/util/deep-equal";
import {
fireEvent,
type HASSDomCurrentTargetEvent,
@@ -30,16 +29,10 @@ import {
type Selector,
type TargetSelector,
} from "../data/selector";
import {
getServiceFieldDefaults,
getServiceFieldVisibility,
getServiceHiddenFields,
} from "../data/service";
import type { HassServiceTargetWithExclude } from "../data/target";
import type { HomeAssistant, ValueChangedEvent } from "../types";
import { documentationUrl } from "../util/documentation-url";
import { getSelectorFallbackValue } from "./ha-form/get-selector-fallback-value";
import type { HaFormDataContainer, HaFormVisible } from "./ha-form/types";
import "./ha-checkbox";
import type { HaCheckbox } from "./ha-checkbox";
import "./ha-icon-button";
@@ -87,26 +80,20 @@ const isFullWidthSelector = (selector?: Selector): boolean =>
const isBooleanSelector = (selector?: Selector): boolean =>
!!selector && "boolean" in selector;
type ServiceField = Omit<HassService["fields"][string], "selector"> & {
visible?: HaFormVisible;
};
interface Field extends ServiceField {
interface Field extends Omit<HassService["fields"][string], "selector"> {
key: string;
selector?: Selector;
}
interface ExtHassService extends Omit<HassService, "fields"> {
fields: (ServiceField & {
fields: (Omit<HassService["fields"][string], "selector"> & {
key: string;
selector?: Selector;
fields?: Record<string, ServiceField>;
fields?: Record<string, Omit<HassService["fields"][string], "selector">>;
collapsed?: boolean;
})[];
flatFields: Field[];
hasSelector: string[];
fieldVisibility: [string, HaFormVisible | undefined][];
fieldDefaults: HaFormDataContainer;
}
@customElement("ha-service-control")
@@ -223,11 +210,7 @@ export class HaServiceControl extends LitElement {
);
if (filled.changed) {
updatedDefaultValue = true;
this._value.data = this._withoutHiddenDefaults(
serviceData,
filled.values,
this._value.data
);
this._value.data = filled.values;
}
}
if (updatedDefaultValue) {
@@ -295,87 +278,10 @@ export class HaServiceControl extends LitElement {
fields,
flatFields,
hasSelector,
fieldVisibility: getServiceFieldVisibility(
serviceDomains[domain][serviceName].fields
),
fieldDefaults: getServiceFieldDefaults(
serviceDomains[domain][serviceName].fields
),
};
}
);
private _hiddenFields = memoizeOne(
(
serviceData: ExtHassService | undefined,
data: Record<string, any> | undefined
): Set<string> =>
serviceData
? getServiceHiddenFields(
serviceData.fieldVisibility,
serviceData.fieldDefaults,
data
)
: new Set()
);
// Defaults are not added to hidden fields, existing values are kept so
// opening an action does not change it
private _withoutHiddenDefaults(
serviceData: ExtHassService,
filled: Record<string, any>,
original: Record<string, any>
) {
const data = { ...filled };
this._hiddenFields(serviceData, filled).forEach((key) => {
if (!(key in original)) {
delete data[key];
}
});
return data;
}
// Hidden fields lose their value, and required fields that show again get
// their default back
private _applyFieldVisibility(data: Record<string, any>) {
const serviceData = this._getServiceInfo(
this._value?.action,
this.hass.services
);
if (!serviceData?.fieldVisibility.length) {
return data;
}
let previous = this._value?.data;
let current = data;
// A restored default can hide or show other fields, settle until nothing
// changes. The bound guards against conditions that keep flipping.
for (let i = 0; i <= serviceData.flatFields.length; i++) {
const hidden = this._hiddenFields(serviceData, current);
const wasHidden = this._hiddenFields(serviceData, previous);
const visibleData = { ...current };
hidden.forEach((key) => {
delete visibleData[key];
delete this._stickySelector[key];
this._checkedKeys.delete(key);
});
const shownAgain = serviceData.flatFields.filter(
(field) => wasHidden.has(field.key) && !hidden.has(field.key)
);
const next = fillFieldDefaults(
visibleData,
shownAgain.map((field) => [field.key, field]),
true,
(field) => !field.required
).values;
if (deepEqual(next, current)) {
return next;
}
previous = current;
current = next;
}
return current;
}
private _getTargetedEntities = memoizeOne((target, value) => {
const targetSelector = target ? { target } : { target: {} };
if (
@@ -564,8 +470,6 @@ export class HaServiceControl extends LitElement {
this._value
);
const hiddenFields = this._hiddenFields(serviceData, this._value?.data);
const domain = this._value?.action
? computeDomain(this._value.action)
: undefined;
@@ -673,20 +577,18 @@ export class HaServiceControl extends LitElement {
></ha-yaml-editor>`
: serviceData?.fields.map((dataField) => {
if (!dataField.fields) {
return hiddenFields.has(dataField.key)
? nothing
: this._renderField(
dataField,
hasOptional,
domain,
serviceName,
targetEntities
);
return this._renderField(
dataField,
hasOptional,
domain,
serviceName,
targetEntities
);
}
const fields = Object.entries(dataField.fields)
.map(([key, field]) => ({ key, ...field }))
.filter((field) => !hiddenFields.has(field.key));
const fields = Object.entries(dataField.fields).map(
([key, field]) => ({ key, ...field })
);
return fields.length &&
this._hasFilteredFields(fields, targetEntities)
@@ -712,9 +614,9 @@ export class HaServiceControl extends LitElement {
.service=${this._value!.action}
.section=${dataField.key}
></ha-service-section-icon>
${fields.map((field) =>
${Object.entries(dataField.fields).map(([key, field]) =>
this._renderField(
field,
{ key, ...field },
hasOptional,
domain,
serviceName,
@@ -924,7 +826,7 @@ export class HaServiceControl extends LitElement {
fireEvent(this, "value-changed", {
value: {
...this._value,
data: this._applyFieldVisibility(data),
data,
},
});
}
@@ -1053,7 +955,7 @@ export class HaServiceControl extends LitElement {
fireEvent(this, "value-changed", {
value: {
...this._value,
data: this._applyFieldVisibility(data),
data,
},
});
}
+102 -18
View File
@@ -252,12 +252,14 @@ export interface EnergyPreferences {
energy_sources: EnergySource[];
device_consumption: DeviceConsumptionEnergyPreference[];
device_consumption_water: DeviceConsumptionEnergyPreference[];
device_consumption_gas: DeviceConsumptionEnergyPreference[];
}
export const EMPTY_PREFERENCES: EnergyPreferences = {
energy_sources: [],
device_consumption: [],
device_consumption_water: [],
device_consumption_gas: [],
};
export interface EnergyInfo {
@@ -275,6 +277,7 @@ export interface EnergyPreferencesValidation {
energy_sources: EnergyValidationIssue[][];
device_consumption: EnergyValidationIssue[][];
device_consumption_water: EnergyValidationIssue[][];
device_consumption_gas: EnergyValidationIssue[][];
}
export const getEnergyInfo = (callWS: HomeAssistant["callWS"]) =>
@@ -507,6 +510,11 @@ export const getReferencedStatisticIds = (
...prefs.device_consumption_water.map((d) => d.stat_consumption)
);
}
if (!(includeTypes && !includeTypes.includes("gas"))) {
statIDs.push(
...prefs.device_consumption_gas.map((d) => d.stat_consumption)
);
}
return statIDs;
};
@@ -543,6 +551,7 @@ export const getReferencedStatisticIdsPower = (
}
statIDs.push(...prefs.device_consumption.map((d) => d.stat_rate));
statIDs.push(...prefs.device_consumption_water.map((d) => d.stat_rate));
statIDs.push(...prefs.device_consumption_gas.map((d) => d.stat_rate));
return statIDs.filter(Boolean) as string[];
};
@@ -651,9 +660,16 @@ const getEnergyData = async (
const gasIsOtherEnergy =
ENERGY_UNITS.includes(gasUnit as any) && gasUnit !== "kWh";
const gasEnergyStatIds = gasIsOtherEnergy
? (energySourcesByType(prefs).gas ?? []).map(
(source) => source.stat_energy_from
)
? [
...new Set([
...(energySourcesByType(prefs).gas ?? []).map(
(source) => source.stat_energy_from
),
...prefs.device_consumption_gas.map(
(device) => device.stat_consumption
),
]),
]
: [];
const gasEnergyUnits: StatisticsUnitConfiguration = {
energy: gasIsOtherEnergy
@@ -1474,13 +1490,18 @@ export type EnergyGasUnitClass = (typeof energyGasUnitClass)[number];
export const getEnergyGasUnitClass = (
prefs: EnergyPreferences,
excludeSource?: string,
statisticsMetaData: Record<string, StatisticsMetaData> = {}
statisticsMetaData: Record<string, StatisticsMetaData> = {},
excludeFrom?: "source" | "device"
): EnergyGasUnitClass | undefined => {
for (const source of prefs.energy_sources) {
if (source.type !== "gas") {
continue;
}
if (excludeSource && excludeSource === source.stat_energy_from) {
if (
excludeSource &&
excludeSource === source.stat_energy_from &&
excludeFrom !== "device"
) {
continue;
}
const statisticIdWithMeta = statisticsMetaData[source.stat_energy_from];
@@ -1492,6 +1513,19 @@ export const getEnergyGasUnitClass = (
return statisticIdWithMeta.unit_class as EnergyGasUnitClass;
}
}
for (const device of prefs.device_consumption_gas) {
if (
excludeSource &&
excludeSource === device.stat_consumption &&
excludeFrom !== "source"
) {
continue;
}
const unitClass = statisticsMetaData[device.stat_consumption]?.unit_class;
if (energyGasUnitClass.includes(unitClass as EnergyGasUnitClass)) {
return unitClass as EnergyGasUnitClass;
}
}
return undefined;
};
@@ -1501,15 +1535,15 @@ const getEnergyGasUnit = (
prefs: EnergyPreferences,
statisticsMetaData: Record<string, StatisticsMetaData> = {}
): string => {
const units = prefs.energy_sources
.filter((s) => s.type === "gas")
.map((s) =>
getDisplayUnit(
states,
s.stat_energy_from,
statisticsMetaData[s.stat_energy_from]
)
);
const gasStatIds = [
...prefs.energy_sources.flatMap((s) =>
s.type === "gas" ? [s.stat_energy_from] : []
),
...prefs.device_consumption_gas.map((d) => d.stat_consumption),
];
const units = gasStatIds.map((id) =>
getDisplayUnit(states, id, statisticsMetaData[id])
);
const unitClass = getEnergyGasUnitClass(prefs, undefined, statisticsMetaData);
if (unitClass === "energy") {
@@ -1535,10 +1569,7 @@ const getEnergyGasUnit = (
return [];
}),
]);
const isSharedWithKWh = prefs.energy_sources.some(
(source) =>
source.type === "gas" && kWhStatIds.has(source.stat_energy_from)
);
const isSharedWithKWh = gasStatIds.some((id) => kWhStatIds.has(id));
const first = units[0];
if (
!isSharedWithKWh &&
@@ -2200,6 +2231,48 @@ export const formatFlowRateShort = (
});
};
export type WaterGasResource = "water" | "gas";
export const getWaterGasDevicesKey = (resource: WaterGasResource) =>
`device_consumption_${resource}` as const;
export const getWaterGasDevices = (
prefs: EnergyPreferences,
resource: WaterGasResource
): DeviceConsumptionEnergyPreference[] =>
prefs[getWaterGasDevicesKey(resource)];
export const getFlowRateUnit = (
resource: WaterGasResource,
prefs: EnergyPreferences,
states: HomeAssistant["states"]
): string | undefined => {
const ids = [
...prefs.energy_sources.flatMap((s) =>
s.type === resource && s.stat_rate ? [s.stat_rate] : []
),
...getWaterGasDevices(prefs, resource).flatMap((d) =>
d.stat_rate ? [d.stat_rate] : []
),
];
for (const id of ids) {
const unit = states[id]?.attributes.unit_of_measurement;
if (unit) {
return unit;
}
}
return undefined;
};
export const getFlowRateInUnit = (
stateObj: HassEntity | undefined,
unit: string
): number | undefined => {
const value = getFlowRateFromState(stateObj);
const factor = FLOW_RATE_TO_LMIN[unit];
return value === undefined || factor === undefined ? value : value / factor;
};
/**
* Get current power value from entity state, normalized to watts (W)
* @param stateObj - The entity state object to get power value from
@@ -2280,6 +2353,7 @@ export const downloadEnergyData = (
const energy_sources = energyData.prefs.energy_sources;
const device_consumption = energyData.prefs.device_consumption;
const device_consumption_water = energyData.prefs.device_consumption_water;
const device_consumption_gas = energyData.prefs.device_consumption_gas;
const stats = state.stats;
interface CsvRow {
@@ -2460,6 +2534,16 @@ export const downloadEnergyData = (
printCategory("device_consumption_water", waterDevices, state.waterUnit);
}
if (device_consumption_gas) {
const gasDevices: string[] = [];
device_consumption_gas.forEach((source) => {
source = source as DeviceConsumptionEnergyPreference;
gasDevices.push(source.stat_consumption);
});
printCategory("device_consumption_gas", gasDevices, state.gasUnit);
}
const { summedData } = getSummedData(state);
const { consumption } = computeConsumptionData(summedData, undefined);
+1 -1
View File
@@ -10,7 +10,7 @@ export const voiceAssistants = {
},
"cloud.google_assistant": {
domain: "google_assistant",
name: "Google Assistant",
name: "Google Home",
},
} as const;
-99
View File
@@ -1,15 +1,5 @@
import type { HassService } from "home-assistant-js-websocket";
import { ensureArray } from "../common/array/ensure-array";
import { hasTemplate } from "../common/string/has-template";
import { isValidEntityId } from "../common/entity/valid_entity_id";
import {
combineVisible,
getHiddenFields,
} from "../components/ha-form/conditions";
import type {
HaFormDataContainer,
HaFormVisible,
} from "../components/ha-form/types";
import type { Context, HomeAssistant, ServiceCallRequest } from "../types";
import type { Action } from "./script";
@@ -50,92 +40,3 @@ export const getServiceCallEntityIds = (
.filter(isValidEntityId)
),
];
interface ServiceFieldOrSection {
visible?: HaFormVisible;
default?: unknown;
fields?: Record<string, { visible?: HaFormVisible; default?: unknown }>;
}
// Fields inside a section are flattened, they are only shown when both the
// section and the field are visible.
export const getServiceFieldVisibility = (
fields: HassService["fields"]
): [string, HaFormVisible | undefined][] =>
Object.entries(fields as Record<string, ServiceFieldOrSection>).flatMap(
([key, field]) =>
field.fields
? Object.entries(field.fields).map(
([subKey, subField]) =>
[subKey, combineVisible(field.visible, subField.visible)] as [
string,
HaFormVisible,
]
)
: [[key, field.visible] as [string, HaFormVisible | undefined]]
);
export const getServiceFieldDefaults = (
fields: HassService["fields"]
): HaFormDataContainer => {
const defaults: HaFormDataContainer = {};
for (const [key, field] of Object.entries(
fields as Record<string, ServiceFieldOrSection>
)) {
const entries = field.fields
? Object.entries(field.fields)
: [[key, field]];
for (const [fieldKey, subField] of entries as [
string,
{ default?: HaFormDataContainer[string] },
][]) {
if (subField.default !== undefined) {
defaults[fieldKey] = subField.default;
}
}
}
return defaults;
};
const getConditionFields = (visible: HaFormVisible | undefined): string[] =>
visible === undefined || typeof visible === "boolean"
? []
: ensureArray(visible).flatMap((condition) =>
"condition" in condition
? condition.conditions.flatMap((sub) => getConditionFields(sub))
: [condition.field]
);
// Like in config flows and blueprints, a field without a value is evaluated
// with its default. A template is only known when the action runs, so a
// condition on a field holding one never hides anything.
export const getServiceHiddenFields = (
visibility: [string, HaFormVisible | undefined][],
defaults: HaFormDataContainer,
data: Record<string, any> | undefined
): Set<string> => {
const values = { ...defaults };
const templates = new Set<string>();
for (const [key, value] of Object.entries(data ?? {})) {
if (value !== undefined) {
values[key] = value;
}
if (hasTemplate(value)) {
templates.add(key);
}
}
return getHiddenFields(
visibility.map(([key, visible]) => [
key,
visible === undefined || typeof visible === "boolean"
? visible
: ensureArray(visible).filter(
(condition) =>
!getConditionFields(condition).some((field) =>
templates.has(field)
)
),
]),
values
);
};
@@ -839,6 +839,9 @@ class DataEntryFlowDialog extends DirtyStateProviderMixin<
return [
haStyleDialog,
css`
ha-dialog {
--ha-dialog-header-title-height: auto;
}
.dialog-title {
overflow: hidden;
text-overflow: ellipsis;
@@ -375,21 +375,16 @@ export class HaVoiceAssistantSetupStepSuccess extends LitElement {
margin-top: 24px;
}
.rows {
gap: var(--ha-space-4);
display: flex;
flex-direction: column;
}
.row {
display: flex;
justify-content: space-between;
display: grid;
grid-template-columns: minmax(0, 1fr) auto;
gap: var(--ha-space-4) var(--ha-space-1);
align-items: center;
}
.row > *:first-child {
flex: 1;
margin-right: 4px;
.row {
display: contents;
}
.row ha-button {
width: 82px;
.row > :only-child {
grid-column: 1 / -1;
}
ha-select {
+6 -44
View File
@@ -27,9 +27,10 @@ import {
dataTableQueryStateFromUrl,
} from "../common/url/data-table-query-params";
import "../components/chips/ha-assist-chip";
import { GROUP_ACTIONS_SLOT_PREFIX } from "../components/data-table/ha-data-table";
import "../components/data-table/ha-data-table";
import type {
DataTableColumnContainer,
DataTableGroupActions,
DataTableRowData,
HaDataTable,
SortingDirection,
@@ -216,6 +217,9 @@ export class HaTabsSubpageDataTable extends KeyboardShortcutMixin(LitElement) {
@property({ attribute: false }) public groupOrder?: string[];
@property({ attribute: false })
public renderGroupActions?: DataTableGroupActions;
@property({ attribute: false }) public columnOrder?: string[];
@property({ attribute: false }) public hiddenColumns?: string[];
@@ -627,13 +631,11 @@ export class HaTabsSubpageDataTable extends KeyboardShortcutMixin(LitElement) {
.sortDirection=${this._sortDirection}
.groupColumn=${this._groupColumn}
.groupOrder=${this.groupOrder}
.renderGroupActions=${this.renderGroupActions}
.initialCollapsedGroups=${this.initialCollapsedGroups}
.columnOrder=${this.columnOrder}
.hiddenColumns=${this.hiddenColumns}
>
${this._groupActionsSlots.map(
(slot) => html`<slot name=${slot} slot=${slot}></slot>`
)}
${
!this.narrow
? html`
@@ -849,46 +851,6 @@ export class HaTabsSubpageDataTable extends KeyboardShortcutMixin(LitElement) {
this._setGroupColumn(group);
}
// Slots of the group actions the page supplies, passed through to the table
@state() private _groupActionsSlots: string[] = [];
private _groupActionsObserver = new MutationObserver(() =>
this._updateGroupActionsSlots()
);
public connectedCallback(): void {
super.connectedCallback();
this._updateGroupActionsSlots();
// Subtree, so slot changes on the page's children are seen too
this._groupActionsObserver.observe(this, {
childList: true,
subtree: true,
attributeFilter: ["slot"],
});
}
public disconnectedCallback(): void {
super.disconnectedCallback();
this._groupActionsObserver.disconnect();
}
private _updateGroupActionsSlots() {
const slots = [
...new Set(
Array.from(this.children, (child) => child.slot).filter((slot) =>
slot.startsWith(GROUP_ACTIONS_SLOT_PREFIX)
)
),
];
if (
slots.length !== this._groupActionsSlots.length ||
slots.some((slot, index) => slot !== this._groupActionsSlots[index])
) {
this._groupActionsSlots = slots;
}
}
private _setGroupColumn(columnId: string, link = false) {
this._groupColumn = columnId;
fireEvent(this, "grouping-changed", { value: columnId, link });
+107 -4
View File
@@ -10,6 +10,7 @@ import {
mdiScriptText,
mdiShape,
mdiTools,
mdiTransitConnectionVariant,
} from "@mdi/js";
import { startOfYesterday } from "date-fns";
import type { HassEntity } from "home-assistant-js-websocket/dist/types";
@@ -19,6 +20,7 @@ import { customElement, property, state } from "lit/decorators";
import memoizeOne from "memoize-one";
import { isComponentLoaded } from "../../../common/config/is_component_loaded";
import { consume } from "../../../common/decorators/consume";
import { transform } from "../../../common/decorators/transform";
import type { HASSDomCurrentTargetEvent } from "../../../common/dom/fire_event";
import { computeDeviceNameDisplay } from "../../../common/entity/compute_device_name";
import { computeDomain } from "../../../common/entity/compute_domain";
@@ -34,9 +36,11 @@ import "../../../components/ha-card";
import "../../../components/ha-dropdown";
import type { HaDropdownSelectEvent } from "../../../components/ha-dropdown";
import "../../../components/ha-dropdown-item";
import "../../../components/ha-domain-icon";
import "../../../components/ha-icon-button";
import "../../../components/ha-icon-button-next";
import "../../../components/ha-icon-next";
import "../../../components/ha-state-icon";
import "../../../components/ha-svg-icon";
import "../../../components/ha-tooltip";
import "../../../components/item/ha-list-item-base";
@@ -49,7 +53,13 @@ import {
updateAreaRegistryEntry,
} from "../../../data/area/area_registry";
import type { AutomationEntity } from "../../../data/automation";
import { fireRelatedContext, fullEntitiesContext } from "../../../data/context";
import type { ConfigEntry } from "../../../data/config_entries";
import {
brandsAccessTokenContext,
configEntriesContext,
fireRelatedContext,
fullEntitiesContext,
} from "../../../data/context";
import type { DeviceRegistryEntry } from "../../../data/device/device_registry";
import { sortDeviceRegistryByName } from "../../../data/device/device_registry";
import type { EntityRegistryEntry } from "../../../data/entity/entity_registry";
@@ -68,6 +78,7 @@ import "../../../layouts/hass-error-screen";
import "../../../layouts/hass-subpage";
import { haStyle } from "../../../resources/styles";
import type { HomeAssistant } from "../../../types";
import { brandsUrl } from "../../../util/brands-url";
import "../../logbook/ha-logbook";
import { isHelperDomain } from "../helpers/const";
import {
@@ -149,6 +160,24 @@ class HaConfigAreaPage extends LitElement {
@consume({ context: fullEntitiesContext, subscribe: true })
_entityReg: EntityRegistryEntry[] = [];
// Integration domain per config entry ID, to show the brand icon of a device
@state()
@consume({ context: configEntriesContext, subscribe: true })
@transform<ConfigEntry[], Record<string, string>>({
transformer: (entries) =>
entries
? Object.fromEntries(
entries.map((entry) => [entry.entry_id, entry.domain])
)
: undefined,
})
private _configEntryDomains?: Record<string, string>;
@state()
@consume({ context: brandsAccessTokenContext, subscribe: true })
// @ts-ignore re-render trigger only, its value is never read
private _brandsAccessToken?: string;
@state() private _related?: RelatedResult;
private _logbookTime = { recent: 86400 };
@@ -387,6 +416,7 @@ class HaConfigAreaPage extends LitElement {
.href=${`/config/devices/device/${device.id}`}
.headline=${device.name}
>
${this._renderDeviceIcon(device)}
<ha-icon-next slot="end"></ha-icon-next>
</ha-list-item-button>
`
@@ -419,6 +449,7 @@ class HaConfigAreaPage extends LitElement {
.entity=${entity}
.headline=${entity.name ?? undefined}
>
${this._renderEntityIcon(entity)}
<ha-icon-next slot="end"></ha-icon-next>
</ha-list-item-button>
`
@@ -794,14 +825,73 @@ class HaConfigAreaPage extends LitElement {
return { groupedEntities, relatedEntities };
}
private _renderDeviceIcon(device: DeviceRegistryEntry) {
const entryIds = device.primary_config_entry
? [device.primary_config_entry, ...device.config_entries]
: device.config_entries;
const domain = entryIds
.map((entryId) => this._configEntryDomains?.[entryId])
.find(Boolean);
if (!domain) {
return html`<ha-svg-icon
slot="start"
.path=${
device.entry_type === "service"
? mdiTransitConnectionVariant
: mdiDevices
}
></ha-svg-icon>`;
}
return html`<img
slot="start"
alt=""
crossorigin="anonymous"
referrerpolicy="no-referrer"
src=${brandsUrl(
{
domain,
type: "icon",
darkOptimized: this.hass.themes?.darkMode,
},
this.hass.auth.data.hassUrl
)}
@error=${this._onImageError}
@load=${this._onImageLoad}
/>`;
}
private _renderEntityIcon(entity: EntityRegistryEntry) {
const stateObj = this.hass.states[entity.entity_id];
return stateObj
? html`<ha-state-icon slot="start" .stateObj=${stateObj}></ha-state-icon>`
: html`<ha-domain-icon
slot="start"
.domain=${computeDomain(entity.entity_id)}
.icon=${entity.icon ?? undefined}
></ha-domain-icon>`;
}
// Keep the space of a brand icon that failed to load so the rows stay aligned
private _onImageError(ev: HASSDomCurrentTargetEvent<HTMLImageElement>) {
ev.currentTarget.style.visibility = "hidden";
}
private _onImageLoad(ev: HASSDomCurrentTargetEvent<HTMLImageElement>) {
ev.currentTarget.style.visibility = "";
}
private _renderScene(name: string, entityState: SceneEntity) {
return entityState.attributes.id
? this._renderLink(
name,
entityState,
`/config/scene/edit/${entityState.attributes.id}`
)
: this._renderNotEditable(
name,
entityState,
`scene-${slugify(entityState.entity_id)}`
);
}
@@ -810,22 +900,30 @@ class HaConfigAreaPage extends LitElement {
return entityState.attributes.id
? this._renderLink(
name,
entityState,
`/config/automation/edit/${encodeURIComponent(entityState.attributes.id)}`
)
: this._renderNotEditable(
name,
entityState,
`automation-${slugify(entityState.entity_id)}`
);
}
private _renderLink(name: string, href: string) {
private _renderLink(name: string, entityState: HassEntity, href: string) {
return html`<ha-list-item-button .href=${href} .headline=${name}>
<ha-state-icon slot="start" .stateObj=${entityState}></ha-state-icon>
<ha-icon-next slot="end"></ha-icon-next>
</ha-list-item-button>`;
}
private _renderNotEditable(name: string, id: string) {
private _renderNotEditable(
name: string,
entityState: HassEntity,
id: string
) {
return html`<ha-list-item-base id=${id} .headline=${name}>
<ha-state-icon slot="start" .stateObj=${entityState}></ha-state-icon>
<ha-icon-next slot="end"></ha-icon-next>
</ha-list-item-base>
<ha-tooltip .for=${id} .distance=${-4}>
@@ -841,7 +939,7 @@ class HaConfigAreaPage extends LitElement {
if (entry) {
url = `/config/script/edit/${entry.unique_id}`;
}
return this._renderLink(name, url);
return this._renderLink(name, entityState, url);
}
private async _findRelated() {
@@ -1017,6 +1115,11 @@ class HaConfigAreaPage extends LitElement {
ha-list-item-button ha-icon-next {
color: var(--secondary-text-color);
}
img[slot="start"] {
width: 24px;
height: 24px;
object-fit: contain;
}
ha-card > *:first-child {
margin-top: -16px;
}
@@ -122,7 +122,7 @@ import { documentationUrl } from "../../../util/documentation-url";
import { turnOnOffEntity } from "../../lovelace/common/entity/turn-on-off-entity";
import { showAreaRegistryDetailDialog } from "../areas/show-dialog-area-registry-detail";
import { showAssignCategoryDialog } from "../category/show-dialog-assign-category";
import { renderCategoryGroupActions } from "../category/render-category-group-actions";
import { categoryGroupActions } from "../category/category-group-actions";
import { showCategoryRegistryDetailDialog } from "../category/show-dialog-category-registry-detail";
import {
getAreaTableColumn,
@@ -537,6 +537,12 @@ class HaAutomationPicker extends SubscribeMixin(LitElement) {
}
.columns=${this._columns(this.narrow, this.hass.localize, automations)}
.initialGroupColumn=${this._tableSettings.grouping}
.renderGroupActions=${this._groupActions(
"automation",
this._categories,
this._tableSettings.grouping,
this._categoryDeleted
)}
.initialCollapsedGroups=${this._activeCollapsed}
.initialSorting=${this._tableSettings.sorting}
.columnOrder=${this._activeColumnOrder}
@@ -565,12 +571,6 @@ class HaAutomationPicker extends SubscribeMixin(LitElement) {
.path=${mdiHelpCircleOutline}
@click=${this._showHelp}
></ha-icon-button>
${renderCategoryGroupActions(
"automation",
this._categories,
this._tableSettings.grouping,
this._categoryDeleted
)}
<ha-filter-floor-areas
.type=${"automation"}
.value=${this._filters["ha-filter-floor-areas"]?.value}
@@ -941,6 +941,8 @@ class HaAutomationPicker extends SubscribeMixin(LitElement) {
});
};
private _groupActions = memoizeOne(categoryGroupActions);
private _categoryDeleted = (categoryId: string) => {
const value = this._filters["ha-filter-categories"]?.value;
@@ -1,6 +1,5 @@
import { html, nothing } from "lit";
import { repeat } from "lit/directives/repeat";
import { groupActionsSlot } from "../../../components/data-table/ha-data-table";
import type { DataTableGroupActions } from "../../../components/data-table/ha-data-table";
import type { CategoryRegistryEntry } from "../../../data/category_registry";
import "./ha-category-group-actions";
@@ -8,22 +7,21 @@ import "./ha-category-group-actions";
// group headers of a table grouped by category, which names its groups after
// the categories. Other groupings can have groups with the same names.
// onDeleted lets the page take a deleted category out of its filter.
export const renderCategoryGroupActions = (
export const categoryGroupActions = (
scope: string,
categories: CategoryRegistryEntry[] | undefined,
grouping: string | undefined,
onDeleted: (categoryId: string) => void
) =>
): DataTableGroupActions | undefined =>
grouping === "category"
? repeat(
categories ?? [],
(category) => category.category_id,
(category) =>
html`<ha-category-group-actions
slot=${groupActionsSlot(category.name)}
.category=${category}
.scope=${scope}
.onDeleted=${onDeleted}
></ha-category-group-actions>`
)
: nothing;
? (group) => {
const category = categories?.find((cat) => cat.name === group);
return category
? html`<ha-category-group-actions
.category=${category}
.scope=${scope}
.onDeleted=${onDeleted}
></ha-category-group-actions>`
: nothing;
}
: undefined;
@@ -95,6 +95,7 @@ import {
showAlertDialog,
showConfirmationDialog,
} from "../../../dialogs/generic/show-dialog-box";
import { showMoreInfoDialog } from "../../../dialogs/more-info/show-ha-more-info-dialog";
import { showVoiceAssistantSetupDialog } from "../../../dialogs/voice-assistant-setup/show-voice-assistant-setup-dialog";
import "../../../layouts/hass-error-screen";
import "../../../layouts/hass-subpage";
@@ -994,7 +995,13 @@ export class HaConfigDevicePage extends LitElement {
(batteryDomain === "binary_sensor" ||
!Number.isNaN(Number(battery.state)))
? html`
<div class="battery">
<button
class="battery"
aria-label=${`${this.hass.formatEntityName(
battery
)} ${this.hass.formatEntityState(battery)}`}
@click=${this._showBatteryMoreInfo}
>
${
batteryDomain === "sensor"
? this.hass.formatEntityState(battery)
@@ -1004,7 +1011,7 @@ export class HaConfigDevicePage extends LitElement {
.batteryStateObj=${battery}
.batteryChargingStateObj=${batteryChargingState}
></ha-battery-icon>
</div>
</button>
`
: ""
}
@@ -1387,6 +1394,15 @@ export class HaConfigDevicePage extends LitElement {
}
}
private _showBatteryMoreInfo() {
const batteryEntity = this._batteryEntity(
this._entities(this.deviceId, this._entityReg, this.hass.devices)
);
if (batteryEntity) {
showMoreInfoDialog(this, { entityId: batteryEntity.entity_id });
}
}
private _computeEntityName(
entity: EntityRegistryEntry,
devices: HomeAssistant["devices"]
@@ -1756,6 +1772,19 @@ export class HaConfigDevicePage extends LitElement {
align-items: center;
display: flex;
white-space: nowrap;
background: none;
border: none;
padding: 0;
margin: 0;
font: inherit;
color: inherit;
cursor: pointer;
border-radius: var(--ha-border-radius-sm);
}
.battery:focus-visible {
outline: 2px solid var(--primary-color);
outline-offset: 2px;
}
.column > *:not(:first-child) {
@@ -18,6 +18,7 @@ import { consume } from "../../../common/decorators/consume";
import { storage } from "../../../common/decorators/storage";
import type { HASSDomEvent } from "../../../common/dom/fire_event";
import { fireEvent } from "../../../common/dom/fire_event";
import { computeAreaName } from "../../../common/entity/compute_area_name";
import { computeDeviceNameDisplay } from "../../../common/entity/compute_device_name";
import { computeFloorName } from "../../../common/entity/compute_floor_name";
import { computeStateDomain } from "../../../common/entity/compute_state_domain";
@@ -28,13 +29,13 @@ import {
} from "../../../common/integrations/protocolIntegrationPicked";
import { navigate } from "../../../common/navigate";
import type { LocalizeFunc } from "../../../common/translations/localize";
import { deepEqual } from "../../../common/util/deep-equal";
import {
devicesFilterQueryParamConfig,
devicesFilterQueryParams,
devicesFilterQueryString,
devicesLinkFromUrl,
} from "../../../common/url/devices-query-params";
import { deepEqual } from "../../../common/util/deep-equal";
import {
hasRejectedItems,
rejectedItems,
@@ -47,8 +48,6 @@ import type {
import "../../../components/data-table/ha-data-table-labels";
import "../../../components/entity/ha-battery-icon";
import "../../../components/ha-alert";
import "../../../components/skeleton/ha-skeleton-icon";
import "../../../components/skeleton/ha-skeleton-text";
import "../../../components/ha-button";
import "../../../components/ha-dropdown";
import type { HaDropdownSelectEvent } from "../../../components/ha-dropdown";
@@ -58,6 +57,8 @@ import "../../../components/ha-filter-integrations";
import "../../../components/ha-filter-labels";
import "../../../components/ha-filter-states";
import "../../../components/ha-icon-button";
import "../../../components/skeleton/ha-skeleton-icon";
import "../../../components/skeleton/ha-skeleton-text";
import { createAreaRegistryEntry } from "../../../data/area/area_registry";
import type { ConfigEntry, SubEntry } from "../../../data/config_entries";
import { getSubEntries, sortConfigEntries } from "../../../data/config_entries";
@@ -67,7 +68,6 @@ import {
deserializeFilters,
serializeFilters,
} from "../../../data/data_table_filters";
import { computeDeviceAreaLabel } from "../../../data/device/device_picker";
import type {
DeviceEntityLookup,
DeviceRegistryEntry,
@@ -451,31 +451,19 @@ export class HaConfigDeviceDashboard extends LitElement {
const parentDevice = device.parent_device_id
? this.hass.devices[device.parent_device_id]
: undefined;
const viaDevice = device.via_device_id
? this.hass.devices[device.via_device_id]
: undefined;
// The device that identifies this device's family: its parent for a
// child device, itself for a device that has children.
const familyParentDevice =
parentDevice ??
(deviceIdsWithChildren.has(device.id) ? device : undefined);
const { areaName } = computeDeviceAreaLabel(
device,
this.hass.areas,
this.hass.devices,
this.hass.localize,
this.hass.language,
this.hass.translationMetadata
);
const area = getDeviceArea(device, areas, this.hass.devices);
const areaName = area ? computeAreaName(area) : undefined;
const floorArea =
getDeviceArea(device, areas, this.hass.devices) ??
(device.via_device_id && this.hass.devices[device.via_device_id]
? getDeviceArea(
this.hass.devices[device.via_device_id],
areas,
this.hass.devices
)
: undefined);
const floorId = floorArea?.floor_id;
const floorId = area?.floor_id;
const floorName =
floorId && this.hass.floors?.[floorId]
? computeFloorName(this.hass.floors[floorId])
@@ -511,6 +499,9 @@ export class HaConfigDeviceDashboard extends LitElement {
parent_device_name: parentDevice
? computeDeviceNameDisplay(parentDevice, this.hass.localize)
: "",
via_device_name: viaDevice
? computeDeviceNameDisplay(viaDevice, this.hass.localize)
: undefined,
// Grouping key that keeps a device with its family: children group
// under their parent's name, a parent groups under its own name, and
// standalone devices stay ungrouped. The name is always computed from
@@ -634,6 +625,15 @@ export class HaConfigDeviceDashboard extends LitElement {
minWidth: "120px",
template: (device) => device.parent_device_name || "",
},
via_device_name: {
title: localize("ui.panel.config.devices.data_table.connected_via"),
sortable: true,
filterable: true,
groupable: true,
urlName: "via",
defaultHidden: true,
minWidth: "120px",
},
manufacturer: {
title: localize("ui.panel.config.devices.data_table.manufacturer"),
sortable: true,
@@ -1,10 +1,11 @@
import {
mdiAlertCircle,
mdiDelete,
mdiWater,
mdiDragHorizontalVariant,
mdiFire,
mdiPencil,
mdiPlus,
mdiWater,
} from "@mdi/js";
import type { CSSResultGroup, TemplateResult } from "lit";
import { css, html, LitElement, nothing } from "lit";
@@ -24,9 +25,13 @@ import type {
EnergyPreferences,
EnergyPreferencesValidation,
EnergyValidationIssue,
WaterGasResource,
} from "../../../../data/energy";
import {
computeEnergyLabel,
getEnergyGasUnitClass,
getWaterGasDevices,
getWaterGasDevicesKey,
saveEnergyPreferences,
} from "../../../../data/energy";
import type { StatisticsMetaData } from "../../../../data/recorder";
@@ -37,14 +42,22 @@ import {
import { haStyle } from "../../../../resources/styles";
import type { HomeAssistant } from "../../../../types";
import { documentationUrl } from "../../../../util/documentation-url";
import { showEnergySettingsDeviceWaterDialog } from "../dialogs/show-dialogs-energy";
import { showEnergySettingsDeviceWaterGasDialog } from "../dialogs/show-dialogs-energy";
import "./ha-energy-validation-result";
import { energyCardStyles } from "./styles";
import type { LocalizeKeys } from "../../../../common/translations/localize";
@customElement("ha-energy-device-settings-water")
export class EnergyDeviceSettingsWater extends LitElement {
const ICONS: Record<WaterGasResource, string> = {
water: mdiWater,
gas: mdiFire,
};
@customElement("ha-energy-device-settings-water-gas")
export class EnergyDeviceSettingsWaterGas extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;
@property({ attribute: false }) public resource!: WaterGasResource;
@property({ attribute: false })
public preferences!: EnergyPreferences;
@@ -54,34 +67,51 @@ export class EnergyDeviceSettingsWater extends LitElement {
@property({ attribute: false })
public validationResult?: EnergyPreferencesValidation;
private get _devices(): DeviceConsumptionEnergyPreference[] {
return getWaterGasDevices(this.preferences, this.resource);
}
private _withDevices(
devices: DeviceConsumptionEnergyPreference[]
): EnergyPreferences {
return {
...this.preferences,
[getWaterGasDevicesKey(this.resource)]: devices,
} as EnergyPreferences;
}
private _t(key: string): string {
return this.hass.localize(
`ui.panel.config.energy.device_consumption_${this.resource}.${key}` as LocalizeKeys
);
}
protected render(): TemplateResult {
const devices = this._devices;
const validation =
this.validationResult?.[getWaterGasDevicesKey(this.resource)];
return html`
<ha-card>
<h1 class="card-header">
<ha-svg-icon .path=${mdiWater}></ha-svg-icon>
${this.hass.localize(
"ui.panel.config.energy.device_consumption_water.title"
)}
<ha-svg-icon .path=${ICONS[this.resource]}></ha-svg-icon>
${this._t("title")}
</h1>
<div class="card-content">
<p>
${this.hass.localize(
"ui.panel.config.energy.device_consumption_water.sub"
)}
${this._t("sub")}
<a
target="_blank"
rel="noopener noreferrer"
href=${documentationUrl(
this.hass,
"/docs/energy/water/#individual-devices"
`/docs/energy/${this.resource}/#individual-${this.resource}-devices`
)}
>${this.hass.localize(
"ui.panel.config.energy.device_consumption_water.learn_more"
)}</a
>${this._t("learn_more")}</a
>
</p>
${this.validationResult?.device_consumption_water.map(
${validation?.map(
(result) => html`
<ha-energy-validation-result
.hass=${this.hass}
@@ -90,7 +120,7 @@ export class EnergyDeviceSettingsWater extends LitElement {
`
)}
${
this.preferences.device_consumption_water.length > 0
devices.length > 0
? html`
<div class="items-container">
<ha-sortable
@@ -99,7 +129,7 @@ export class EnergyDeviceSettingsWater extends LitElement {
>
<div class="devices">
${repeat(
this.preferences.device_consumption_water,
devices,
(device) => device.stat_consumption,
(device, index) => html`
<div class="row" .device=${device}>
@@ -110,9 +140,7 @@ export class EnergyDeviceSettingsWater extends LitElement {
</div>
${this._renderName(device)}
${this._renderIssueIndicator(
this.validationResult?.device_consumption_water[
index
],
validation?.[index],
index
)}
<ha-icon-button
@@ -138,9 +166,7 @@ export class EnergyDeviceSettingsWater extends LitElement {
<div class="row">
<ha-button @click=${this._addDevice} appearance="filled" size="s">
<ha-svg-icon slot="start" .path=${mdiPlus}></ha-svg-icon
>${this.hass.localize(
"ui.panel.config.energy.device_consumption_water.add_device"
)}</ha-button
>${this._t("add_device")}</ha-button
>
</div>
</div>
@@ -203,64 +229,69 @@ export class EnergyDeviceSettingsWater extends LitElement {
private _itemMoved(ev: CustomEvent): void {
ev.stopPropagation();
const { oldIndex, newIndex } = ev.detail;
const devices = this.preferences.device_consumption_water.concat();
const devices = this._devices.concat();
const device = devices.splice(oldIndex, 1)[0];
devices.splice(newIndex, 0, device);
const newPrefs = {
...this.preferences,
device_consumption_water: devices,
};
const newPrefs = this._withDevices(devices);
fireEvent(this, "value-changed", { value: newPrefs });
this._savePreferences(newPrefs);
}
private _allowedUnitClass(excludeSource?: string) {
return this.resource === "gas"
? getEnergyGasUnitClass(
this.preferences,
excludeSource,
this.statsMetadata,
"device"
)
: undefined;
}
private _editDevice(ev) {
const origDevice: DeviceConsumptionEnergyPreference =
ev.currentTarget.closest(".row").device;
showEnergySettingsDeviceWaterDialog(this, {
showEnergySettingsDeviceWaterGasDialog(this, {
resource: this.resource,
statsMetadata: this.statsMetadata,
device: { ...origDevice },
device_consumptions: this.preferences
.device_consumption_water as DeviceConsumptionEnergyPreference[],
allowedUnitClass: this._allowedUnitClass(origDevice.stat_consumption),
device_consumptions: this._devices,
saveCallback: async (newDevice) => {
const newPrefs = {
...this.preferences,
device_consumption_water:
this.preferences.device_consumption_water.map((d) =>
d === origDevice ? newDevice : d
),
};
this._sanitizeParents(newPrefs);
await this._savePreferences(newPrefs);
const newPrefs = this._withDevices(
this._devices.map((d) => (d === origDevice ? newDevice : d))
);
await this._savePreferences(this._sanitizeParents(newPrefs));
},
});
}
private _addDevice() {
showEnergySettingsDeviceWaterDialog(this, {
showEnergySettingsDeviceWaterGasDialog(this, {
resource: this.resource,
statsMetadata: this.statsMetadata,
device_consumptions: this.preferences
.device_consumption_water as DeviceConsumptionEnergyPreference[],
allowedUnitClass: this._allowedUnitClass(),
device_consumptions: this._devices,
saveCallback: async (device) => {
await this._savePreferences({
...this.preferences,
device_consumption_water:
this.preferences.device_consumption_water.concat(device),
});
await this._savePreferences(
this._withDevices(this._devices.concat(device))
);
},
});
}
private _sanitizeParents(prefs: EnergyPreferences) {
const statIds = prefs.device_consumption_water.map(
(d) => d.stat_consumption
);
prefs.device_consumption_water.forEach((d) => {
if (d.included_in_stat && !statIds.includes(d.included_in_stat)) {
delete d.included_in_stat;
}
});
private _sanitizeParents(prefs: EnergyPreferences): EnergyPreferences {
const devices = getWaterGasDevices(prefs, this.resource);
const statIds = devices.map((d) => d.stat_consumption);
return {
...prefs,
[getWaterGasDevicesKey(this.resource)]: devices.map((d) =>
d.included_in_stat && !statIds.includes(d.included_in_stat)
? { ...d, included_in_stat: undefined }
: d
),
} as EnergyPreferences;
}
private async _deleteDevice(ev) {
@@ -276,14 +307,11 @@ export class EnergyDeviceSettingsWater extends LitElement {
}
try {
const newPrefs = {
...this.preferences,
device_consumption_water:
this.preferences.device_consumption_water.filter(
(device) => device !== deviceToDelete
),
};
this._sanitizeParents(newPrefs);
const newPrefs = this._sanitizeParents(
this._withDevices(
this._devices.filter((device) => device !== deviceToDelete)
)
);
await this._savePreferences(newPrefs);
} catch (err: any) {
showAlertDialog(this, { title: `Failed to save config: ${err.message}` });
@@ -330,6 +358,6 @@ export class EnergyDeviceSettingsWater extends LitElement {
declare global {
interface HTMLElementTagNameMap {
"ha-energy-device-settings-water": EnergyDeviceSettingsWater;
"ha-energy-device-settings-water-gas": EnergyDeviceSettingsWaterGas;
}
}
@@ -151,7 +151,8 @@ export class EnergyGasSettings extends LitElement {
allowedGasUnitClass: getEnergyGasUnitClass(
this.preferences,
undefined,
this.statsMetadata
this.statsMetadata,
"source"
),
gas_sources: this.preferences.energy_sources.filter(
(src) => src.type === "gas"
@@ -175,7 +176,8 @@ export class EnergyGasSettings extends LitElement {
allowedGasUnitClass: getEnergyGasUnitClass(
this.preferences,
origSource.stat_energy_from,
this.statsMetadata
this.statsMetadata,
"source"
),
gas_sources: this.preferences.energy_sources.filter(
(src) => src.type === "gas"
@@ -10,7 +10,10 @@ import "../../../../components/ha-dialog-footer";
import "../../../../components/input/ha-input";
import "./ha-energy-upstream-device-picker";
import type { HaInput } from "../../../../components/input/ha-input";
import type { DeviceConsumptionEnergyPreference } from "../../../../data/energy";
import type {
DeviceConsumptionEnergyPreference,
WaterGasResource,
} from "../../../../data/energy";
import {
computeEnergyLabel,
energyStatisticHelpUrl,
@@ -26,22 +29,22 @@ import { haStyleDialog } from "../../../../resources/styles";
import type { HomeAssistant, ValueChangedEvent } from "../../../../types";
import type { IntegralUnits } from "./energy-integral";
import { confirmIntegrals, integralUnits } from "./energy-integral";
import type { EnergySettingsDeviceWaterDialogParams } from "./show-dialogs-energy";
import type { EnergySettingsDeviceWaterGasDialogParams } from "./show-dialogs-energy";
import type { LocalizeKeys } from "../../../../common/translations/localize";
const volumeUnitClasses = ["volume"];
const flowRateUnitClasses = ["volume_flow_rate"];
// Without a water sensor, saving offers to create one from the flow rate sensor
// Without a consumption sensor, saving offers to create one from the flow rate sensor
type DeviceConsumptionDraft = Partial<DeviceConsumptionEnergyPreference>;
@customElement("dialog-energy-device-settings-water")
export class DialogEnergyDeviceSettingsWater
@customElement("dialog-energy-device-settings-water-gas")
export class DialogEnergyDeviceSettingsWaterGas
extends DirtyStateProviderMixin<DeviceConsumptionDraft | null>()(LitElement)
implements HassDialog<EnergySettingsDeviceWaterDialogParams>
implements HassDialog<EnergySettingsDeviceWaterGasDialogParams>
{
@property({ attribute: false }) public hass!: HomeAssistant;
@state() private _params?: EnergySettingsDeviceWaterDialogParams;
@state() private _params?: EnergySettingsDeviceWaterGasDialogParams;
@state() private _open = false;
@@ -49,6 +52,8 @@ export class DialogEnergyDeviceSettingsWater
@state() private _volume_units?: string[];
@state() private _energy_units?: string[];
@state() private _flow_rate_units?: string[];
@state() private _error?: string;
@@ -62,7 +67,7 @@ export class DialogEnergyDeviceSettingsWater
private _possibleParents: DeviceConsumptionEnergyPreference[] = [];
public async showDialog(
params: EnergySettingsDeviceWaterDialogParams
params: EnergySettingsDeviceWaterGasDialogParams
): Promise<void> {
this._params = params;
this._device = this._params.device;
@@ -70,8 +75,13 @@ export class DialogEnergyDeviceSettingsWater
this._saving = false;
this._computePossibleParents();
this._volume_units = (
await getSensorDeviceClassConvertibleUnits(this.hass, "water")
await getSensorDeviceClassConvertibleUnits(this.hass, params.resource)
).units;
this._energy_units =
params.resource === "gas"
? (await getSensorDeviceClassConvertibleUnits(this.hass, "energy"))
.units
: [];
this._flow_rate_units = (
await getSensorDeviceClassConvertibleUnits(this.hass, "volume_flow_rate")
).units;
@@ -86,6 +96,38 @@ export class DialogEnergyDeviceSettingsWater
this._initDirtyTracking({ type: "deep" }, this._device ?? null);
}
private get _resource(): WaterGasResource {
return this._params!.resource;
}
private _t(key: string, params?: Record<string, string>): string {
return this.hass.localize(
`ui.panel.config.energy.device_consumption_${this._resource}.dialog.${key}` as LocalizeKeys,
params as any
);
}
private get _unitClasses(): string[] {
if (this._params?.allowedUnitClass) {
return [this._params.allowedUnitClass];
}
return this._resource === "gas" ? ["volume", "energy"] : ["volume"];
}
private get _deviceClasses(): string[] | undefined {
if (this._resource === "water") {
return undefined;
}
switch (this._params?.allowedUnitClass) {
case "volume":
return ["gas"];
case "energy":
return ["energy"];
default:
return ["gas", "energy"];
}
}
private _computePossibleParents() {
if (!this._device || !this._params) {
this._possibleParents = [];
@@ -101,7 +143,7 @@ export class DialogEnergyDeviceSettingsWater
}
});
}
// Saving without a water sensor can reuse the original one
// Saving without a consumption sensor can reuse the original one
const stat =
this._device.stat_consumption ?? this._params.device?.stat_consumption;
if (stat) {
@@ -134,7 +176,15 @@ export class DialogEnergyDeviceSettingsWater
return nothing;
}
const pickableUnit = this._volume_units?.join(", ") || "";
const resource = this._resource;
const pickableUnit = [
...(this._params.allowedUnitClass === "energy"
? []
: this._volume_units || []),
...(this._params.allowedUnitClass === "volume"
? []
: this._energy_units || []),
].join(", ");
const labelStatistic =
this._device?.stat_consumption ?? this._device?.stat_rate;
@@ -142,9 +192,7 @@ export class DialogEnergyDeviceSettingsWater
return html`
<ha-dialog
.open=${this._open}
header-title=${this.hass.localize(
"ui.panel.config.energy.device_consumption_water.dialog.header"
)}
header-title=${this._t("header")}
.preventScrimClose=${this.isDirtyState}
@closed=${this._dialogClosed}
>
@@ -152,42 +200,33 @@ export class DialogEnergyDeviceSettingsWater
<ha-statistic-picker
.helpMissingEntityUrl=${energyStatisticHelpUrl}
.includeUnitClass=${volumeUnitClasses}
.includeUnitClass=${this._unitClasses}
.includeDeviceClass=${this._deviceClasses}
.value=${this._device?.stat_consumption}
.label=${this.hass.localize(
"ui.panel.config.energy.device_consumption_water.dialog.device_consumption_water"
)}
.label=${this._t(`device_consumption_${resource}`)}
.excludeStatistics=${this._excludeList}
@value-changed=${this._statisticChanged}
.disabled=${this._saving}
.helper=${this.hass.localize(
"ui.panel.config.energy.device_consumption_water.dialog.selected_stat_intro",
{ unit: pickableUnit }
)}
.helper=${this._t("selected_stat_intro", { unit: pickableUnit })}
autofocus
></ha-statistic-picker>
<ha-statistic-picker
.includeUnitClass=${flowRateUnitClasses}
.value=${this._device?.stat_rate}
.label=${this.hass.localize(
"ui.panel.config.energy.device_consumption_water.dialog.device_consumption_water_flow_rate"
)}
.label=${this._t(`device_consumption_${resource}_flow_rate`)}
.excludeStatistics=${this._excludeListFlowRate}
@value-changed=${this._flowRateStatisticChanged}
.disabled=${this._saving}
.invalid=${!!this._flowRateError()}
.errorMessage=${this._flowRateError()}
.helper=${this.hass.localize(
"ui.panel.config.energy.device_consumption_water.dialog.selected_stat_intro",
{ unit: this._flow_rate_units?.join(", ") || "" }
)}
.helper=${this._t("selected_stat_intro", {
unit: this._flow_rate_units?.join(", ") || "",
})}
></ha-statistic-picker>
<ha-input
.label=${this.hass.localize(
"ui.panel.config.energy.device_consumption_water.dialog.display_name"
)}
.label=${this._t("display_name")}
type="text"
.disabled=${!this._device || this._saving}
.value=${this._device?.name || ""}
@@ -207,18 +246,12 @@ export class DialogEnergyDeviceSettingsWater
<ha-energy-upstream-device-picker
.hass=${this.hass}
.label=${this.hass.localize(
"ui.panel.config.energy.device_consumption_water.dialog.included_in_device"
)}
.helper=${this.hass.localize(
"ui.panel.config.energy.device_consumption_water.dialog.included_in_device_helper"
)}
.label=${this._t("included_in_device")}
.helper=${this._t("included_in_device_helper")}
.value=${this._device?.included_in_stat}
.possibleParents=${this._possibleParents}
.statsMetadata=${this._params.statsMetadata}
.emptyLabel=${this.hass.localize(
"ui.panel.config.energy.device_consumption_water.dialog.no_upstream_devices"
)}
.emptyLabel=${this._t("no_upstream_devices")}
.disabled=${!this._device || this._saving}
@value-changed=${this._parentChanged}
></ha-energy-upstream-device-picker>
@@ -258,7 +291,7 @@ export class DialogEnergyDeviceSettingsWater
this._device =
device.stat_consumption || device.stat_rate ? device : undefined;
this._computePossibleParents();
// The new water sensor can rule out the picked upstream device
// The new consumption sensor can rule out the picked upstream device
if (
device.included_in_stat &&
!this._possibleParents.some(
@@ -332,14 +365,20 @@ export class DialogEnergyDeviceSettingsWater
) {
return undefined;
}
return this.hass.localize(
"ui.panel.config.energy.device_consumption_water.dialog.flow_rate_no_volume"
return this._t(
this._params?.allowedUnitClass === "energy"
? "flow_rate_energy_sources"
: "flow_rate_no_volume"
);
}
private _integralUnits(): IntegralUnits | undefined {
const statRate = this._device?.stat_rate;
if (!statRate || !this._volume_units) {
if (
!statRate ||
!this._volume_units ||
this._params?.allowedUnitClass === "energy"
) {
return undefined;
}
return integralUnits(
@@ -358,7 +397,7 @@ export class DialogEnergyDeviceSettingsWater
this._saving = true;
try {
const statConsumption =
draft.stat_consumption ?? (await this._waterFromFlowRate(draft));
draft.stat_consumption ?? (await this._volumeFromFlowRate(draft));
if (!statConsumption) {
return;
}
@@ -383,7 +422,7 @@ export class DialogEnergyDeviceSettingsWater
}
}
private async _waterFromFlowRate(
private async _volumeFromFlowRate(
draft: DeviceConsumptionDraft
): Promise<string | undefined> {
const statRate = draft.stat_rate!;
@@ -391,8 +430,9 @@ export class DialogEnergyDeviceSettingsWater
if (!units) {
return undefined;
}
const resource = this._resource;
const sensors = await confirmIntegrals(this, this.hass, {
deviceClass: "water",
deviceClass: resource,
states: this.hass.states,
entities: this.hass.entities,
requests: [
@@ -400,7 +440,7 @@ export class DialogEnergyDeviceSettingsWater
rate: statRate,
units,
name: this.hass.localize(
"ui.panel.config.energy.integral.water.sensor_name",
`ui.panel.config.energy.integral.${resource}.sensor_name` as LocalizeKeys,
{
name: computeEnergyLabel(
this.hass.states,
@@ -409,13 +449,11 @@ export class DialogEnergyDeviceSettingsWater
this._params?.statsMetadata?.[statRate],
draft.name
),
}
} as any
),
},
],
intro: this.hass.localize(
"ui.panel.config.energy.device_consumption_water.dialog.water_sensor_needed"
),
intro: this._t(`${resource}_sensor_needed`),
exclude: this._excludeList,
statsMetadata: this._params?.statsMetadata,
});
@@ -448,6 +486,6 @@ export class DialogEnergyDeviceSettingsWater
declare global {
interface HTMLElementTagNameMap {
"dialog-energy-device-settings-water": DialogEnergyDeviceSettingsWater;
"dialog-energy-device-settings-water-gas": DialogEnergyDeviceSettingsWaterGas;
}
}
@@ -9,6 +9,7 @@ import type {
SolarSourceTypeEnergyPreference,
WaterSourceTypeEnergyPreference,
HomeSourceTypeEnergyPreference,
WaterGasResource,
} from "../../../../data/energy";
import type { StatisticsMetaData } from "../../../../data/recorder";
import type { HomeAssistant } from "../../../../types";
@@ -63,10 +64,12 @@ export interface EnergySettingsDeviceDialogParams {
saveCallback: (device: DeviceConsumptionEnergyPreference) => Promise<void>;
}
export interface EnergySettingsDeviceWaterDialogParams {
export interface EnergySettingsDeviceWaterGasDialogParams {
resource: WaterGasResource;
device?: DeviceConsumptionEnergyPreference;
device_consumptions: DeviceConsumptionEnergyPreference[];
statsMetadata?: Record<string, StatisticsMetaData>;
allowedUnitClass?: EnergyGasUnitClass;
saveCallback: (device: DeviceConsumptionEnergyPreference) => Promise<void>;
}
@@ -136,14 +139,14 @@ export const showEnergySettingsWaterDialog = (
});
};
export const showEnergySettingsDeviceWaterDialog = (
export const showEnergySettingsDeviceWaterGasDialog = (
element: HTMLElement,
dialogParams: EnergySettingsDeviceWaterDialogParams
dialogParams: EnergySettingsDeviceWaterGasDialogParams
): void => {
fireEvent(element, "show-dialog", {
dialogTag: "dialog-energy-device-settings-water",
dialogImport: () => import("./dialog-energy-device-settings-water"),
dialogParams: dialogParams,
dialogTag: "dialog-energy-device-settings-water-gas",
dialogImport: () => import("./dialog-energy-device-settings-water-gas"),
dialogParams,
});
};
+14 -4
View File
@@ -30,7 +30,7 @@ import { haStyle } from "../../../resources/styles";
import type { HomeAssistant, Route } from "../../../types";
import "../../../components/ha-alert";
import "./components/ha-energy-device-settings";
import "./components/ha-energy-device-settings-water";
import "./components/ha-energy-device-settings-water-gas";
import "./components/ha-energy-grid-settings";
import "./components/ha-energy-solar-settings";
import "./components/ha-energy-home-settings";
@@ -46,6 +46,7 @@ const INITIAL_CONFIG: EnergyPreferences = {
energy_sources: [],
device_consumption: [],
device_consumption_water: [],
device_consumption_gas: [],
};
const TABS: PageNavigation[] = [
@@ -202,6 +203,14 @@ class HaConfigEnergy extends LitElement {
.validationResult=${this._validationResult}
@value-changed=${this._prefsChanged}
></ha-energy-gas-settings>
<ha-energy-device-settings-water-gas
.hass=${this.hass}
.resource=${"gas"}
.preferences=${this._preferences!}
.statsMetadata=${this._statsMetadata}
.validationResult=${this._validationResult}
@value-changed=${this._prefsChanged}
></ha-energy-device-settings-water-gas>
`;
case "water":
return html`
@@ -212,13 +221,14 @@ class HaConfigEnergy extends LitElement {
.validationResult=${this._validationResult}
@value-changed=${this._prefsChanged}
></ha-energy-water-settings>
<ha-energy-device-settings-water
<ha-energy-device-settings-water-gas
.hass=${this.hass}
.resource=${"water"}
.preferences=${this._preferences!}
.statsMetadata=${this._statsMetadata}
.validationResult=${this._validationResult}
@value-changed=${this._prefsChanged}
></ha-energy-device-settings-water>
></ha-energy-device-settings-water-gas>
`;
default:
return nothing;
@@ -343,7 +353,7 @@ class HaConfigEnergy extends LitElement {
ha-energy-gas-settings,
ha-energy-water-settings,
ha-energy-device-settings,
ha-energy-device-settings-water {
ha-energy-device-settings-water-gas {
display: block;
margin-bottom: 12px;
}
@@ -1742,7 +1742,7 @@ ${rejected
}
}
// Alexa and Google Assistant can't be exposed to from the UI when they
// Alexa and Google Home can't be exposed to from the UI when they
// are configured manually in YAML, same as on the voice assistants expose page
private _exposableAssistants = memoize(
(cloudStatus: CloudStatus | undefined, hass: HomeAssistant) =>
+51
View File
@@ -38,3 +38,54 @@ export type Helper =
type HelperWithoutId<T> = T extends { id: string } ? Omit<T, "id"> : never;
export type HelperFormItem = HelperWithoutId<Helper>;
export const HELPER_CATEGORIES = [
"input",
"time",
"calculation",
"other",
] as const;
export type HelperCategory = (typeof HELPER_CATEGORIES)[number];
// Helper integrations have no category or short description in their
// manifest, so the ones shipped with Home Assistant are listed here. Any other
// helper falls into "other" and has no description.
const HELPER_DOMAIN_CATEGORIES = {
input_boolean: "input",
input_button: "input",
input_datetime: "input",
input_number: "input",
input_select: "input",
input_text: "input",
counter: "input",
schedule: "time",
timer: "time",
tod: "time",
history_stats: "time",
derivative: "calculation",
filter: "calculation",
integration: "calculation",
min_max: "calculation",
mold_indicator: "calculation",
random: "calculation",
statistics: "calculation",
threshold: "calculation",
trend: "calculation",
template: "calculation",
utility_meter: "calculation",
generic_hygrostat: "other",
generic_thermostat: "other",
group: "other",
otp: "other",
switch_as_x: "other",
} as const satisfies Record<string, HelperCategory>;
export type KnownHelperDomain = keyof typeof HELPER_DOMAIN_CATEGORIES;
export const isKnownHelperDomain = (
domain: string
): domain is KnownHelperDomain => domain in HELPER_DOMAIN_CATEGORIES;
export const getHelperCategory = (domain: string): HelperCategory =>
isKnownHelperDomain(domain) ? HELPER_DOMAIN_CATEGORIES[domain] : "other";
+251 -173
View File
@@ -1,32 +1,33 @@
import { mdiAlertOutline } from "@mdi/js";
import type { CSSResultGroup, TemplateResult } from "lit";
import Fuse from "fuse.js";
import type { HassConfig } from "home-assistant-js-websocket";
import type { CSSResultGroup, PropertyValues, TemplateResult } from "lit";
import { css, html, LitElement, nothing } from "lit";
import { customElement, property, query, state } from "lit/decorators";
import memoizeOne from "memoize-one";
import { isComponentLoaded } from "../../../common/config/is_component_loaded";
import { dynamicElement } from "../../../common/dom/dynamic-element-directive";
import type {
HASSDomCurrentTargetEvent,
HASSDomEvent,
HASSDomTargetEvent,
} from "../../../common/dom/fire_event";
import type { HASSDomEvent } from "../../../common/dom/fire_event";
import { fireEvent } from "../../../common/dom/fire_event";
import { stopPropagation } from "../../../common/dom/stop_propagation";
import { stringCompare } from "../../../common/string/compare";
import type { LocalizeFunc } from "../../../common/translations/localize";
import "../../../components/ha-area-picker";
import "../../../components/ha-button";
import "../../../components/ha-combo-box-item";
import "../../../components/ha-dialog";
import "../../../components/ha-dialog-footer";
import "../../../components/ha-domain-icon";
import "../../../components/ha-icon-next";
import "../../../components/ha-picker-combo-box";
import type {
HaPickerComboBox,
PickerComboBoxIndexSelectedDetail,
PickerComboBoxItem,
} from "../../../components/ha-picker-combo-box";
import "../../../components/ha-spinner";
import "../../../components/ha-svg-icon";
import "../../../components/ha-tooltip";
import "../../../components/input/ha-input-search";
import type { HaInputSearch } from "../../../components/input/ha-input-search";
import "../../../components/item/ha-list-item-option";
import type { HaListItemOption } from "../../../components/item/ha-list-item-option";
import "../../../components/list/ha-list-selectable";
import type { HaListSelectable } from "../../../components/list/ha-list-selectable";
import { DirtyStateProviderMixin } from "../../../mixins/dirty-state-provider-mixin";
import { getConfigFlowHandlers } from "../../../data/config_flow";
import { createCounter } from "../../../data/counter";
import {
@@ -51,11 +52,25 @@ import { createTimer } from "../../../data/timer";
import { showConfigFlowDialog } from "../../../dialogs/config-flow/show-dialog-config-flow";
import { showAlertDialog } from "../../../dialogs/generic/show-dialog-box";
import { showMoreInfoDialog } from "../../../dialogs/more-info/show-ha-more-info-dialog";
import { haStyleDialog, haStyleScrollbar } from "../../../resources/styles";
import { DirtyStateProviderMixin } from "../../../mixins/dirty-state-provider-mixin";
import {
multiTermSortedSearch,
type FuseWeightedKey,
} from "../../../resources/fuseMultiTerm";
import { haStyleDialog } from "../../../resources/styles";
import type { HomeAssistant, ValueChangedEvent } from "../../../types";
import { brandsUrl } from "../../../util/brands-url";
import type { Helper, HelperDomain, HelperFormItem } from "./const";
import { isHelperDomain } from "./const";
import type {
Helper,
HelperCategory,
HelperDomain,
HelperFormItem,
} from "./const";
import {
getHelperCategory,
HELPER_CATEGORIES,
isHelperDomain,
isKnownHelperDomain,
} from "./const";
import type { ShowDialogHelperDetailParams } from "./show-dialog-helper-detail";
type HelperCreators = Record<
@@ -116,6 +131,18 @@ const HELPERS: HelperCreators = {
},
};
interface HelperPickerItem extends PickerComboBoxItem {
category: HelperCategory;
loaded: boolean;
}
const HELPER_SEARCH_KEYS: FuseWeightedKey[] = [
{ name: "primary", weight: 10 },
{ name: "search_labels.aliases", weight: 8 },
{ name: "id", weight: 4 },
{ name: "secondary", weight: 2 },
];
@customElement("dialog-helper-detail")
export class DialogHelperDetail extends DirtyStateProviderMixin<
{ item?: HelperFormItem; area?: string } | undefined
@@ -128,6 +155,8 @@ export class DialogHelperDetail extends DirtyStateProviderMixin<
@state() private _open = false;
@state() private _shown = false;
@state() private _domain?: string;
@state() private _error?: string;
@@ -138,10 +167,11 @@ export class DialogHelperDetail extends DirtyStateProviderMixin<
@state() private _loading = false;
@state() private _filter?: string;
private _category?: HelperCategory;
@query("ha-input-search")
private _searchInput?: HTMLElement & { updateComplete?: Promise<unknown> };
private _search?: string;
@query("ha-picker-combo-box") private _comboBox?: HaPickerComboBox;
private _pendingConfigFlow?: {
startFlowHandler: string;
@@ -157,6 +187,8 @@ export class DialogHelperDetail extends DirtyStateProviderMixin<
this._domain = params.domain;
this._item = params.item;
this._area = undefined;
this._category = undefined;
this._search = undefined;
if (this._domain && this._domain in HELPERS) {
this._loading = true;
try {
@@ -178,19 +210,41 @@ export class DialogHelperDetail extends DirtyStateProviderMixin<
this._helperFlows = flows;
await this.updateComplete;
await this._focusSearchInput();
this._comboBox?.refreshItems();
this._focusPicker();
}
protected updated(changedProps: PropertyValues<this>): void {
super.updated(changedProps);
const oldHass = changedProps.get("hass");
if (
oldHass &&
(oldHass.localize !== this.hass.localize ||
oldHass.config !== this.hass.config)
) {
this._comboBox?.refreshItems();
}
}
public closeDialog(): void {
this._open = false;
}
private _dialogShown(ev: Event): void {
if (ev.eventPhase !== Event.AT_TARGET) {
return;
}
this._shown = true;
}
private _dialogClosed(): void {
this._open = false;
this._shown = false;
this._error = undefined;
this._domain = undefined;
this._params = undefined;
this._filter = undefined;
this._category = undefined;
this._search = undefined;
fireEvent(this, "dialog-closed", { dialog: this.localName });
if (this._pendingConfigFlow) {
@@ -211,6 +265,7 @@ export class DialogHelperDetail extends DirtyStateProviderMixin<
}
let content: TemplateResult;
let footer: TemplateResult | typeof nothing = nothing;
let showPicker = false;
if (this._domain && !this._loading) {
content = html`
@@ -255,78 +310,34 @@ export class DialogHelperDetail extends DirtyStateProviderMixin<
} else if (this._loading || this._helperFlows === undefined) {
content = html`<ha-spinner></ha-spinner>`;
} else {
const items = this._filterHelpers(
HELPERS,
this._helperFlows,
this._filter
);
showPicker = true;
content = html`
<ha-input-search
appearance="outlined"
<ha-picker-combo-box
autofocus
.value=${this._filter}
@input=${this._filterChanged}
.placeholder=${this.hass.localize(
mode="dialog"
.shown=${this._shown}
no-sort
.label=${this.hass.localize(
"ui.panel.config.integrations.search_helper"
)}
></ha-input-search>
<ha-list-selectable
controlled
aria-label=${this.hass.localize(
"ui.panel.config.helpers.dialog.create_helper"
.getItems=${this._getItems}
.rowRenderer=${this._renderHelperRow}
.sections=${this._getSections(
this._helperFlows,
this.hass.localize,
this.hass.config,
this.hass.locale.language
)}
@ha-list-item-selected=${this._domainPicked}
>
${items.map(([domain, label]) => {
// Only OG helpers need to be loaded prior adding one
const isLoaded =
!(domain in HELPERS) ||
isComponentLoaded(this.hass.config, domain);
return html`
<ha-list-item-option appearance="line" .value=${domain}>
<img
slot="start"
loading="lazy"
alt=""
src=${brandsUrl(
{
domain,
type: "icon",
darkOptimized: this.hass.themes?.darkMode,
},
this.hass.auth.data.hassUrl
)}
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>
<span slot="headline">${label}</span>
${
isLoaded
? html`<ha-icon-next slot="end"></ha-icon-next>`
: html`<ha-svg-icon
slot="end"
.id="icon-${domain}"
path=${mdiAlertOutline}
@click=${stopPropagation}
></ha-svg-icon>
<ha-tooltip slot="end" .for="icon-${domain}">
${this.hass.localize(
"ui.dialogs.helper_settings.platform_not_loaded",
{ platform: domain }
)}
</ha-tooltip>`
}
</ha-list-item-option>
`;
})}
</ha-list-selectable>
.selectedSection=${this._category}
@index-selected=${this._helperPicked}
></ha-picker-combo-box>
`;
}
return html`
<ha-dialog
class=${showPicker ? "picker" : ""}
.flexContent=${showPicker}
.open=${this._open}
.preventScrimClose=${this.isDirtyState}
header-title=${
@@ -346,6 +357,7 @@ export class DialogHelperDetail extends DirtyStateProviderMixin<
)
: this.hass.localize("ui.panel.config.helpers.dialog.create_helper")
}
@after-show=${this._dialogShown}
@closed=${this._dialogClosed}
>
${content} ${footer}
@@ -353,51 +365,148 @@ export class DialogHelperDetail extends DirtyStateProviderMixin<
`;
}
private _filterHelpers = memoizeOne(
private _getHelperItems = memoizeOne(
(
predefinedHelpers: HelperCreators,
flowHelpers?: string[],
filter?: string
) => {
const items: [string, string][] = [];
for (const helper of Object.keys(
predefinedHelpers
) as (keyof typeof predefinedHelpers)[]) {
items.push([
helper,
this.hass.localize(`ui.panel.config.helpers.types.${helper}`) ||
helper,
]);
}
if (flowHelpers) {
for (const domain of flowHelpers) {
items.push([domain, domainToName(this.hass.localize, domain)]);
}
}
return items
.filter(([domain, label]) => {
if (filter) {
const lowerFilter = filter.toLowerCase();
return (
label.toLowerCase().includes(lowerFilter) ||
domain.toLowerCase().includes(lowerFilter) ||
(predefinedHelpers[domain as HelperDomain]?.alias || []).some(
(alias) => alias.toLowerCase().includes(lowerFilter)
)
);
}
return true;
flowHelpers: string[],
localize: LocalizeFunc,
config: HassConfig,
language: string
): HelperPickerItem[] => {
const domains = [
...(Object.keys(HELPERS) as HelperDomain[]),
...flowHelpers,
];
return domains
.map((domain) => {
const helperDomain = isHelperDomain(domain) ? domain : undefined;
return {
id: domain,
primary:
(helperDomain &&
localize(`ui.panel.config.helpers.types.${helperDomain}`)) ||
domainToName(localize, domain),
secondary: isKnownHelperDomain(domain)
? localize(
`ui.panel.config.helpers.dialog.descriptions.${domain}`
)
: undefined,
category: getHelperCategory(domain),
// Only OG helpers need to be loaded prior adding one
loaded: !helperDomain || isComponentLoaded(config, domain),
search_labels: {
aliases: helperDomain
? (HELPERS[helperDomain].alias?.join(" ") ?? null)
: null,
},
};
})
.sort((a, b) => stringCompare(a[1], b[1], this.hass.locale.language));
.sort((a, b) => stringCompare(a.primary, b.primary, language));
}
);
private async _filterChanged(e: HASSDomTargetEvent<HaInputSearch>) {
this._filter = e.target.value;
}
// Only offer the categories that have helpers.
private _getSections = memoizeOne(
(
flowHelpers: string[],
localize: LocalizeFunc,
config: HassConfig,
language: string
) => {
const items = this._getHelperItems(
flowHelpers,
localize,
config,
language
);
return HELPER_CATEGORIES.filter((category) =>
items.some((item) => item.category === category)
).map((category) => ({
id: category,
label: localize(
`ui.panel.config.helpers.dialog.categories.${category}`
),
}));
}
);
private _fuseIndex = memoizeOne((items: HelperPickerItem[]) =>
Fuse.createIndex(HELPER_SEARCH_KEYS, items)
);
private _getItems = (
search?: string,
section?: string
): (HelperPickerItem | string)[] => {
this._category = section as HelperCategory | undefined;
this._search = search;
const allItems = this._getHelperItems(
this._helperFlows ?? [],
this.hass.localize,
this.hass.config,
this.hass.locale.language
);
const items = search
? multiTermSortedSearch(
allItems,
search,
(item) => item.id,
this._fuseIndex(allItems)
)
: allItems;
if (this._category) {
return items.filter((item) => item.category === this._category);
}
// Group by category, keeping the search order within each one.
return HELPER_CATEGORIES.flatMap((category) => {
const categoryItems = items.filter((item) => item.category === category);
return categoryItems.length
? [
this.hass.localize(
`ui.panel.config.helpers.dialog.categories.${category}`
),
...categoryItems,
]
: [];
});
};
private _renderHelperRow = (item: PickerComboBoxItem) => {
const { id: domain, primary, secondary, loaded } = item as HelperPickerItem;
return html`
<ha-combo-box-item>
<ha-domain-icon
slot="start"
style="margin: var(--ha-space-1);"
.domain=${domain}
brand-fallback
></ha-domain-icon>
<span slot="headline">${primary}</span>
${
secondary
? html`<span slot="supporting-text">${secondary}</span>`
: nothing
}
${
loaded
? html`<ha-icon-next slot="end"></ha-icon-next>`
: html`<ha-svg-icon
slot="end"
.id="icon-${domain}"
.path=${mdiAlertOutline}
@click=${stopPropagation}
></ha-svg-icon>
<ha-tooltip slot="end" .for="icon-${domain}">
${this.hass.localize(
"ui.dialogs.helper_settings.platform_not_loaded",
{ platform: domain }
)}
</ha-tooltip>`
}
</ha-combo-box-item>
`;
};
private _valueChanged(ev: CustomEvent): void {
this._item = ev.detail.value;
@@ -498,18 +607,12 @@ export class DialogHelperDetail extends DirtyStateProviderMixin<
this._updateDirtyState({ item: this._item, area: this._area });
}
private async _domainPicked(
ev: HASSDomEvent<number> & HASSDomCurrentTargetEvent<HaListSelectable>
private async _helperPicked(
ev: HASSDomEvent<PickerComboBoxIndexSelectedDetail>
): Promise<void> {
const domain = (
ev.currentTarget.items[ev.detail] as HaListItemOption | undefined
)?.value;
if (!domain) {
return;
}
const isLoaded =
!(domain in HELPERS) || isComponentLoaded(this.hass.config, domain);
if (!isLoaded) {
ev.stopPropagation();
const { id: domain, loaded } = ev.detail.item as HelperPickerItem;
if (!loaded) {
showAlertDialog(this, {
text: this.hass.localize(
"ui.dialogs.helper_settings.platform_not_loaded",
@@ -543,43 +646,35 @@ export class DialogHelperDetail extends DirtyStateProviderMixin<
}
private async _goBack() {
const search = this._search;
this._domain = undefined;
this._item = undefined;
this._area = undefined;
this._error = undefined;
await this.updateComplete;
await this._focusSearchInput();
await this._comboBox?.updateComplete;
if (search) {
this._comboBox?.setFieldValue(search);
}
this._focusPicker();
}
private async _focusSearchInput() {
const searchInput = this._searchInput;
if (!searchInput) {
return;
}
await searchInput.updateComplete;
searchInput.focus();
private async _focusPicker() {
await this._comboBox?.updateComplete;
this._comboBox?.focus();
}
static get styles(): CSSResultGroup {
return [
haStyleScrollbar,
haStyleDialog,
css`
ha-dialog {
--dialog-content-padding: 0;
}
ha-icon-next {
width: var(--ha-space-6);
}
ha-list-item-option img {
width: 24px;
height: 24px;
object-fit: contain;
}
ha-tooltip {
pointer-events: auto;
@media all and (min-width: 451px) and (min-height: 501px) {
ha-dialog.picker {
--ha-dialog-min-height: min(620px, 90vh);
}
}
.form {
padding: var(--ha-space-6);
@@ -588,23 +683,6 @@ export class DialogHelperDetail extends DirtyStateProviderMixin<
display: block;
margin-top: var(--ha-space-4);
}
ha-input-search {
margin: 0 var(--ha-space-4) var(--ha-space-3);
}
ha-list-selectable::part(base) {
height: calc(60vh - 184px);
}
@media all and (max-width: 450px), all and (max-height: 500px) {
ha-list-selectable::part(base) {
height: calc(
100vh -
184px - var(--safe-area-inset-top, 0px) - var(
--safe-area-inset-bottom,
0px
)
);
}
}
`,
];
}
@@ -123,7 +123,7 @@ import { haStyle } from "../../../resources/styles";
import type { HomeAssistant, Route } from "../../../types";
import { fileDownload } from "../../../util/file_download";
import { showAssignCategoryDialog } from "../category/show-dialog-assign-category";
import { renderCategoryGroupActions } from "../category/render-category-group-actions";
import { categoryGroupActions } from "../category/category-group-actions";
import { showCategoryRegistryDetailDialog } from "../category/show-dialog-category-registry-detail";
import {
getAreaTableColumn,
@@ -718,6 +718,12 @@ export class HaConfigHelpers extends SubscribeMixin(LitElement) {
.columns=${this._columns(this.hass.localize, helpers)}
.data=${helpers}
.initialGroupColumn=${this._tableSettings.grouping}
.renderGroupActions=${this._groupActions(
"helpers",
this._categories,
this._tableSettings.grouping,
this._categoryDeleted
)}
.initialCollapsedGroups=${this._activeCollapsed}
.initialSorting=${this._tableSettings.sorting}
.columnOrder=${this._activeColumnOrder}
@@ -880,12 +886,6 @@ export class HaConfigHelpers extends SubscribeMixin(LitElement) {
.hass=${this.hass}
slot="toolbar-icon"
></ha-integration-overflow-menu>
${renderCategoryGroupActions(
"helpers",
this._categories,
this._tableSettings.grouping,
this._categoryDeleted
)}
<ha-button slot="fab" size="l" @click=${this._createHelper}>
<ha-svg-icon slot="start" .path=${mdiPlus}></ha-svg-icon>
${this.hass.localize("ui.panel.config.helpers.picker.create_helper")}
@@ -931,6 +931,8 @@ export class HaConfigHelpers extends SubscribeMixin(LitElement) {
this._applyFilters();
}
private _groupActions = memoizeOne(categoryGroupActions);
private _categoryDeleted = (categoryId: string) => {
const value = this._filters["ha-filter-categories"];
@@ -109,7 +109,7 @@ import { documentationUrl } from "../../../util/documentation-url";
import { showToast } from "../../../util/toast";
import { showAreaRegistryDetailDialog } from "../areas/show-dialog-area-registry-detail";
import { showAssignCategoryDialog } from "../category/show-dialog-assign-category";
import { renderCategoryGroupActions } from "../category/render-category-group-actions";
import { categoryGroupActions } from "../category/category-group-actions";
import { showCategoryRegistryDetailDialog } from "../category/show-dialog-category-registry-detail";
import {
getAreaTableColumn,
@@ -535,6 +535,12 @@ class HaSceneDashboard extends SubscribeMixin(LitElement) {
.columns=${this._columns(this.hass.localize, scenes)}
id="entity_id"
.initialGroupColumn=${this._tableSettings.grouping}
.renderGroupActions=${this._groupActions(
"scene",
this._categories,
this._tableSettings.grouping,
this._categoryDeleted
)}
.initialCollapsedGroups=${this._activeCollapsed}
.initialSorting=${this._tableSettings.sorting}
.columnOrder=${this._activeColumnOrder}
@@ -563,12 +569,6 @@ class HaSceneDashboard extends SubscribeMixin(LitElement) {
.label=${this.hass.localize("ui.common.help")}
.path=${mdiHelpCircleOutline}
></ha-icon-button>
${renderCategoryGroupActions(
"scene",
this._categories,
this._tableSettings.grouping,
this._categoryDeleted
)}
<ha-filter-floor-areas
.type=${"scene"}
@@ -795,6 +795,8 @@ class HaSceneDashboard extends SubscribeMixin(LitElement) {
});
};
private _groupActions = memoizeOne(categoryGroupActions);
private _categoryDeleted = (categoryId: string) => {
const value = this._filters["ha-filter-categories"]?.value;
+9 -7
View File
@@ -113,7 +113,7 @@ import { showToast } from "../../../util/toast";
import { showAreaRegistryDetailDialog } from "../areas/show-dialog-area-registry-detail";
import { showNewAutomationDialog } from "../automation/show-dialog-new-automation";
import { showAssignCategoryDialog } from "../category/show-dialog-assign-category";
import { renderCategoryGroupActions } from "../category/render-category-group-actions";
import { categoryGroupActions } from "../category/category-group-actions";
import { showCategoryRegistryDetailDialog } from "../category/show-dialog-category-registry-detail";
import {
getAreaTableColumn,
@@ -453,6 +453,12 @@ class HaScriptPicker extends SubscribeMixin(LitElement) {
)}
has-filters
.initialGroupColumn=${this._tableSettings.grouping}
.renderGroupActions=${this._groupActions(
"script",
this._categories,
this._tableSettings.grouping,
this._categoryDeleted
)}
.initialCollapsedGroups=${this._activeCollapsed}
.initialSorting=${this._tableSettings.sorting}
.columnOrder=${this._activeColumnOrder}
@@ -497,12 +503,6 @@ class HaScriptPicker extends SubscribeMixin(LitElement) {
.path=${mdiHelpCircleOutline}
@click=${this._showHelp}
></ha-icon-button>
${renderCategoryGroupActions(
"script",
this._categories,
this._tableSettings.grouping,
this._categoryDeleted
)}
<ha-filter-floor-areas
.type=${"script"}
.value=${this._filters["ha-filter-floor-areas"]?.value}
@@ -739,6 +739,8 @@ class HaScriptPicker extends SubscribeMixin(LitElement) {
});
};
private _groupActions = memoizeOne(categoryGroupActions);
private _categoryDeleted = (categoryId: string) => {
const value = this._filters["ha-filter-categories"]?.value;
+5 -23
View File
@@ -42,9 +42,6 @@ import type { Action, ServiceAction } from "../../../../data/script";
import { migrateAutomationAction } from "../../../../data/script";
import {
callExecuteScript,
getServiceFieldDefaults,
getServiceFieldVisibility,
getServiceHiddenFields,
serviceCallWillDisconnect,
} from "../../../../data/service";
import { haStyle } from "../../../../resources/styles";
@@ -406,8 +403,6 @@ class HaPanelDevAction extends MatchMinHeightMixin(
serviceData: ServiceAction | undefined,
fields,
target,
visibility: ReturnType<typeof getServiceFieldVisibility>,
defaults: ReturnType<typeof getServiceFieldDefaults>,
yamlMode: boolean,
localize: LocalizeFunc
): string | undefined => {
@@ -426,9 +421,6 @@ class HaPanelDevAction extends MatchMinHeightMixin(
}
const dataIsTemplate =
typeof serviceData.data === "string" && isTemplate(serviceData.data);
const hidden = dataIsTemplate
? new Set<string>()
: getServiceHiddenFields(visibility, defaults, serviceData.data);
if (
target &&
!dataIsTemplate &&
@@ -445,7 +437,6 @@ class HaPanelDevAction extends MatchMinHeightMixin(
if (
field.required &&
!dataIsTemplate &&
!hidden.has(field.key) &&
(!serviceData.data || serviceData.data[field.key] === undefined)
) {
return localize(
@@ -461,22 +452,17 @@ class HaPanelDevAction extends MatchMinHeightMixin(
(
serviceDomains: HomeAssistant["services"],
domainService: string | undefined
): {
target: boolean;
fields: any[];
visibility: ReturnType<typeof getServiceFieldVisibility>;
defaults: ReturnType<typeof getServiceFieldDefaults>;
} => {
): { target: boolean; fields: any[] } => {
if (!domainService) {
return { target: false, fields: [], visibility: [], defaults: {} };
return { target: false, fields: [] };
}
const domain = computeDomain(domainService);
const service = computeObjectId(domainService);
if (!(domain in serviceDomains)) {
return { target: false, fields: [], visibility: [], defaults: {} };
return { target: false, fields: [] };
}
if (!(service in serviceDomains[domain])) {
return { target: false, fields: [], visibility: [], defaults: {} };
return { target: false, fields: [] };
}
const target = "target" in serviceDomains[domain][service];
const fields = serviceDomains[domain][service].fields;
@@ -502,8 +488,6 @@ class HaPanelDevAction extends MatchMinHeightMixin(
return {
target,
fields: result,
visibility: getServiceFieldVisibility(fields),
defaults: getServiceFieldDefaults(fields),
};
}
);
@@ -520,7 +504,7 @@ class HaPanelDevAction extends MatchMinHeightMixin(
return;
}
const { target, fields, visibility, defaults } = this._fields(
const { target, fields } = this._fields(
this.hass.services,
this._serviceData?.action
);
@@ -529,8 +513,6 @@ class HaPanelDevAction extends MatchMinHeightMixin(
this._serviceData,
fields,
target,
visibility,
defaults,
this._yamlMode,
this.hass.localize
);
@@ -34,6 +34,7 @@ export class EnergySetupWizard extends LitElement implements LovelaceCard {
energy_sources: [],
device_consumption: [],
device_consumption_water: [],
device_consumption_gas: [],
};
public getCardSize() {
+18 -2
View File
@@ -58,6 +58,9 @@ export const hasWaterSource = (prefs: EnergyPreferences): boolean =>
export const hasWaterDevices = (prefs: EnergyPreferences): boolean =>
(prefs.device_consumption_water?.length ?? 0) > 0;
export const hasGasDevices = (prefs: EnergyPreferences): boolean =>
(prefs.device_consumption_gas?.length ?? 0) > 0;
export const hasDeviceConsumption = (prefs: EnergyPreferences): boolean =>
prefs.device_consumption.length > 0;
@@ -78,6 +81,9 @@ export const hasPowerDevices = (prefs: EnergyPreferences): boolean =>
export const hasWaterRateDevices = (prefs: EnergyPreferences): boolean =>
(prefs.device_consumption_water ?? []).some((device) => device.stat_rate);
export const hasGasRateDevices = (prefs: EnergyPreferences): boolean =>
prefs.device_consumption_gas.some((device) => device.stat_rate);
/** A water source exposing a live flow-rate statistic. */
export const hasWaterRateSource = (prefs: EnergyPreferences): boolean =>
prefs.energy_sources.some(
@@ -96,7 +102,8 @@ export const hasNowViewContent = (prefs: EnergyPreferences): boolean =>
hasPowerDevices(prefs) ||
hasWaterRateSource(prefs) ||
hasWaterRateDevices(prefs) ||
hasGasRateSource(prefs);
hasGasRateSource(prefs) ||
hasGasRateDevices(prefs);
// --- Card catalog ----------------------------------------------------------
@@ -155,7 +162,7 @@ export const ENERGY_CARD_CATALOG: readonly EnergyCardCatalogEntry[] = [
"overview",
"energy-gas-graph",
"ui.panel.energy.cards.energy_gas_graph_title",
(p) => hasGasSource(p)
(p) => hasGasSource(p) || hasGasDevices(p)
),
// One toggle gates the water row, which renders energy-water-graph (sources)
// or, with only water devices, water-sankey.
@@ -253,6 +260,9 @@ export const ENERGY_CARD_CATALOG: readonly EnergyCardCatalogEntry[] = [
"ui.panel.energy.cards.energy_sources_table_title",
(p) => hasGasSource(p)
),
entry("gas", "gas-sankey", "ui.panel.energy.cards.gas_sankey_title", (p) =>
hasGasDevices(p)
),
// --- Water ---
entry(
@@ -293,6 +303,12 @@ export const ENERGY_CARD_CATALOG: readonly EnergyCardCatalogEntry[] = [
"ui.panel.energy.cards.water_flow_sankey_title",
(p) => hasWaterRateDevices(p)
),
entry(
"now",
"gas-flow-sankey",
"ui.panel.energy.cards.gas_flow_sankey_title",
(p) => hasGasRateDevices(p)
),
];
// --- Lookup helpers --------------------------------------------------------
@@ -17,6 +17,7 @@ import type { EnergyViewPath } from "./energy-cards";
import {
hasDeviceConsumption,
hasEnergySource,
hasGasDevices,
hasGasSource,
hasNowViewContent,
hasPowerSources,
@@ -91,7 +92,8 @@ export class EnergyDashboardStrategy extends ReactiveElement {
!prefs ||
(prefs.device_consumption.length === 0 &&
prefs.energy_sources.length === 0 &&
!hasWaterDevices(prefs))
!hasWaterDevices(prefs) &&
!hasGasDevices(prefs))
) {
await import("../cards/energy-setup-wizard-card");
return {
@@ -102,7 +104,7 @@ export class EnergyDashboardStrategy extends ReactiveElement {
const hasEnergy = hasEnergySource(prefs);
const hasPowerSource = hasPowerSources(prefs);
const hasWater = hasWaterSource(prefs) || hasWaterDevices(prefs);
const hasGas = hasGasSource(prefs);
const hasGas = hasGasSource(prefs) || hasGasDevices(prefs);
const hasDevices = hasDeviceConsumption(prefs);
const hidden = _config.hidden_cards;
@@ -8,7 +8,12 @@ import type { HomeAssistant } from "../../../types";
import type { LovelaceViewConfig } from "../../../data/lovelace/config/view";
import type { LovelaceStrategyDependency } from "../../lovelace/strategies/types";
import type { EnergyViewStrategyConfig } from "./energy-cards";
import { hasWaterSource, isEnergyCardVisible } from "./energy-cards";
import {
hasGasDevices,
hasGasSource,
hasWaterSource,
isEnergyCardVisible,
} from "./energy-cards";
@customElement("energy-overview-view-strategy")
export class EnergyOverviewViewStrategy extends ReactiveElement {
@@ -55,7 +60,8 @@ export class EnergyOverviewViewStrategy extends ReactiveElement {
if (
!prefs ||
(prefs.device_consumption.length === 0 &&
prefs.energy_sources.length === 0)
prefs.energy_sources.length === 0 &&
!hasGasDevices(prefs))
) {
return view;
}
@@ -142,14 +148,20 @@ export class EnergyOverviewViewStrategy extends ReactiveElement {
view.sections!.push({
type: "grid",
cards: [
{
title: hass.localize(
"ui.panel.energy.cards.energy_gas_graph_title"
),
type: "energy-gas-graph",
collection_key: collectionKey,
combine_sources: true,
},
hasGasSource(prefs)
? {
title: hass.localize(
"ui.panel.energy.cards.energy_gas_graph_title"
),
type: "energy-gas-graph",
collection_key: collectionKey,
combine_sources: true,
}
: {
title: hass.localize("ui.panel.energy.cards.gas_sankey_title"),
type: "gas-sankey",
collection_key: collectionKey,
},
],
});
}
@@ -8,9 +8,14 @@ import {
import type { HomeAssistant } from "../../../types";
import type { LovelaceViewConfig } from "../../../data/lovelace/config/view";
import type { EnergyViewStrategyConfig } from "./energy-cards";
import { hasGasSource, isEnergyCardVisible } from "./energy-cards";
import {
hasGasDevices,
hasGasSource,
isEnergyCardVisible,
} from "./energy-cards";
import type { LovelaceSectionConfig } from "../../../data/lovelace/config/section";
import type { LovelaceStrategyDependency } from "../../lovelace/strategies/types";
import { shouldShowFloorsAndAreas } from "./show-floors-and-areas";
@customElement("gas-view-strategy")
export class GasViewStrategy extends ReactiveElement {
@@ -52,8 +57,8 @@ export class GasViewStrategy extends ReactiveElement {
}
const prefs = energyCollection.prefs;
// No gas sources available
if (!prefs || !hasGasSource(prefs)) {
// No gas sources or devices available
if (!prefs || (!hasGasSource(prefs) && !hasGasDevices(prefs))) {
return view;
}
@@ -97,6 +102,24 @@ export class GasViewStrategy extends ReactiveElement {
});
}
if (isEnergyCardVisible("gas", "gas-sankey", prefs, hidden)) {
const showFloorsAndAreas = shouldShowFloorsAndAreas(
prefs.device_consumption_gas,
hass,
(d) => d.stat_consumption
);
section.cards!.push({
title: hass.localize("ui.panel.energy.cards.gas_sankey_title"),
type: "gas-sankey",
collection_key: collectionKey,
group_by_floor: showFloorsAndAreas,
group_by_area: showFloorsAndAreas,
grid_options: {
columns: 24,
},
});
}
return view;
}
}
@@ -151,6 +151,24 @@ export class PowerViewStrategy extends ReactiveElement {
});
}
if (isEnergyCardVisible("now", "gas-flow-sankey", prefs, hidden)) {
const showFloorsAndAreas = shouldShowFloorsAndAreas(
prefs.device_consumption_gas,
hass,
(d) => d.stat_rate
);
chartsSection.cards!.push({
title: hass.localize("ui.panel.energy.cards.gas_flow_sankey_title"),
type: "gas-flow-sankey",
collection_key: collectionKey,
group_by_floor: showFloorsAndAreas,
group_by_area: showFloorsAndAreas,
grid_options: {
columns: 36,
},
});
}
if (badges.length) {
view.badges = badges;
}
@@ -141,6 +141,36 @@ export const findDevicesOverCap = ({
return grouped;
};
/**
* Re-parent each device to its nearest ancestor that has data, so a device
* whose parent has no usable value is not orphaned or double-counted.
*/
export const resolveSankeyParents = (
devices: DeviceConsumptionEnergyPreference[],
isUsable: (device: DeviceConsumptionEnergyPreference) => boolean
): DeviceConsumptionEnergyPreference[] => {
const usable = new Set(
devices.filter(isUsable).map((d) => d.stat_consumption)
);
const parentByStat = new Map(
devices.map((d) => [d.stat_consumption, d.included_in_stat])
);
return devices.map((d) => {
const seen = new Set([d.stat_consumption]);
let parent = d.included_in_stat;
while (parent && !usable.has(parent) && !seen.has(parent)) {
seen.add(parent);
parent = parentByStat.get(parent);
}
if (parent && !usable.has(parent)) {
parent = undefined;
}
return parent === d.included_in_stat
? d
: { ...d, included_in_stat: parent };
});
};
export interface BuildSankeyDeviceNodesOptions {
devices: DeviceConsumptionEnergyPreference[];
computedStyle: CSSStyleDeclaration;
@@ -181,6 +181,7 @@ export class HuiDistributionCard extends LitElement implements LovelaceCard {
public setConfig(config: DistributionCardConfig): void {
this._config = config;
this._expandLegend = config.expand_legend ?? false;
// Handle empty entities gracefully
if (!config.entities || config.entities.length === 0) {
+12 -1
View File
@@ -220,6 +220,14 @@ export interface EnergyGasGraphCardConfig extends EnergyCardConfig {
combine_sources?: boolean;
}
export interface GasSankeyCardConfig extends EnergyCardSankeyConfig {
type: "gas-sankey";
}
export interface GasFlowSankeyCardConfig extends EnergyCardSankeyConfig {
type: "gas-flow-sankey";
}
export interface EnergyWaterGraphCardConfig extends EnergyCardConfig {
type: "energy-water-graph";
combine_sources?: boolean;
@@ -293,7 +301,9 @@ export type SankeyCardConfig =
| EnergySankeyCardConfig
| PowerSankeyCardConfig
| WaterSankeyCardConfig
| WaterFlowSankeyCardConfig;
| WaterFlowSankeyCardConfig
| GasSankeyCardConfig
| GasFlowSankeyCardConfig;
export interface EntityFilterCardConfig extends LovelaceCardConfig {
type: "entity-filter";
@@ -798,6 +808,7 @@ export interface DistributionCardConfig extends LovelaceCardConfig {
type: "distribution";
title?: string;
entities: (string | DistributionEntityConfig)[];
expand_legend?: boolean;
}
export interface DiscoveredDevicesCardConfig extends LovelaceCardConfig {
@@ -13,25 +13,32 @@ import {
registriesContext,
statesContext,
} from "../../../../data/context";
import type { EnergyData } from "../../../../data/energy";
import type { EnergyData, WaterGasResource } from "../../../../data/energy";
import {
computeEnergyDeviceLabels,
formatFlowRateShort,
getFlowRateFromState,
getFlowRateInUnit,
getFlowRateUnit,
getWaterGasDevices,
validateEnergyCollectionKey,
} from "../../../../data/energy";
import { EnergyCollectionController } from "../../../../data/energy-collection-controller";
import type {
HomeAssistant,
HomeAssistantConfig,
HomeAssistantFormatters,
HomeAssistantInternationalization,
HomeAssistantRegistries,
} from "../../../../types";
import type { LovelaceCard, LovelaceGridOptions } from "../../types";
import type { WaterFlowSankeyCardConfig } from "../types";
import type {
EnergyCardSankeyConfig,
GasFlowSankeyCardConfig,
WaterFlowSankeyCardConfig,
} from "../types";
import "../../../../components/chart/ha-sankey-chart";
import type { Link, Node } from "../../../../components/chart/ha-sankey-chart";
import { formatNumberWithUnit } from "../../../../common/number/format_number";
import { MobileAwareMixin } from "../../../../mixins/mobile-aware-mixin";
import {
buildSankeyDeviceNodes,
@@ -39,18 +46,21 @@ import {
DEFAULT_MAX_SANKEY_DEVICES,
fireSankeyNodeMoreInfo,
MIN_SANKEY_THRESHOLD_FACTOR,
resolveSankeyParents,
} from "../energy/common/sankey";
import type { LocalizeKeys } from "../../../../common/translations/localize";
const DEFAULT_CONFIG: Partial<WaterFlowSankeyCardConfig> = {
const DEFAULT_CONFIG = {
group_by_floor: true,
group_by_area: true,
};
@customElement("hui-water-flow-sankey-card")
class HuiWaterFlowSankeyCard
abstract class HuiWaterGasFlowSankeyCard
extends MobileAwareMixin(LitElement)
implements LovelaceCard
{
protected abstract readonly resource: WaterGasResource;
public static async getConfigElement() {
await import("../../editor/config-elements/hui-energy-sankey-card-editor");
return document.createElement("hui-energy-sankey-card-editor");
@@ -58,34 +68,24 @@ class HuiWaterFlowSankeyCard
@property({ attribute: false }) public layout?: string;
@state() private _config?: WaterFlowSankeyCardConfig;
public static getStubConfig(
_hass: HomeAssistant,
_entities: string[],
_entitiesFill: string[]
): WaterFlowSankeyCardConfig {
return {
type: "water-flow-sankey",
layout: "auto",
...DEFAULT_CONFIG,
};
}
@state() private _config?: EnergyCardSankeyConfig;
@state() private _data?: EnergyData;
@state()
@consume({ context: statesContext, subscribe: true })
@transform<HassEntities, HassEntities>({
transformer: function (this: HuiWaterFlowSankeyCard, states) {
transformer: function (this: HuiWaterGasFlowSankeyCard, states) {
const tracked: HassEntities = {};
const prefs = this._data?.prefs;
if (states && prefs) {
[
...prefs.energy_sources.map((source) =>
source.type === "water" ? source.stat_rate : undefined
source.type === this.resource ? source.stat_rate : undefined
),
...getWaterGasDevices(prefs, this.resource).map(
(device) => device.stat_rate
),
...prefs.device_consumption_water.map((device) => device.stat_rate),
].forEach((entityId) => {
if (entityId && states[entityId]) {
tracked[entityId] = states[entityId];
@@ -133,7 +133,7 @@ class HuiWaterFlowSankeyCard
});
}
public setConfig(config: WaterFlowSankeyCardConfig): void {
public setConfig(config: EnergyCardSankeyConfig): void {
if (config.collection_key) {
validateEnergyCollectionKey(config.collection_key);
}
@@ -153,6 +153,12 @@ class HuiWaterFlowSankeyCard
};
}
private _getRateUnit(): string | undefined {
return this.resource === "water" || !this._data
? undefined
: getFlowRateUnit(this.resource, this._data.prefs, this._states);
}
protected render() {
if (!this._config) {
return nothing;
@@ -162,18 +168,35 @@ class HuiWaterFlowSankeyCard
return html`${this._i18n.localize("ui.panel.lovelace.cards.energy.loading")}`;
}
const resource = this.resource;
const isWater = resource === "water";
const prefs = this._data.prefs;
// Gas shows the unit of its rate sensors (as the gas badge does); water keeps
// the fixed L/min or gal/min. Set at the start of every render.
const rateUnit = this._getRateUnit();
const computedStyle = getComputedStyle(this);
// Collect water sources with stat_rate
const waterSources = prefs.energy_sources.filter(
(source) => source.type === "water" && source.stat_rate
const prefDevices = getWaterGasDevices(prefs, resource);
// Collect sources with a live stat_rate
const sources = prefs.energy_sources.filter(
(source) =>
source.type === resource &&
source.stat_rate &&
getFlowRateFromState(this._states[source.stat_rate]) !== undefined
);
// Devices whose parent has no flow move up to the nearest ancestor with flow
const devices = resolveSankeyParents(
prefDevices,
(d) =>
!!d.stat_rate && this._getCurrentFlowRate(d.stat_rate, rateUnit) > 0
);
let totalInflow = 0;
waterSources.forEach((source) => {
if (source.type === "water" && source.stat_rate) {
const value = this._getCurrentFlowRate(source.stat_rate);
sources.forEach((source) => {
if (source.type === resource && source.stat_rate) {
const value = this._getCurrentFlowRate(source.stat_rate, rateUnit);
if (value > 0) totalInflow += value;
}
});
@@ -184,58 +207,62 @@ class HuiWaterFlowSankeyCard
// while its children have positive values. Skip sub-trackers so the
// total only reflects top-level devices and we don't double-count.
let totalDeviceFlow = 0;
if (waterSources.length === 0) {
prefs.device_consumption_water.forEach((device) => {
if (device.included_in_stat) {
return;
}
if (device.stat_rate) {
totalDeviceFlow += this._getCurrentFlowRate(device.stat_rate);
if (sources.length === 0) {
devices.forEach((d) => {
if (!d.included_in_stat && d.stat_rate) {
totalDeviceFlow += Math.max(
0,
this._getCurrentFlowRate(d.stat_rate, rateUnit)
);
}
});
}
const effectiveTotalInflow =
waterSources.length === 0 ? totalDeviceFlow : totalInflow;
sources.length === 0 ? totalDeviceFlow : totalInflow;
// Calculate dynamic threshold
const minFlowThreshold = effectiveTotalInflow * MIN_SANKEY_THRESHOLD_FACTOR;
// Fixed floor and ceil only suit integer L/min; gas values in m³/h or
// similar are often below 1
const untrackedFloor = isWater ? 1 : minFlowThreshold;
const nodes: Node[] = [];
const links: Link[] = [];
const waterColor = computedStyle
.getPropertyValue("--energy-water-color")
const resourceColor = computedStyle
.getPropertyValue(`--energy-${resource}-color`)
.trim();
const primaryColor = computedStyle
.getPropertyValue("--primary-color")
.trim();
const defaultSourceLabel = this._i18n.localize(
`ui.panel.lovelace.cards.energy.energy_distribution.${resource}` as LocalizeKeys
);
// Determine the "root" node for device links.
// - 0 sources: home node (value = sum of device values, computed later)
// - 1 source: that source node is the root (no home node)
// - >1 sources: home node aggregates all sources
// - combined sources: one summed source node is the root (no home node)
const combineSources =
waterSources.length > 1 &&
sources.length > 1 &&
(this._config.combine_sources ?? this._data.combineSources ?? false);
const showHomeNode = !combineSources && waterSources.length !== 1;
const showHomeNode = !combineSources && sources.length !== 1;
let rootNodeId: string;
if (showHomeNode) {
// Add source nodes and link to home
waterSources.forEach((source) => {
if (source.type !== "water" || !source.stat_rate) return;
const value = this._getCurrentFlowRate(source.stat_rate);
sources.forEach((source) => {
if (source.type !== resource || !source.stat_rate) return;
const value = this._getCurrentFlowRate(source.stat_rate, rateUnit);
if (value <= 0) return;
const sourceNodeId = `water_source_${source.stat_rate}`;
const sourceNodeId = `${resource}_source_${source.stat_rate}`;
nodes.push({
id: sourceNodeId,
label:
this._getEntityLabel(source.stat_rate) ||
this._i18n.localize(
"ui.panel.lovelace.cards.energy.energy_distribution.water"
),
label: this._getEntityLabel(source.stat_rate) || defaultSourceLabel,
value,
color: waterColor,
color: resourceColor,
index: 0,
entityId: source.stat_rate,
});
@@ -253,35 +280,27 @@ class HuiWaterFlowSankeyCard
rootNodeId = "home";
} else if (combineSources) {
nodes.push({
id: "water_source",
label: this._i18n.localize(
"ui.panel.lovelace.cards.energy.energy_distribution.water"
),
id: `${resource}_source`,
label: defaultSourceLabel,
value: totalInflow,
color: waterColor,
color: resourceColor,
index: 0,
});
rootNodeId = "water_source";
rootNodeId = `${resource}_source`;
} else {
// Single source: that source IS the root, no home node
const source = waterSources[0];
if (source.type === "water" && source.stat_rate) {
const value = this._getCurrentFlowRate(source.stat_rate);
const source = sources[0];
if (source.type === resource && source.stat_rate) {
const value = this._getCurrentFlowRate(source.stat_rate, rateUnit);
nodes.push({
id: source.stat_rate,
label:
this._getEntityLabel(source.stat_rate) ||
this._i18n.localize(
"ui.panel.lovelace.cards.energy.energy_distribution.water"
),
label: this._getEntityLabel(source.stat_rate) || defaultSourceLabel,
value: Math.max(0, value),
color: waterColor,
color: resourceColor,
index: 0,
entityId: source.stat_rate,
});
rootNodeId = source.stat_rate;
} else {
// Fallback (shouldn't happen)
rootNodeId = "home";
}
}
@@ -289,7 +308,7 @@ class HuiWaterFlowSankeyCard
const deviceLabels = computeEnergyDeviceLabels(
this._states,
this._formatEntityName,
prefs.device_consumption_water,
prefDevices,
this._data.statsMetadata,
"stat_rate"
);
@@ -300,17 +319,17 @@ class HuiWaterFlowSankeyCard
links: deviceLinks,
untrackedConsumption,
} = buildSankeyDeviceNodes({
devices: prefs.device_consumption_water,
devices,
computedStyle,
localize: this._i18n.localize,
rootNodeId,
minThreshold: minFlowThreshold,
maxDevices: this._config.max_devices ?? DEFAULT_MAX_SANKEY_DEVICES,
untrackedFloor: 1,
ceilOtherValue: true,
untrackedFloor,
ceilOtherValue: isWater,
initialUntracked: effectiveTotalInflow,
getId: (device) => device.stat_rate,
getValue: (id) => this._getCurrentFlowRate(id),
getValue: (id) => this._getCurrentFlowRate(id, rateUnit),
getLabel: (id) => deviceLabels[id] || this._getEntityLabel(id),
getEntityId: (id) => id,
});
@@ -328,7 +347,7 @@ class HuiWaterFlowSankeyCard
groupByFloor: !!group_by_floor,
groupByArea: !!group_by_area,
untrackedConsumption,
untrackedFloor: 1,
untrackedFloor,
});
nodes.push(...sankeyLayout.nodes);
links.push(...sankeyLayout.links);
@@ -366,19 +385,33 @@ class HuiWaterFlowSankeyCard
`;
}
private _valueFormatter = (value: number) =>
formatFlowRateShort(
this._i18n.locale,
this._hassConfig.unit_system.length,
value
);
private _valueFormatter = (value: number) => {
const rateUnit = this._getRateUnit();
return rateUnit
? formatNumberWithUnit(
value,
rateUnit,
this._i18n.locale,
value < 0.1 ? { maximumFractionDigits: 3 } : undefined
)
: formatFlowRateShort(
this._i18n.locale,
this._hassConfig.unit_system.length,
value
);
};
private _handleNodeClick(ev: CustomEvent<{ node: Node }>) {
fireSankeyNodeMoreInfo(this, ev.detail.node);
}
private _getCurrentFlowRate(entityId: string): number {
return getFlowRateFromState(this._states[entityId]) ?? 0;
private _getCurrentFlowRate(entityId: string, rateUnit?: string): number {
const stateObj = this._states[entityId];
return (
(rateUnit
? getFlowRateInUnit(stateObj, rateUnit)
: getFlowRateFromState(stateObj)) ?? 0
);
}
private _getEntityLabel(entityId: string): string {
@@ -408,8 +441,27 @@ class HuiWaterFlowSankeyCard
`;
}
@customElement("hui-water-flow-sankey-card")
class HuiWaterFlowSankeyCard extends HuiWaterGasFlowSankeyCard {
protected readonly resource = "water";
public static getStubConfig(): WaterFlowSankeyCardConfig {
return { type: "water-flow-sankey", layout: "auto", ...DEFAULT_CONFIG };
}
}
@customElement("hui-gas-flow-sankey-card")
class HuiGasFlowSankeyCard extends HuiWaterGasFlowSankeyCard {
protected readonly resource = "gas";
public static getStubConfig(): GasFlowSankeyCardConfig {
return { type: "gas-flow-sankey", layout: "auto", ...DEFAULT_CONFIG };
}
}
declare global {
interface HTMLElementTagNameMap {
"hui-water-flow-sankey-card": HuiWaterFlowSankeyCard;
"hui-gas-flow-sankey-card": HuiGasFlowSankeyCard;
}
}
@@ -13,9 +13,10 @@ import {
registriesContext,
statesContext,
} from "../../../../data/context";
import type { EnergyData } from "../../../../data/energy";
import type { EnergyData, WaterGasResource } from "../../../../data/energy";
import {
computeEnergyDeviceLabels,
getWaterGasDevices,
validateEnergyCollectionKey,
} from "../../../../data/energy";
import { EnergyCollectionController } from "../../../../data/energy-collection-controller";
@@ -25,14 +26,17 @@ import {
isExternalStatistic,
} from "../../../../data/recorder";
import type {
HomeAssistant,
HomeAssistantConfig,
HomeAssistantFormatters,
HomeAssistantInternationalization,
HomeAssistantRegistries,
} from "../../../../types";
import type { LovelaceCard, LovelaceGridOptions } from "../../types";
import type { WaterSankeyCardConfig } from "../types";
import type {
EnergyCardSankeyConfig,
GasSankeyCardConfig,
WaterSankeyCardConfig,
} from "../types";
import "../../../../components/chart/ha-sankey-chart";
import type { Link, Node } from "../../../../components/chart/ha-sankey-chart";
import { formatNumberWithUnit } from "../../../../common/number/format_number";
@@ -43,18 +47,21 @@ import {
DEFAULT_MAX_SANKEY_DEVICES,
fireSankeyNodeMoreInfo,
MIN_SANKEY_THRESHOLD_FACTOR,
resolveSankeyParents,
} from "../energy/common/sankey";
import type { LocalizeKeys } from "../../../../common/translations/localize";
const DEFAULT_CONFIG: Partial<WaterSankeyCardConfig> = {
const DEFAULT_CONFIG = {
group_by_floor: true,
group_by_area: true,
};
@customElement("hui-water-sankey-card")
class HuiWaterSankeyCard
abstract class HuiWaterGasSankeyCard
extends MobileAwareMixin(LitElement)
implements LovelaceCard
{
protected abstract readonly resource: WaterGasResource;
public static async getConfigElement() {
await import("../../editor/config-elements/hui-energy-sankey-card-editor");
return document.createElement("hui-energy-sankey-card-editor");
@@ -62,19 +69,7 @@ class HuiWaterSankeyCard
@property({ attribute: false }) public layout?: string;
@state() private _config?: WaterSankeyCardConfig;
public static getStubConfig(
_hass: HomeAssistant,
_entities: string[],
_entitiesFill: string[]
): WaterSankeyCardConfig {
return {
type: "water-sankey",
layout: "auto",
...DEFAULT_CONFIG,
};
}
@state() private _config?: EnergyCardSankeyConfig;
@state() private _data?: EnergyData;
@@ -117,7 +112,7 @@ class HuiWaterSankeyCard
});
}
public setConfig(config: WaterSankeyCardConfig): void {
public setConfig(config: EnergyCardSankeyConfig): void {
if (config.collection_key) {
validateEnergyCollectionKey(config.collection_key);
}
@@ -146,44 +141,46 @@ class HuiWaterSankeyCard
return html`${this._i18n.localize("ui.panel.lovelace.cards.energy.loading")}`;
}
const prefs = this._data.prefs;
const waterSources = prefs.energy_sources.filter(
(source) => source.type === "water"
const data = this._data;
const resource = this.resource;
const prefs = data.prefs;
const sources = prefs.energy_sources.filter(
(source) => source.type === resource
);
const prefDevices = getWaterGasDevices(prefs, resource);
const computedStyle = getComputedStyle(this);
const nodes: Node[] = [];
const links: Link[] = [];
const statValue = (statId: string) =>
statId in data.stats
? calculateStatisticSumGrowth(data.stats[statId]) || 0
: 0;
// Devices whose parent has no data move up to the nearest ancestor with data
const devices = resolveSankeyParents(
prefDevices,
(d) => statValue(d.stat_consumption) > 0
);
// Sum only top-level devices. Devices with `included_in_stat` are already
// counted inside their parent stat; adding them again would double-count
// and push the home total above the source meter.
const totalDownstreamConsumption = prefs.device_consumption_water.reduce(
(total, device) => {
if (device.included_in_stat) {
return total;
}
const value =
device.stat_consumption in this._data!.stats
? calculateStatisticSumGrowth(
this._data!.stats[device.stat_consumption]
) || 0
: 0;
return total + value;
},
const totalDownstreamConsumption = devices.reduce(
(total, d) =>
d.included_in_stat ? total : total + statValue(d.stat_consumption),
0
);
const totalSourceSupply = waterSources.reduce((total, source) => {
const value =
source.stat_energy_from in this._data!.stats
? calculateStatisticSumGrowth(
this._data!.stats[source.stat_energy_from]
) || 0
: 0;
return total + value;
}, 0);
const totalWaterConsumption = Math.max(
const totalSourceSupply = sources.reduce(
(total, source) =>
source.type === resource
? total + statValue(source.stat_energy_from)
: total,
0
);
const totalConsumption = Math.max(
totalDownstreamConsumption,
totalSourceSupply
);
@@ -192,47 +189,42 @@ class HuiWaterSankeyCard
const homeNode: Node = {
id: "home",
label: this._hassConfig.location_name,
value: Math.max(0, totalWaterConsumption),
value: Math.max(0, totalConsumption),
color: computedStyle.getPropertyValue("--primary-color").trim(),
index: 1,
};
nodes.push(homeNode);
const minWaterThreshold = homeNode.value * MIN_SANKEY_THRESHOLD_FACTOR;
const minThreshold = homeNode.value * MIN_SANKEY_THRESHOLD_FACTOR;
// Add water source nodes
const waterColor = computedStyle
.getPropertyValue("--energy-water-color")
// Add source nodes
const resourceColor = computedStyle
.getPropertyValue(`--energy-${resource}-color`)
.trim();
const combineSources =
waterSources.length > 1 &&
(this._config.combine_sources ?? this._data.combineSources ?? false);
sources.length > 1 &&
(this._config.combine_sources ?? data.combineSources ?? false);
if (combineSources && totalSourceSupply > 0) {
nodes.push({
id: "source-water",
id: `source-${resource}`,
label: this._i18n.localize(
"ui.panel.lovelace.cards.energy.energy_distribution.water"
`ui.panel.lovelace.cards.energy.energy_distribution.${resource}` as LocalizeKeys
),
value: totalSourceSupply,
color: waterColor,
color: resourceColor,
index: 0,
});
links.push({
source: "source-water",
source: `source-${resource}`,
target: "home",
value: totalSourceSupply,
});
}
waterSources.forEach((source) => {
if (combineSources || source.type !== "water") {
sources.forEach((source) => {
if (combineSources || source.type !== resource) {
return;
}
const value =
source.stat_energy_from in this._data!.stats
? calculateStatisticSumGrowth(
this._data!.stats[source.stat_energy_from]
) || 0
: 0;
const value = statValue(source.stat_energy_from);
if (value <= 0) {
return;
@@ -246,10 +238,10 @@ class HuiWaterSankeyCard
this._states,
this._formatEntityName,
source.stat_energy_from,
this._data!.statsMetadata[source.stat_energy_from]
data.statsMetadata[source.stat_energy_from]
),
value,
color: waterColor,
color: resourceColor,
index: 0,
});
@@ -260,16 +252,11 @@ class HuiWaterSankeyCard
});
});
const deviceValue = (statConsumption: string) =>
statConsumption in this._data!.stats
? calculateStatisticSumGrowth(this._data!.stats[statConsumption]) || 0
: 0;
const deviceLabels = computeEnergyDeviceLabels(
this._states,
this._formatEntityName,
prefs.device_consumption_water,
this._data!.statsMetadata
prefDevices,
data.statsMetadata
);
const deviceLabel = (statConsumption: string) =>
@@ -278,7 +265,7 @@ class HuiWaterSankeyCard
this._states,
this._formatEntityName,
statConsumption,
this._data!.statsMetadata[statConsumption]
data.statsMetadata[statConsumption]
);
const {
@@ -287,17 +274,17 @@ class HuiWaterSankeyCard
links: deviceLinks,
untrackedConsumption,
} = buildSankeyDeviceNodes({
devices: prefs.device_consumption_water,
devices,
computedStyle,
localize: this._i18n.localize,
rootNodeId: "home",
minThreshold: minWaterThreshold,
minThreshold,
maxDevices: this._config.max_devices ?? DEFAULT_MAX_SANKEY_DEVICES,
untrackedFloor: 0,
ceilOtherValue: false,
initialUntracked: homeNode.value,
getId: (device) => device.stat_consumption,
getValue: deviceValue,
getValue: statValue,
getLabel: deviceLabel,
getEntityId: (id) => (isExternalStatistic(id) ? undefined : id),
});
@@ -359,7 +346,7 @@ class HuiWaterSankeyCard
value < 0.1 ? { maximumFractionDigits: 3 } : undefined;
return formatNumberWithUnit(
value,
this._data!.waterUnit,
this.resource === "gas" ? this._data!.gasUnit : this._data!.waterUnit,
this._i18n.locale,
formatOptions
);
@@ -390,8 +377,27 @@ class HuiWaterSankeyCard
`;
}
@customElement("hui-water-sankey-card")
class HuiWaterSankeyCard extends HuiWaterGasSankeyCard {
protected readonly resource = "water";
public static getStubConfig(): WaterSankeyCardConfig {
return { type: "water-sankey", layout: "auto", ...DEFAULT_CONFIG };
}
}
@customElement("hui-gas-sankey-card")
class HuiGasSankeyCard extends HuiWaterGasSankeyCard {
protected readonly resource = "gas";
public static getStubConfig(): GasSankeyCardConfig {
return { type: "gas-sankey", layout: "auto", ...DEFAULT_CONFIG };
}
}
declare global {
interface HTMLElementTagNameMap {
"hui-water-sankey-card": HuiWaterSankeyCard;
"hui-gas-sankey-card": HuiGasSankeyCard;
}
}
@@ -51,6 +51,9 @@ const LAZY_LOAD_TYPES = {
"energy-distribution": () =>
import("../cards/energy/hui-energy-distribution-card"),
"energy-gas-graph": () => import("../cards/energy/hui-energy-gas-graph-card"),
"gas-sankey": () => import("../cards/water-gas/hui-water-gas-sankey-card"),
"gas-flow-sankey": () =>
import("../cards/water-gas/hui-water-gas-flow-sankey-card"),
"energy-water-graph": () =>
import("../cards/energy/hui-energy-water-graph-card"),
"energy-grid-neutrality-gauge": () =>
@@ -68,9 +71,9 @@ const LAZY_LOAD_TYPES = {
"energy-usage-graph": () =>
import("../cards/energy/hui-energy-usage-graph-card"),
"energy-sankey": () => import("../cards/energy/hui-energy-sankey-card"),
"water-sankey": () => import("../cards/water/hui-water-sankey-card"),
"water-sankey": () => import("../cards/water-gas/hui-water-gas-sankey-card"),
"water-flow-sankey": () =>
import("../cards/water/hui-water-flow-sankey-card"),
import("../cards/water-gas/hui-water-gas-flow-sankey-card"),
"power-sources-graph": () =>
import("../cards/energy/hui-power-sources-graph-card"),
"power-sankey": () => import("../cards/energy/hui-power-sankey-card"),
@@ -5,6 +5,7 @@ import {
array,
assert,
assign,
boolean,
object,
optional,
string,
@@ -38,6 +39,7 @@ const cardConfigStruct = assign(
object({
title: optional(string()),
entities: array(union([string(), distributionEntityConfigStruct])),
expand_legend: optional(boolean()),
})
);
@@ -58,7 +60,10 @@ const SUB_FORM = {
] as const,
};
const SCHEMA = [{ name: "title", selector: { text: {} } }] as const;
const SCHEMA = [
{ name: "title", selector: { text: {} } },
{ name: "expand_legend", required: false, selector: { boolean: {} } },
] as const;
@customElement("hui-distribution-card-editor")
export class HuiDistributionCardEditor
@@ -207,6 +212,10 @@ export class HuiDistributionCardEditor
)} (${this.hass!.localize(
"ui.panel.lovelace.editor.card.config.optional"
)})`;
case "expand_legend":
return this.hass!.localize(
"ui.panel.lovelace.editor.card.generic.expand_legend"
);
default:
return "";
}
@@ -27,8 +27,10 @@ const cardConfigStruct = assign(
type: union([
literal("power-sankey"),
literal("water-sankey"),
literal("gas-sankey"),
literal("energy-sankey"),
literal("water-flow-sankey"),
literal("gas-flow-sankey"),
]),
title: optional(string()),
collection_key: optional(string()),
@@ -28,6 +28,8 @@ const NON_STANDARD_CARD_URLS = {
"power-sankey": "energy/#power-flow-sankey-graph",
"water-sankey": "energy/#water-sankey-graph",
"water-flow-sankey": "energy/#water-flow-sankey-graph",
"gas-sankey": "energy/#gas-sankey-graph",
"gas-flow-sankey": "energy/#gas-flow-sankey-graph",
"power-sources-graph": "energy/#power-sources-graph",
};
@@ -208,6 +208,14 @@ export const energyCards: Card[] = [
type: "water-flow-sankey",
showElement: false,
},
{
type: "gas-sankey",
showElement: false,
},
{
type: "gas-flow-sankey",
showElement: false,
},
{
type: "energy-solar-consumed-gauge",
showElement: false,
@@ -35,9 +35,9 @@ import {
sortedMarketplaceFilterQueryString,
} from "../../../common/url/marketplace-query-params";
import { shallowEqual } from "../../../common/util/shallow-equal";
import { groupActionsSlot } from "../../../components/data-table/ha-data-table";
import type {
DataTableColumnContainer,
DataTableGroupActions,
SelectionChangedEvent,
} from "../../../components/data-table/ha-data-table";
import "../../../layouts/hass-tabs-subpage";
@@ -400,6 +400,7 @@ export class HaMarketplaceDashboard extends LitElement {
this._tableSettings.grouping,
this._i18n.localize
)}
.renderGroupActions=${this._renderGroupActions}
.columnOrder=${this._orderTableColumns}
.hiddenColumns=${this._hiddenTableColumns}
@columns-changed=${this._handleColumnsChanged}
@@ -414,14 +415,6 @@ export class HaMarketplaceDashboard extends LitElement {
@sorting-changed=${this._tableSettings.sortingChanged}
>
${this._renderToolbar()}
<ha-icon-button
slot=${groupActionsSlot(
this._i18n.localize("ui.panel.marketplace.repository_status.new")
)}
.label=${this._i18n.localize("ui.panel.marketplace.menu.dismiss")}
.path=${mdiCheckAll}
@click=${this._dismissNew}
></ha-icon-button>
${
this.tab === "installed"
? html`<ha-assist-chip
@@ -984,7 +977,16 @@ export class HaMarketplaceDashboard extends LitElement {
showMarketplaceAddFromLink(this, this._i18n.localize, this.marketplace);
}
private _dismissNew() {
private _renderGroupActions: DataTableGroupActions = (group) =>
group === this._i18n.localize("ui.panel.marketplace.repository_status.new")
? html`<ha-icon-button
.label=${this._i18n.localize("ui.panel.marketplace.menu.dismiss")}
.path=${mdiCheckAll}
@click=${this._dismissNew}
></ha-icon-button>`
: nothing;
private _dismissNew = () => {
showConfirmationDialog(this, {
title: this._i18n.localize(
"ui.panel.marketplace.dialog.dismiss_new.title"
@@ -1021,7 +1023,7 @@ export class HaMarketplaceDashboard extends LitElement {
}
},
});
}
};
private _statusStates = memoize(
(localize: LocalizeFunc, tab: MarketplaceTab) =>
+4
View File
@@ -211,6 +211,10 @@ export const getMyRedirects = (): Redirects => ({
tools_states: {
redirect: "/config/tools/state",
},
tools_action_history: {
component: "logbook",
redirect: "/config/tools/action-history",
},
tools_actions: {
redirect: "/config/tools/action",
},
@@ -2,12 +2,12 @@ import { mdiDelete } from "@mdi/js";
import type { CSSResultGroup, TemplateResult } from "lit";
import { css, html, LitElement, nothing } from "lit";
import { customElement, property } from "lit/decorators";
import "@home-assistant/components/card";
import memoizeOne from "memoize-one";
import { relativeTime } from "../../common/datetime/relative_time";
import { fireEvent } from "../../common/dom/fire_event";
import type { HASSDomCurrentTargetEvent } from "../../common/dom/fire_event";
import "../../components/ha-button";
import "../../components/ha-card";
import "../../components/ha-icon-button";
import type { HaIconButton } from "../../components/ha-icon-button";
import "../../components/ha-settings-row";
@@ -37,83 +37,79 @@ class HaLongLivedTokens extends LitElement {
const accessTokens = this._accessTokens(this.refreshTokens);
return html`
<ha-card
.header=${this.hass.localize(
"ui.panel.profile.long_lived_access_tokens.header"
)}
>
<div class="card-content">
<hac-card>
<h2 slot="header" class="header">
${this.hass.localize(
"ui.panel.profile.long_lived_access_tokens.description"
"ui.panel.profile.long_lived_access_tokens.header"
)}
</h2>
${this.hass.localize(
"ui.panel.profile.long_lived_access_tokens.description"
)}
<a
href="https://contributors.home-assistant.io/core/internals/auth-api/#make-authenticated-requests"
target="_blank"
rel="noreferrer"
>
${this.hass.localize(
"ui.panel.profile.long_lived_access_tokens.learn_auth_requests"
)}
</a>
${
!accessTokens.length
? html`<p>
${this.hass.localize(
"ui.panel.profile.long_lived_access_tokens.empty_state"
)}
</p>`
: accessTokens.map(
(token) =>
html`<ha-settings-row two-line>
<span slot="heading">${token.client_name}</span>
<div slot="description">
${this.hass.localize(
"ui.panel.profile.long_lived_access_tokens.created",
{
date: relativeTime(
new Date(token.created_at),
this.hass.locale
),
}
)}
</div>
${
this._hasBeenUsed(token)
? html`<div slot="description">
${this.hass.localize(
"ui.panel.profile.long_lived_access_tokens.last_used",
{
date: relativeTime(
new Date(token.last_used_at),
this.hass.locale
),
location: token.last_used_ip,
}
)}
</div>`
: nothing
}
<ha-icon-button
.token=${token}
.disabled=${token.is_current}
.label=${this.hass.localize("ui.common.delete")}
.path=${mdiDelete}
@click=${this._deleteToken}
></ha-icon-button>
</ha-settings-row>`
)
}
</div>
<div class="card-actions">
<ha-button @click=${this._createToken}>
${this.hass.localize(
"ui.panel.profile.long_lived_access_tokens.create"
)}
</ha-button>
</div>
</ha-card>
<a
href="https://contributors.home-assistant.io/core/internals/auth-api/#make-authenticated-requests"
target="_blank"
rel="noreferrer"
>
${this.hass.localize(
"ui.panel.profile.long_lived_access_tokens.learn_auth_requests"
)}
</a>
${
!accessTokens.length
? html`<p>
${this.hass.localize(
"ui.panel.profile.long_lived_access_tokens.empty_state"
)}
</p>`
: accessTokens.map(
(token) =>
html`<ha-settings-row two-line>
<span slot="heading">${token.client_name}</span>
<div slot="description">
${this.hass.localize(
"ui.panel.profile.long_lived_access_tokens.created",
{
date: relativeTime(
new Date(token.created_at),
this.hass.locale
),
}
)}
</div>
${
this._hasBeenUsed(token)
? html`<div slot="description">
${this.hass.localize(
"ui.panel.profile.long_lived_access_tokens.last_used",
{
date: relativeTime(
new Date(token.last_used_at),
this.hass.locale
),
location: token.last_used_ip,
}
)}
</div>`
: nothing
}
<ha-icon-button
.token=${token}
.disabled=${token.is_current}
.label=${this.hass.localize("ui.common.delete")}
.path=${mdiDelete}
@click=${this._deleteToken}
></ha-icon-button>
</ha-settings-row>`
)
}
<ha-button slot="footer" @click=${this._createToken}>
${this.hass.localize(
"ui.panel.profile.long_lived_access_tokens.create"
)}
</ha-button>
</hac-card>
`;
}
@@ -174,6 +170,9 @@ class HaLongLivedTokens extends LitElement {
return [
haStyle,
css`
hac-card::part(footer) {
justify-content: flex-end;
}
ha-settings-row {
padding: 0;
}
@@ -183,9 +182,18 @@ class HaLongLivedTokens extends LitElement {
ha-icon-button {
color: var(--primary-text-color);
}
.card-actions {
display: flex;
justify-content: flex-end;
.header {
margin: 0;
padding: var(--ha-space-5) var(--ha-space-4) var(--ha-space-4);
color: var(--ha-card-header-color, var(--primary-text-color));
font-family: var(--ha-card-header-font-family, inherit);
font-size: var(--ha-card-header-font-size, var(--ha-font-size-2xl));
font-weight: var(--ha-font-weight-normal);
line-height: var(--ha-line-height-condensed);
letter-spacing: -0.012em;
/* haStyle truncates h2 to one line, long titles should wrap */
white-space: normal;
overflow: visible;
}
`,
];
+82 -16
View File
@@ -2347,7 +2347,7 @@
"aliases_description": "Aliases are the names voice assistants use for this entity.",
"aliases_description_assist": "Assist uses all aliases equally.",
"aliases_description_alexa": "Amazon Alexa uses only the first alias.",
"aliases_description_google": "Google Assistant uses the first alias as the main name and the rest as alternatives.",
"aliases_description_google": "Google Home uses the first alias as the main name and the rest as alternatives.",
"aliases_no_unique_id": "Aliases are not supported for entities without a unique ID. See the {faq_link} for more detail.",
"entity_name_alias_description": "Default name. When enabled, it is used as the first alias.",
"ask_pin": "Ask for PIN",
@@ -4879,6 +4879,26 @@
"flow_rate_no_volume": "[%key:ui::panel::config::energy::water::dialog::flow_rate_no_volume%]"
}
},
"device_consumption_gas": {
"title": "Individual gas devices",
"sub": "Tracking the gas usage of individual devices allows Home Assistant to break down your gas usage by device.",
"learn_more": "More information on how to get started",
"devices": "Devices",
"add_device": "Add device",
"dialog": {
"header": "Add a gas device",
"display_name": "Display name",
"device_consumption_gas": "Device gas consumption",
"device_consumption_gas_flow_rate": "Device gas flow rate",
"selected_stat_intro": "Select the gas sensor that measures the device's gas consumption in either of {unit}.",
"included_in_device": "Upstream device",
"included_in_device_helper": "If this device is already counted by another device (such as a gas meter measured by the main gas supply), selecting the upstream device prevents duplicate gas tracking.",
"no_upstream_devices": "No eligible upstream devices",
"gas_sensor_needed": "To track this device, you need a gas sensor.",
"flow_rate_no_volume": "[%key:ui::panel::config::energy::gas::dialog::flow_rate_no_volume%]",
"flow_rate_energy_sources": "[%key:ui::panel::config::energy::gas::dialog::flow_rate_energy_sources%]"
}
},
"integral": {
"energy": {
"create_title": "{count, plural,\n one {Create energy sensor?}\n other {Create energy sensors?}\n}",
@@ -4941,7 +4961,42 @@
"dialog": {
"create": "Create",
"create_helper": "Create helper",
"create_platform": "Create {platform}"
"create_platform": "Create {platform}",
"categories": {
"input": "Inputs",
"time": "Time",
"calculation": "Calculations",
"other": "Other"
},
"descriptions": {
"input_boolean": "Switches between on and off",
"input_button": "Starts automations when pressed",
"input_text": "Stores a text value you can change",
"input_number": "Stores a number you can set with a slider or input field",
"input_datetime": "Stores a date, a time, or both",
"input_select": "Lets you choose from a list of options",
"counter": "Counts up or down in steps",
"schedule": "Turns on during the time blocks you set for each day",
"timer": "Counts down from a set duration",
"tod": "Turns on between two times of the day",
"history_stats": "Measures how long or how often an entity was in a state over a period",
"derivative": "Shows how fast a sensor's value changes",
"filter": "Smooths out or cleans up the values of a sensor",
"integration": "Adds up a sensor's value over time, like power into energy",
"min_max": "Combines sensors into their minimum, maximum, mean, median, or sum",
"mold_indicator": "Estimates the risk of mold from temperature and humidity",
"random": "Creates random values",
"statistics": "Calculates statistics, like the average, from a sensor's history",
"threshold": "Turns on when a sensor's value is above or below a limit",
"trend": "Turns on when a sensor's value is rising or falling",
"template": "Creates an entity whose state comes from a template",
"utility_meter": "Tracks consumption, like energy or water, per billing period",
"generic_hygrostat": "Controls humidity using a switch or fan and a humidity sensor",
"generic_thermostat": "Controls temperature using a switch or fan and a temperature sensor",
"group": "Combines entities of the same type into one",
"otp": "Creates one-time passwords for two-factor authentication",
"switch_as_x": "Shows a switch as a light, fan, cover, or another entity type"
}
}
},
"core": {
@@ -5400,8 +5455,8 @@
"title": "Get the best assistant with {home_assistant_cloud}",
"features": {
"assistants": {
"title": "Google Assistant and Amazon Alexa",
"text": "Control all your Home Assistant devices via any Google Assistant-enabled or Alexa-enabled device."
"title": "Google Home and Amazon Alexa",
"text": "Control all your Home Assistant devices via any Google Home-enabled or Alexa-enabled device."
},
"speech": {
"title": "Amazing speech options for Assist",
@@ -7008,7 +7063,7 @@
"cloud": {
"description_login": "Logged in and connected",
"description_not_login": "Not logged in",
"description_features": "Control home when away and integrate with Alexa and Google Assistant",
"description_features": "Control home when away and integrate with Alexa and Google Home",
"login": {
"title": "Cloud login",
"landing_title": "[%key:ui::panel::config::cloud::account::onboarding::ha_cloud_title%]",
@@ -7112,7 +7167,7 @@
"alexa_google_both": "Alexa and Google linked",
"alexa_google_alexa": "Alexa linked",
"alexa_google_google": "Google linked",
"alexa_google_none": "Control your home with Amazon Alexa and Google Assistant",
"alexa_google_none": "Control your home with Amazon Alexa and Google Home",
"feature_cameras": "Cameras and streaming",
"feature_webhooks": "Webhooks",
"webhooks_active": "{count} active",
@@ -7149,7 +7204,7 @@
"backup_body": "Keep an encrypted off-site copy of your latest backup, ready to restore on first boot.",
"voice_title": "Voice assistants",
"voice_helper": "Alexa, Google, and Home Assistant voice",
"voice_body": "Talk to your home your way. Home Assistant Cloud powers fast, private voice for Assist out of the box, and you can add Amazon Alexa or Google Assistant.",
"voice_body": "Talk to your home your way. Home Assistant Cloud powers fast, private voice for Assist out of the box, and you can add Amazon Alexa or Google Home.",
"streaming_title": "Smoother streaming",
"streaming_helper": "Faster, more reliable camera feeds",
"streaming_body": "Make camera feeds and Music Assistant playback faster and more reliable, over a secure connection.",
@@ -7164,7 +7219,7 @@
"alexa_tagline": "Voice control with Echo devices",
"alexa_bullet1": "Control devices from your Echo by voice",
"alexa_bullet2": "Use Home Assistant in your Alexa routines",
"google_name": "Google Assistant",
"google_name": "Google Home",
"google_tagline": "Hands-free control with Nest devices",
"google_bullet1": "Control devices from your Nest speakers by voice",
"google_bullet2": "Cast your cameras to a Nest Hub display"
@@ -7209,7 +7264,7 @@
"support_package_privacy_warning": "This file may contain personal data about your home. Avoid sharing them with unverified or untrusted parties.",
"reset_cloud_data": "Reset cloud data",
"reset_data_confirm_title": "Reset cloud data?",
"reset_data_confirm_text": "This will reset all your cloud settings. This includes your remote connection, Google Assistant, and Amazon Alexa integrations. This action cannot be undone.",
"reset_data_confirm_text": "This will reset all your cloud settings. This includes your remote connection, Google Home, and Amazon Alexa integrations. This action cannot be undone.",
"reset": "Reset",
"reset_data_failed": "Failed to reset cloud data",
"thank_you_note": "It's because of people like you that we are able to make a great home automation experience for everyone.",
@@ -7222,7 +7277,7 @@
"integrations_introduction": "Integrations for Home Assistant Cloud allow you to connect with services in the cloud without having to expose your Home Assistant instance publicly on the internet.",
"integrations_introduction2": "Check the website for ",
"integrations_link_all_features": " all available features",
"tip_moved_voice_assistants": "Looking for Google Assistant and Amazon Alexa settings? They are moved to the new voice assistants page.",
"tip_moved_voice_assistants": "Looking for Google Home and Amazon Alexa settings? They are moved to the new voice assistants page.",
"connected": "Connected",
"connecting": "Connecting…",
"not_connected": "Not connected",
@@ -7309,26 +7364,26 @@
"expose_new_entities_info": "Should new entities be exposed to Alexa automatically? Exposes supported devices that are not classified as security devices."
},
"google": {
"title": "Google Assistant",
"info": "With the Google Assistant integration for Home Assistant Cloud, you'll be able to control all your Home Assistant devices via any Google Assistant-enabled device.",
"title": "Google Home",
"info": "With the Google Home integration for Home Assistant Cloud, you'll be able to control all your Home Assistant devices via any Google Home-enabled device.",
"http_use_ssl_warning_title": "Local communication unavailable",
"http_use_ssl_warning_text": "Google devices will not be able to talk locally with Home Assistant, because you have configured an SSL certificate for your HTTP integration.",
"config_documentation": "Configuration documentation",
"enable_state_reporting": "Enable state reporting",
"info_state_reporting": "If you enable state reporting, Home Assistant will send all state changes of exposed entities to Google. This speeds up voice commands and allows you to always see the latest states in the Google app.",
"security_devices": "Security devices",
"enter_pin_info": "Please enter a PIN to interact with security devices. Security devices are doors, garage doors, and locks. You will be asked to say/enter this PIN when interacting with security devices via Google Assistant.",
"enter_pin_info": "Please enter a PIN to interact with security devices. Security devices are doors, garage doors, and locks. You will be asked to say/enter this PIN when interacting with security devices via Google Home.",
"devices_pin": "Security devices PIN",
"enter_pin_hint": "Enter a PIN to use security devices",
"show_entities": "Show entities",
"exposed_entities": "{number} {number, plural,\n one {entity}\n other {entities}\n} exposed",
"manual_config": "Editing which entities are exposed via the UI is disabled because you have configured entity filters in configuration.yaml.",
"enter_pin_error": "Unable to store PIN:",
"not_configured_title": "Continue setting up Google Assistant",
"not_configured_text": "To start using Google Assistant, add the 'Home Assistant Cloud by Nabu Casa' service in the Google Home app on your smartphone.",
"not_configured_title": "Continue setting up Google Home",
"not_configured_text": "To start using Google Home, add the 'Home Assistant Cloud by Nabu Casa' service in the Google Home app on your smartphone.",
"link_learn_how_it_works": "[%key:ui::panel::config::cloud::account::remote::link_learn_how_it_works%]",
"expose_new_entities": "Expose new entities",
"expose_new_entities_info": "Should new entities be exposed to Google Assistant automatically? Exposes supported devices that are not classified as security devices."
"expose_new_entities_info": "Should new entities be exposed to Google Home automatically? Exposes supported devices that are not classified as security devices."
},
"webhooks": {
"title": "Webhooks",
@@ -7523,6 +7578,7 @@
"integration": "Integration",
"parent_device": "Parent device",
"part_of_device": "Part of {name}",
"connected_via": "[%key:ui::panel::config::integrations::config_entry::via%]",
"firmware_version": "Firmware",
"battery": "Battery",
"disabled_by": "Disabled",
@@ -11148,6 +11204,14 @@
"name": "Water flow Sankey graph",
"description": "This card shows the real-time flow of water in your home."
},
"gas-sankey": {
"name": "Gas Sankey graph",
"description": "This card shows the distribution of gas consumption in your home on a Sankey graph."
},
"gas-flow-sankey": {
"name": "Gas flow Sankey graph",
"description": "This card shows the real-time flow of gas in your home."
},
"energy-solar-consumed-gauge": {
"name": "Solar consumed gauge",
"description": "This card shows how much of the solar energy was used by your home and was not exported to the grid."
@@ -13014,9 +13078,11 @@
"energy_devices_detail_graph_title": "Individual devices detail",
"energy_sankey_title": "Energy flow",
"water_sankey_title": "Water flow",
"gas_sankey_title": "Gas flow",
"energy_top_consumers_title": "Top consumers",
"power_sankey_title": "Current power flow",
"water_flow_sankey_title": "Current water flow",
"gas_flow_sankey_title": "Current gas flow",
"power_sources_graph_title": "Power sources",
"energy_grid_balance_title": "Grid energy balance",
"energy_grid_neutrality_gauge_title": "Grid neutrality gauge",
@@ -125,6 +125,9 @@
"ml": {
"nativeName": "മലയാളം"
},
"ms": {
"nativeName": "Bahasa Melayu"
},
"nl": {
"nativeName": "Nederlands"
},
@@ -27,6 +27,7 @@ const gasPrefs = (sources: number): EnergyPreferences => ({
})),
device_consumption: [],
device_consumption_water: [],
device_consumption_gas: [],
});
const small = generateEnergyData(1, {
@@ -33,6 +33,7 @@ const solarPrefs = (sources: number, forecast: boolean): EnergyPreferences => {
energy_sources: energySources,
device_consumption: [],
device_consumption_water: [],
device_consumption_gas: [],
};
};
@@ -163,6 +163,17 @@ describe("getHiddenFields", () => {
...getHiddenFields(fields, { mode: "advanced", token: "kept" }),
]).toEqual([]);
});
it("does not depend on the order of the fields", () => {
const token = ["token", { field: "mode", value: "advanced" }] as const;
const extra = [
"extra",
{ field: "token", operator: "not_exists" },
] as const;
const data = { mode: "simple", token: "stale" };
expect([...getHiddenFields([token, extra], data)]).toEqual(["token"]);
expect([...getHiddenFields([extra, token], data)]).toEqual(["token"]);
});
});
describe("getHiddenSchemaFields", () => {
+1 -159
View File
@@ -1,11 +1,5 @@
import type { HassService } from "home-assistant-js-websocket";
import { describe, expect, it } from "vitest";
import {
getServiceCallEntityIds,
getServiceFieldDefaults,
getServiceFieldVisibility,
getServiceHiddenFields,
} from "../../src/data/service";
import { getServiceCallEntityIds } from "../../src/data/service";
describe("getServiceCallEntityIds", () => {
it("returns an empty list when no entities are targeted", () => {
@@ -54,155 +48,3 @@ describe("getServiceCallEntityIds", () => {
).toEqual([]);
});
});
const asFields = (fields: Record<string, unknown>) =>
fields as unknown as HassService["fields"];
describe("getServiceFieldVisibility", () => {
it("keeps the condition of a top level field", () => {
expect(
getServiceFieldVisibility(
asFields({
a: { selector: { boolean: {} } },
b: { selector: { text: {} }, visible: { field: "a", value: true } },
})
)
).toEqual([
["a", undefined],
["b", { field: "a", value: true }],
]);
});
it("flattens the fields of a section", () => {
expect(
getServiceFieldVisibility(
asFields({
section: {
fields: {
a: { selector: { boolean: {} } },
b: { selector: { text: {} } },
},
},
})
)
).toEqual([
["a", []],
["b", []],
]);
});
it("gives the fields of a section the condition of the section", () => {
expect(
getServiceFieldVisibility(
asFields({
a: { selector: { boolean: {} } },
section: {
visible: { field: "a", value: true },
fields: {
b: { selector: { text: {} } },
c: {
selector: { text: {} },
visible: { field: "b", operator: "exists" },
},
},
},
})
)
).toEqual([
["a", undefined],
["b", [{ field: "a", value: true }]],
[
"c",
[
{ field: "a", value: true },
{ field: "b", operator: "exists" },
],
],
]);
});
it("hides every field of a section that is never visible", () => {
expect(
getServiceFieldVisibility(
asFields({
section: {
visible: false,
fields: { a: { selector: { text: {} } } },
},
})
)
).toEqual([["a", false]]);
});
});
describe("getServiceHiddenFields", () => {
const modeFields = {
mode: {
required: true,
default: "duration",
selector: { select: { options: ["duration", "end_of_track"] } },
},
advanced: {
fields: {
duration: {
required: true,
selector: { duration: {} },
visible: { field: "mode", value: "duration" },
},
},
},
} as unknown as HassService["fields"];
const visibility = getServiceFieldVisibility(modeFields);
const defaults = getServiceFieldDefaults(modeFields);
it("collects the defaults of fields, including fields in sections", () => {
expect(defaults).toEqual({ mode: "duration" });
});
it("evaluates a field without a value with its default", () => {
expect(getServiceHiddenFields(visibility, defaults, {})).toEqual(new Set());
expect(
getServiceHiddenFields(visibility, defaults, { mode: undefined })
).toEqual(new Set());
});
it("never hides a field based on a template", () => {
expect(
getServiceHiddenFields(visibility, defaults, {
mode: "{{ states('input_select.mode') }}",
})
).toEqual(new Set());
});
it("keeps the conditions that do not depend on a template", () => {
const fields = {
enabled: { selector: { boolean: {} } },
mode: { selector: { text: {} } },
section: {
visible: { field: "enabled", value: true },
fields: {
duration: {
selector: { duration: {} },
visible: { field: "mode", value: "duration" },
},
},
},
} as unknown as HassService["fields"];
expect(
getServiceHiddenFields(
getServiceFieldVisibility(fields),
{},
{
enabled: false,
mode: "{{ states('input_select.mode') }}",
}
)
).toEqual(new Set(["duration"]));
});
it("uses the value of the field when it has one", () => {
expect(
getServiceHiddenFields(visibility, defaults, { mode: "end_of_track" })
).toEqual(new Set(["duration"]));
});
});
+1
View File
@@ -81,6 +81,7 @@ export const generateEnergyPreferences = (
energy_sources: sources,
device_consumption: [],
device_consumption_water: [],
device_consumption_gas: [],
};
};
@@ -8,6 +8,7 @@ import {
ENERGY_CARD_CATALOG,
energyCardKey,
hasEnergySource,
hasGasDevices,
hasGasRateSource,
hasNowViewContent,
hasWaterRateSource,
@@ -25,6 +26,7 @@ const makePrefs = (
energy_sources: [],
device_consumption: [],
device_consumption_water: [],
device_consumption_gas: [],
...prefs,
});
@@ -46,6 +48,11 @@ const WATER_RATE = source({
stat_energy_from: "sensor.water",
stat_rate: "sensor.water_rate",
});
const GAS_DEVICE = { stat_consumption: "sensor.gas_device" };
const GAS_DEVICE_RATE = {
stat_consumption: "sensor.gas_device",
stat_rate: "sensor.gas_device_rate",
};
describe("energyCardKey", () => {
it("joins the view path and card type", () => {
@@ -275,3 +282,67 @@ describe("isEnergyCardVisible", () => {
}
});
});
describe("gas devices", () => {
it("hasGasDevices is true only with gas devices", () => {
expect(hasGasDevices(makePrefs())).toBe(false);
expect(
hasGasDevices(makePrefs({ device_consumption_gas: [GAS_DEVICE] }))
).toBe(true);
});
it("shows the gas sankey with gas devices only", () => {
const prefs = makePrefs({ device_consumption_gas: [GAS_DEVICE] });
expect(isEnergyCardVisible("gas", "gas-sankey", prefs, undefined)).toBe(
true
);
expect(
isEnergyCardVisible("gas", "gas-sankey", makePrefs(), undefined)
).toBe(false);
expect(
isEnergyCardVisible("gas", "gas-sankey", prefs, ["gas.gas-sankey"])
).toBe(false);
expect(applicableEnergyCardKeys("gas", prefs)).toContain("gas.gas-sankey");
});
it("does not show the gas sankey for a gas source without devices", () => {
expect(
isEnergyCardVisible(
"gas",
"gas-sankey",
makePrefs({ energy_sources: [GAS] }),
undefined
)
).toBe(false);
});
it("shows the overview gas graph with gas devices only", () => {
expect(
isEnergyCardVisible(
"overview",
"energy-gas-graph",
makePrefs({ device_consumption_gas: [GAS_DEVICE] }),
undefined
)
).toBe(true);
});
it("shows gas-flow-sankey in the now view only with a device rate", () => {
const withRate = makePrefs({ device_consumption_gas: [GAS_DEVICE_RATE] });
const noRate = makePrefs({ device_consumption_gas: [GAS_DEVICE] });
expect(
isEnergyCardVisible("now", "gas-flow-sankey", withRate, undefined)
).toBe(true);
expect(
isEnergyCardVisible("now", "gas-flow-sankey", noRate, undefined)
).toBe(false);
});
it("hasNowViewContent is true for a gas device with a rate", () => {
expect(
hasNowViewContent(
makePrefs({ device_consumption_gas: [GAS_DEVICE_RATE] })
)
).toBe(true);
});
});
@@ -38,6 +38,7 @@ const gasOnlyPrefs = (sources: number): EnergyPreferences => ({
})),
device_consumption: [],
device_consumption_water: [],
device_consumption_gas: [],
});
// Fixed "now" so the end fallback is deterministic.
@@ -131,6 +132,7 @@ describe("generateEnergyGasGraphData", () => {
],
device_consumption: [],
device_consumption_water: [],
device_consumption_gas: [],
};
const energyData = generateEnergyData(5, {
days: 2,
@@ -48,6 +48,7 @@ const solarPrefs = (opts: {
energy_sources: energySources,
device_consumption: [],
device_consumption_water: [],
device_consumption_gas: [],
};
};
@@ -78,6 +79,7 @@ describe("generateEnergySolarGraphData", () => {
energy_sources: [],
device_consumption: [],
device_consumption_water: [],
device_consumption_gas: [],
},
});
const result = generateEnergySolarGraphData({
+7 -1
View File
@@ -8,6 +8,8 @@
// Modules
"module": "ESNext",
"moduleResolution": "bundler",
// Resolve workspace packages to their sources, see packages/components
"customConditions": ["ha-source"],
"resolveJsonModule": true,
// Babel handles transpiling and no need for declaration files
"noEmit": true,
@@ -32,6 +34,10 @@
{
"name": "ts-lit-plugin",
"strict": true,
// Follows imports of @home-assistant/components entry points (workspace
// packages resolve through node_modules) to the component classes
// that declare their tags
"maxNodeModuleImportDepth": 2,
"rules": {
// Custom elements
"no-unknown-tag-name": "error",
@@ -158,5 +164,5 @@
}
},
// Exclude e2e tests — they have their own tsconfig that adds node types.
"exclude": ["test/e2e"]
"exclude": ["test/e2e", "packages/*/dist"]
}