ADD-ON & Integration开发的不同
ADD-ON的开发基于docker
Integration的开发
通过git安装HomeAssistant Core,从而安装HomeAssistant
git介绍
git 是个工具,github是图形界面版,用于版本管理,一个fork就是一条开发线branch,repository、project历史版本,commit是一瞬间的快照
安装HA前的准备工作
git clone https://github.com/q1uTruth/core.git
sudo apt-get install python3-pip python3-dev python3-venv autoconf libssl-dev libxml2-dev libxslt1-dev libjpeg-dev libffi-dev libudev-dev zlib1g-dev pkg-config libavformat-dev libavcodec-dev libavdevice-dev libavutil-dev libswscale-dev libavresample-dev libavfilter-dev ffmpeg
libavresample-dev
apt install python3.9-venv
python -m pip install --upgrade pip==21.0
pip uninstall charset-normalizer
pip install charset-normalizer==2.0.0
sudo apt-get remove --auto-remove python3.5
cd /usr/bin
rm -rf python
ln -s /usr/bin/python3 python
apt install -y libpcap0.8
启动Home Assistant
连接同一wifi
cd core
source venv/bin/activate
hass -c config
http://127.0.0.1:8123 hass --open-ui
\wsl$看子系统
debugpy远程调试。 debugpy像ida的远程调试,使用单独的协议
cd core
source venv/bin/activate
python3 -m debugpy --listen localhost:5678 --wait-for-client -m homeassistant -c config
如果采用core的安装方式,自己开发的集成这样放入HA:
要在core/config/目录下新建custom_components,将自己开发的集成放到文件夹里
VSCode中的 继续,单步跳过,单步调试,单步跳出
继续:执行到下一个断点
单步跳过:如函数内部无断点,不进入函数内部
单步调试:进入函数内部
单步跳出:跳出函数内部
动态调试,查看调用堆栈
整体逻辑
init.py中的async_setup_entry函数__init__了FileExplorer类(hass.data[DOMAIN]=FileExplorer),但是还没调用获取路径的函数
获取路径的函数是在点击左侧的插件时调用的,点击时先调用view.py(实现了HomeAssistantView的子类)的post方法。
该方法首先获取dir,dir为/root/core/config。通过前者为 /root/core/config/ 后者为空
_path = hass.config.path(‘’) + ‘/’ + res.get(‘path’, ‘’).lstrip(‘/’)
之后判断是请求的类型,如果是get请求,就调用FileExplorer实例的getDirectory,该函数返回一个字典,字典中每个元素对应一个文件,包含文件name、type和size等信息,然后通过self.json传到前端
加载内容使用的是shaonianzhentan.py中的load_content函数
hass的config用于cfg.get(access.key)
关键代码分析
view.py post方法中
async def post(self, request):
hass = request.app["hass"]
fileExplorer = hass.data[DOMAIN]
res = await request.json()
_type = res.get('type', '')
_url = res.get('url', '')
_path = hass.config.path('') + '/' + res.get('path', '').lstrip('/')
try:
if _type == 'get':
# 获取目录和文件
data = fileExplorer.getDirectory(_path)
return self.json(data)
elif _type == 'get-content':
# 获取文件内容
data = load_content(_path)
return self.json({ 'code': 0, 'data': data})
file_explore.py
def getDirectory(self, dir):
allcontent = listdir(dir)
dirItem = []
for item in allcontent:
try:
hashInfo = {}
path_name = join(dir,item)
if exists(path_name) == False:
continue
# os.stat是将文件的相关属性读出来,然后用stat模块来处理
listInfo = stat(path_name)
hashInfo['name'] = item
hashInfo['url'] = item
hashInfo['edit'] = datetime.datetime.fromtimestamp(int(listInfo.st_mtime)).strftime('%Y-%m-%d %H:%M:%S')
if isfile(path_name):
hashInfo['type'] = 'file'
hashInfo['size'] = int(listInfo.st_size)
if isdir(path_name):
hashInfo['type'] = 'dir'
hashInfo['size'] = get_dir_size(path_name)
dirItem.append(hashInfo)
except Exception as ex:
print(ex)
# 按照字典里的name键升序排序
dirItem.sort(key=lambda x: x['name'], reverse=True)
return dirItem
shaonianzhentan.py
# 加载内容
def load_content(file_path):
fp = open(file_path, 'r', encoding='UTF-8')
content = fp.read()
fp.close()
return content
前端VUE开发
大量js代码连接html和css文件,但缺乏正规的组织形式
VUE是一个JavaScript框架
想获得设备的信息好像是去实体状态表里看
\wsl$\kali-linux\root\core\config.storage core.entity_registry
Home Assistant Overview
自动化要有DOMAIN,不仅要理所应当地处理内部,还要对外部的变化进行对应处理
贡献给官方,官方会人工审核