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

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
月光博客
月光博客
T
Threat Research - Cisco Blogs
小众软件
小众软件
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tailwind CSS Blog
Cisco Talos Blog
Cisco Talos Blog
V
V2EX
博客园 - 【当耐特】
C
Cybersecurity and Infrastructure Security Agency CISA
Hugging Face - Blog
Hugging Face - Blog
The Cloudflare Blog
The Last Watchdog
The Last Watchdog
Simon Willison's Weblog
Simon Willison's Weblog
T
Threatpost
S
Secure Thoughts
O
OpenAI News
P
Proofpoint News Feed
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
Scott Helme
Scott Helme
T
Tenable Blog
A
Arctic Wolf
L
LINUX DO - 热门话题
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
Visual Studio Blog
Hacker News: Ask HN
Hacker News: Ask HN
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
博客园 - Franky
WordPress大学
WordPress大学
Know Your Adversary
Know Your Adversary
博客园_首页
雷峰网
雷峰网
IT之家
IT之家
PCI Perspectives
PCI Perspectives
L
LINUX DO - 最新话题
H
Heimdal Security Blog

无数

几何布朗运动下股票价格涨超/跌超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 即可。

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