Update xiaoshi-device-consumables-button.js

This commit is contained in:
xiaoshi
2026-01-01 15:44:49 +08:00
committed by GitHub
parent c30a918614
commit e4c9bcfe32

View File

@@ -1286,7 +1286,6 @@ class XiaoshiConsumablesButton extends LitElement {
super(); super();
this._oilPriceData = []; this._oilPriceData = [];
this._loading = false; this._loading = false;
this._dataLoaded = false; //button新元素
this._refreshInterval = null; this._refreshInterval = null;
this.theme = 'on'; this.theme = 'on';
} }
@@ -1305,7 +1304,7 @@ class XiaoshiConsumablesButton extends LitElement {
// 每300秒刷新一次数据减少频繁刷新 // 每300秒刷新一次数据减少频繁刷新
this._refreshInterval = setInterval(() => { this._refreshInterval = setInterval(() => {
this._loadOilPriceData(); this._loadOilPriceData();
}, 3000); }, 300000);
} }
_evaluateTheme() { _evaluateTheme() {
@@ -1432,11 +1431,9 @@ class XiaoshiConsumablesButton extends LitElement {
} }
this._oilPriceData = consumablesData; this._oilPriceData = consumablesData;
this._dataLoaded = true; //button新元素
} catch (error) { } catch (error) {
console.error('加载设备耗材数据失败:', error); console.error('加载设备耗材数据失败:', error);
this._oilPriceData = []; this._oilPriceData = [];
this._dataLoaded = true; //button新元素
} }
this._loading = false; this._loading = false;
@@ -1866,7 +1863,7 @@ class XiaoshiConsumablesButton extends LitElement {
const buttonBgColor = transparentBg ? 'transparent' : theme === 'on' ? 'rgb(255, 255, 255, 0.6)' : 'rgb(83, 83, 83, 0.6)'; const buttonBgColor = transparentBg ? 'transparent' : theme === 'on' ? 'rgb(255, 255, 255, 0.6)' : 'rgb(83, 83, 83, 0.6)';
// 检查是否需要自动隐藏只有数据加载完成且数量为0时才考虑隐藏 // 检查是否需要自动隐藏只有数据加载完成且数量为0时才考虑隐藏
const shouldAutoHide = this._dataLoaded && autoHide && warningCount === 0; const shouldAutoHide = autoHide && warningCount === 0;
// 如果需要自动隐藏返回空div // 如果需要自动隐藏返回空div
if (shouldAutoHide) { if (shouldAutoHide) {
@@ -1875,47 +1872,6 @@ class XiaoshiConsumablesButton extends LitElement {
// 渲染按钮 // 渲染按钮
let buttonHtml; let buttonHtml;
if (!this._dataLoaded) {
if (badgeMode) {
// 角标模式只显示图标数量为0时不显示角标
buttonHtml = html`
<div class="consumables-status badge-mode" style="--bg-color: ${buttonBgColor};" @click=${this._handleButtonClick}>
<ha-icon class="status-icon" icon="${buttonIcon}"></ha-icon>
</div>
`;
} else {
// 普通模式
// 应用锁定白色功能
const iconColor = lockWhiteFg ? 'rgb(255, 255, 255)' : fgColor;
const textColor = lockWhiteFg ? 'rgb(255, 255, 255)' : fgColor;
// 构建显示文本
let displayText = buttonText;
// 根据hide_colon参数决定是否显示冒号
if (!hideColon) {
displayText += ':';
} else {
displayText += ' ';
}
// 根据hide_zero参数决定是否显示0值
if (!hideZero) {
displayText += ' 0';
} else {
// 隐藏0值时使用CSS空格占位符保持布局稳定
displayText += '\u2002'; // 两个en空格大约等于数字"0"的宽度
}
buttonHtml = html`
<div class="consumables-status" style="--fg-color: ${textColor}; --bg-color: ${buttonBgColor};" @click=${this._handleButtonClick}>
${!hideIcon ? html`<ha-icon class="status-icon" style="color: ${iconColor};" icon="${buttonIcon}"></ha-icon>` : ''}
${displayText}
</div>
`;
}
} else {
// 数据加载完成后
if (badgeMode) { if (badgeMode) {
// 角标模式:只显示图标,根据数量显示角标 // 角标模式:只显示图标,根据数量显示角标
const hasWarning = warningCount > 0; const hasWarning = warningCount > 0;
@@ -1964,7 +1920,6 @@ class XiaoshiConsumablesButton extends LitElement {
</div> </div>
`; `;
} }
}
// 返回最终的渲染结果(包括按钮和预览卡片) // 返回最终的渲染结果(包括按钮和预览卡片)
return html` return html`