惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
P
Privacy International News Feed
T
Tenable Blog
L
Lohrmann on Cybersecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threat Research - Cisco Blogs
S
Securelist
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
S
Schneier on Security
P
Privacy & Cybersecurity Law Blog
Vercel News
Vercel News
Cyberwarzone
Cyberwarzone
月光博客
月光博客
T
The Blog of Author Tim Ferriss
Scott Helme
Scott Helme
爱范儿
爱范儿
Stack Overflow Blog
Stack Overflow Blog
C
Cisco Blogs
aimingoo的专栏
aimingoo的专栏
博客园 - 司徒正美
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Proofpoint News Feed
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
LangChain Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
阮一峰的网络日志
阮一峰的网络日志
Simon Willison's Weblog
Simon Willison's Weblog
T
Tor Project blog
Security Latest
Security Latest
Blog — PlanetScale
Blog — PlanetScale
G
GRAHAM CLULEY
V
Vulnerabilities – Threatpost
博客园 - 三生石上(FineUI控件)
I
InfoQ
Spread Privacy
Spread Privacy
B
Blog RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
S
SegmentFault 最新的问题
云风的 BLOG
云风的 BLOG
Last Week in AI
Last Week in AI
MongoDB | Blog
MongoDB | Blog
C
CERT Recently Published Vulnerability Notes
A
About on SuperTechFans
博客园_首页
Engineering at Meta
Engineering at Meta
Project Zero
Project Zero
Latest news
Latest news

无数

几何布朗运动下股票价格涨超/跌超50%概率计算步骤 两款软件助你方便调节 Windows 外接显示器亮度 iBasso DC07Pro 对比 EPZ TP55 从 Apple watch S4 升级到 S10 的使用体验 自己动手给 FiiO Q5s Type-C 版本更换电池 2025年9月底,265K+5080装机历程及体验 VuePress Toc 组件实现方案 推倒重构博客UI的要点记录 基于 MapBox 构建 VuePress 的旅行地图
HomeAssistant 中使用 Vue 构建UI面板的简易方式
晴和君 · 2025-03-08 · via 无数

一种简易的对 HomeAssistant 进行 UI 自定义的方式

发布于

HomeAssistant 中使用 Vue 构建UI面板的简易方式

HomeAssistant 中使用 Vue 构建UI面板的简易方式

今天在 Home Assistant Developer Docs 中看到了一篇关于如何新建侧边栏项目,以及对应的面板内容的教程。教程中使用了 wired-card ,并提示说可以使用任何想用的框架,我便立马想到了 Vue 。

示例代码中给了一些可用的变量,包括:

static get properties() {
    return {
      hass: { type: Object },
      narrow: { type: Boolean },
      route: { type: Object },
      panel: { type: Object },
    };
  }

其中 hass 就是 HomeAssistant 的实例,narrow 是当前页面是否处于窄屏状态,route 是当前页面的路由信息,panel 是当前页面的信息。

其中当前页面信息可以在 configuration.yaml 中进行配置。

官方的代码我就不重复了,下面直接贴出我的基于 Vue 框架的代码, 可以在此基础上自由发挥。

面板模块代码

新建<config>/www/XXPanel.js, <config>指代的是你的HA的配置目录,你要你安装过 HACS 中的lovelace插件,一般也默认会有 www 文件夹了,如果没有就手动新建下,XXPanel.js文件内容如下:

import { createApp, ref, nextTick } from 'https://unpkg.com/vue@3/dist/vue.esm-browser.js'

// custom_panel.js
const CustomPanel = {
  template: `
    <ha-card header="环境监测">
      <div class="card-content">
        <p>温度: {{ temperature }}°C</p>
        <p>湿度: {{ humidity }}%</p>
        <button @click="updateSensors" :disabled="isLoading">
          {{ isLoading ? '加载中...' : '刷新数据' }}
        </button>
      </div>
    </ha-card>
  `,
  data: () => ({
    temperature: 0,
    humidity: 0,
    isLoading: false
  }),
  methods: {
    async updateSensors() {
      this.isLoading = true;
      const [temp, hum] = await Promise.all([
        this.$hass.states['sensor.temperature'].state,
        this.$hass.states['sensor.relative_humidity'].state
      ]);
      this.temperature = temp;
      this.humidity = hum;
      this.isLoading = false;
    }
  },
  mounted() {
    nextTick(()=>{
      console.log(this.$hass)
      this.updateSensors();
      this.interval = setInterval(this.updateSensors, 60000);
    })
  },
  beforeUnmount() {
    clearInterval(this.interval);
  }
};
  
// 注册自定义元素
customElements.define('xx-panel', class extends HTMLElement {
    set hass(hass) {
      if (!this._panel) {
        this._panel = createApp(CustomPanel).mount(this);
      }
      this._panel.$hass = hass;
    }
});

这里在 mounted 钩子函数中,获取了 HomeAssistant 的实例,然后使用 this.$hass.states['sensor.temperature'].state 来获取温度传感器的值。另外使用了 beforeUnmount 钩子函数,在组件卸载时清除定时器,防止内存泄漏。

修改配置文件

configuration.yaml 文件中添加以下内容:

panel_custom:
  - name: xx-panel
    # url_path needs to be unique for each panel_custom config
    url_path: xx-panel
    sidebar_title: XXPanel
    sidebar_icon: mdi:server
    module_url: /local/XXPanel.js
    config:
      # Data you want to make available to panel
      hello: world

其中注意 name 字段,这个字段就是你在自定义元素中注册的名称,也就是 customElements.define('xx-panel', ...) 中的 xx-panel

最后重启 HA 即可。

© 商业转载请联系站长获得授权;
非商业转载请注明文章来源及链接。