HomeAssistant 组件结构

HS 组件结构

Home Assistant 核心架构概述

  • Event Bus: facilitates the firing and listening of events – the beating heart of Home Assistant.
  • State Machine: keeps track of the states of things and fires a state_changed event when a state has been changed.
  • Service Registry: listens on the event bus for call_service events and allows other code to register services.
  • Timer: sends a time_changed event every 1 second on the event bus.

home assistant javascript card

1
2
3
4
5
6
7
8
9
10
11
type: custom:button-card
entity: input_boolean.kai_guan
show_state: true
show_label: true
icon: mdi:lightbulb

name: >
[[[
return `<ha-icon icon="mdi:sun-compass" style="width:20px;"></ha-icon>下次日落时间`
]]]

https://hacloud.fun/archives/UI%E5%8D%A1%E7%89%87%E6%95%99%E7%A8%8B

core 源码 : https://github.com/home-assistant/core/tree/master

代码运行解析

Docker Desktop安装在Windows上时,它可以配置为使用WSL2作为其后端。Docker引擎在WSL2的Linux内核上运行,不是通过传统的Hyper-V虚拟机。

main

argparse

faulthandler【错误记录的包,C语言编写 处理错误日志】

os 和 sys用于操作系统和系统级操作

threading 县城管理

错误日志文件的名称 FAULT_LOG_FILENAME

“Home Assistant only supports Linux, OSX and Windows using WSL”

1
2
3
4
5
validate_os() 验证操作系统
validate_python() 验证py版本
ensure_config_path 配置路径检查
get_arguments() arguments加工
check_threads() 线程检查 排除non-daemonic threads (thread.is_alive() and not thread.daemon)
1
2
3
4
5
6
7
8
9
def __init__(self, hass: HomeAssistant) -> None:
"""Initialize the auth store."""
self.hass = hass
self._loaded = False
self._users: dict[str, models.User] = None # 从AuthStore.async_get_user可知,_users的结构是:{user_id:user}
self._groups: dict[str, models.Group] = None # type: ignore[assignment] # 同理直接猜测 _groups的结构是 {group_id:group}
self._perm_lookup: PermissionLookup = None # type: ignore[assignment]
self._store = Store[dict[str, list[dict[str, Any]]]](
hass, STORAGE_VERSION, STORAGE_KEY, private=True, atomic_writes=True

用户,用户组,用户身份,相关的用户token都进行了一次保存,而且是以json 的形式保持到了文件中

User 等保存是直接以json的形式直接保存到文件中。设备的检测历史默认保存在sqlite中

home-assistant_v2.db

插件开发

  1. 拉环境 下载hass开发依赖

  2. git pull & git checkout

  3. pip3 install -r requirements_all.txt

    1
    homeassistant` --> `components

在testes new components-test 目录 添加测试用例

项目树

  1. Add our requirements to the manifest.json. If we need to add an external python dependency, it needs to be added here.
  2. Add our platform configuration schema. This will define what values we will expect when a user adds this integration in their configuration.yaml.
  3. Register all of our sensors with Home Assistant. This will be done in our async_setup_platform function.
  4. Create a new entity that represents the state and data we want to collect about each GitHub repository. This entity should also implement the async_update method that updates the data from GitHub.

测试

1
pip install pytest-homeassistant-custom-component

providing a hass instance that is properly setup for your test environment.

1
2
3
4
5
6
7
8
9
async def test_flow_user_step_no_input(hass):
"""Test appropriate error when no input is provided."""
_result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN, context={"source": "user"}
)
result = await hass.config_entries.flow.async_configure(
_result["flow_id"], user_input={}
)
assert {"base": "missing"} == result["errors"]