mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 18:26:35 +00:00
Don't show features settings if none is compatible (#24419)
This commit is contained in:
parent
dfa98a4ba8
commit
3d9bde548d
@ -151,6 +151,38 @@ customCardFeatures.forEach((feature) => {
|
|||||||
CUSTOM_FEATURE_ENTRIES[feature.type] = feature;
|
CUSTOM_FEATURE_ENTRIES[feature.type] = feature;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const getSupportedFeaturesType = (
|
||||||
|
stateObj: HassEntity,
|
||||||
|
featuresTypes?: string[]
|
||||||
|
) => {
|
||||||
|
const filteredFeaturesTypes = UI_FEATURE_TYPES.filter(
|
||||||
|
(type) => !featuresTypes || featuresTypes.includes(type)
|
||||||
|
) as string[];
|
||||||
|
|
||||||
|
const customFeaturesTypes = customCardFeatures.map(
|
||||||
|
(feature) => `${CUSTOM_TYPE_PREFIX}${feature.type}`
|
||||||
|
);
|
||||||
|
return filteredFeaturesTypes
|
||||||
|
.concat(customFeaturesTypes)
|
||||||
|
.filter((type) => supportsFeaturesType(stateObj, type));
|
||||||
|
};
|
||||||
|
|
||||||
|
export const supportsFeaturesType = (stateObj: HassEntity, type: string) => {
|
||||||
|
if (isCustomType(type)) {
|
||||||
|
const customType = stripCustomPrefix(type);
|
||||||
|
const customFeatureEntry = CUSTOM_FEATURE_ENTRIES[customType];
|
||||||
|
if (!customFeatureEntry?.supported) return true;
|
||||||
|
try {
|
||||||
|
return customFeatureEntry.supported(stateObj);
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const supportsFeature = SUPPORTS_FEATURE_TYPES[type];
|
||||||
|
return !supportsFeature || supportsFeature(stateObj);
|
||||||
|
};
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface HASSDomEvents {
|
interface HASSDomEvents {
|
||||||
"features-changed": {
|
"features-changed": {
|
||||||
@ -178,20 +210,12 @@ export class HuiCardFeaturesEditor extends LitElement {
|
|||||||
|
|
||||||
private _supportsFeatureType(type: string): boolean {
|
private _supportsFeatureType(type: string): boolean {
|
||||||
if (!this.stateObj) return false;
|
if (!this.stateObj) return false;
|
||||||
|
return supportsFeaturesType(this.stateObj, type);
|
||||||
|
}
|
||||||
|
|
||||||
if (isCustomType(type)) {
|
private _getSupportedFeaturesType() {
|
||||||
const customType = stripCustomPrefix(type);
|
if (!this.stateObj) return [];
|
||||||
const customFeatureEntry = CUSTOM_FEATURE_ENTRIES[customType];
|
return getSupportedFeaturesType(this.stateObj, this.featuresTypes);
|
||||||
if (!customFeatureEntry?.supported) return true;
|
|
||||||
try {
|
|
||||||
return customFeatureEntry.supported(this.stateObj);
|
|
||||||
} catch {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const supportsFeature = SUPPORTS_FEATURE_TYPES[type];
|
|
||||||
return !supportsFeature || supportsFeature(this.stateObj);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _isFeatureTypeEditable(type: string) {
|
private _isFeatureTypeEditable(type: string) {
|
||||||
@ -225,18 +249,6 @@ export class HuiCardFeaturesEditor extends LitElement {
|
|||||||
return this._featuresKeys.get(feature)!;
|
return this._featuresKeys.get(feature)!;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _getSupportedFeaturesType() {
|
|
||||||
const featuresTypes = UI_FEATURE_TYPES.filter(
|
|
||||||
(type) => !this.featuresTypes || this.featuresTypes.includes(type)
|
|
||||||
) as readonly string[];
|
|
||||||
const customFeaturesTypes = customCardFeatures.map(
|
|
||||||
(feature) => `${CUSTOM_TYPE_PREFIX}${feature.type}`
|
|
||||||
);
|
|
||||||
return featuresTypes
|
|
||||||
.concat(customFeaturesTypes)
|
|
||||||
.filter((type) => this._supportsFeatureType(type));
|
|
||||||
}
|
|
||||||
|
|
||||||
protected render() {
|
protected render() {
|
||||||
if (!this.features || !this.hass) {
|
if (!this.features || !this.hass) {
|
||||||
return nothing;
|
return nothing;
|
||||||
|
@ -14,6 +14,7 @@ import {
|
|||||||
string,
|
string,
|
||||||
union,
|
union,
|
||||||
} from "superstruct";
|
} from "superstruct";
|
||||||
|
import type { HassEntity } from "home-assistant-js-websocket";
|
||||||
import type { HASSDomEvent } from "../../../../common/dom/fire_event";
|
import type { HASSDomEvent } from "../../../../common/dom/fire_event";
|
||||||
import { fireEvent } from "../../../../common/dom/fire_event";
|
import { fireEvent } from "../../../../common/dom/fire_event";
|
||||||
import type { LocalizeFunc } from "../../../../common/translations/localize";
|
import type { LocalizeFunc } from "../../../../common/translations/localize";
|
||||||
@ -36,7 +37,7 @@ import { actionConfigStruct } from "../structs/action-struct";
|
|||||||
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
||||||
import type { EditDetailElementEvent, EditSubElementEvent } from "../types";
|
import type { EditDetailElementEvent, EditSubElementEvent } from "../types";
|
||||||
import { configElementStyle } from "./config-elements-style";
|
import { configElementStyle } from "./config-elements-style";
|
||||||
import "./hui-card-features-editor";
|
import { getSupportedFeaturesType } from "./hui-card-features-editor";
|
||||||
|
|
||||||
const cardConfigStruct = assign(
|
const cardConfigStruct = assign(
|
||||||
baseLovelaceCardConfig,
|
baseLovelaceCardConfig,
|
||||||
@ -262,6 +263,10 @@ export class HuiTileCardEditor
|
|||||||
] as const satisfies readonly HaFormSchema[]
|
] as const satisfies readonly HaFormSchema[]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
private _hasCompatibleFeatures = memoizeOne(
|
||||||
|
(stateObj: HassEntity) => getSupportedFeaturesType(stateObj).length > 0
|
||||||
|
);
|
||||||
|
|
||||||
protected render() {
|
protected render() {
|
||||||
if (!this.hass || !this._config) {
|
if (!this.hass || !this._config) {
|
||||||
return nothing;
|
return nothing;
|
||||||
@ -292,6 +297,9 @@ export class HuiTileCardEditor
|
|||||||
data.features_position = "bottom";
|
data.features_position = "bottom";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const hasCompatibleFeatures =
|
||||||
|
(stateObj && this._hasCompatibleFeatures(stateObj)) || false;
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
<ha-form
|
<ha-form
|
||||||
.hass=${this.hass}
|
.hass=${this.hass}
|
||||||
@ -309,15 +317,19 @@ export class HuiTileCardEditor
|
|||||||
)}
|
)}
|
||||||
</h3>
|
</h3>
|
||||||
<div class="content">
|
<div class="content">
|
||||||
<ha-form
|
${hasCompatibleFeatures
|
||||||
class="features-form"
|
? html`
|
||||||
.hass=${this.hass}
|
<ha-form
|
||||||
.data=${data}
|
class="features-form"
|
||||||
.schema=${featuresSchema}
|
.hass=${this.hass}
|
||||||
.computeLabel=${this._computeLabelCallback}
|
.data=${data}
|
||||||
.computeHelper=${this._computeHelperCallback}
|
.schema=${featuresSchema}
|
||||||
@value-changed=${this._valueChanged}
|
.computeLabel=${this._computeLabelCallback}
|
||||||
></ha-form>
|
.computeHelper=${this._computeHelperCallback}
|
||||||
|
@value-changed=${this._valueChanged}
|
||||||
|
></ha-form>
|
||||||
|
`
|
||||||
|
: nothing}
|
||||||
<hui-card-features-editor
|
<hui-card-features-editor
|
||||||
.hass=${this.hass}
|
.hass=${this.hass}
|
||||||
.stateObj=${stateObj}
|
.stateObj=${stateObj}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user