Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

重构,支持多网关 #82

Open
wants to merge 15 commits into
base: test
Choose a base branch
from
16 changes: 16 additions & 0 deletions .github/workflows/validation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Validate with hassfest and HACS

on:
push:
pull_request:

jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: home-assistant/actions/hassfest@master
- uses: hacs/action@main
with:
category: integration
ignore: integration_manifest
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

此项目是Home Assistant平台[DS-AIR](https://www.daikin-china.com.cn/newha/products/4/19/DS-AIR/)以及[金制空气](https://www.daikin-china.com.cn/newha/products/4/19/jzkq/)自定义组件的实现

支持的网关设备型号为DTA117B611/DTA117C611,其他网关的支持情况未知
支持的网关设备型号为 DTA117B611、DTA117C611,其他网关的支持情况未知。(DTA117D611 可直接选择 DTA117C611)

# 支持设备

Expand All @@ -19,9 +19,22 @@

# 接入方法

1. 将项目ha-air目录部署到自定义组件目录,一般路径为```~/.homeassistant/custom_components/```
或使用hacs载入自定义存储库,设置URL```https://github.com/mypal/ha-dsair``` ,类别 ```集成```
2. 本集成已支持ha可视化配置,在配置-集成-添加集成中选择```DS-AIR``` ,依次填入网关IP、端口号、设备型号提交即可
## 安装
- 方法一:将项目 `ds_air` 目录直接拷贝到 `/config/custom_components/` 目录下

- 方法二:点击此按钮添加 HACS 自定义存储库

[![Open your Home Assistant instance and open a repository inside the Home Assistant Community Store.](https://my.home-assistant.io/badges/hacs_repository.svg)](https://my.home-assistant.io/redirect/hacs_repository/?owner=mypal&repository=ha-dsair&category=integration)

然后点击右下角 DOWNLOAD 安装

## 配置

- 方法一:在`配置-集成-添加集成`中选择`DS-AIR`

- 方法二:直接点击此按钮 [![Open your Home Assistant instance and start setting up a new integration.](https://my.home-assistant.io/badges/config_flow_start.svg)](https://my.home-assistant.io/redirect/config_flow_start/?domain=ds_air)

然后依次填入网关IP、端口号、设备型号提交即可

# 开发过程

Expand Down
70 changes: 35 additions & 35 deletions custom_components/ds_air/__init__.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,41 @@
"""
Platform for DS-AIR of Daikin
"""Platform for DS-AIR of Daikin

https://www.daikin-china.com.cn/newha/products/4/19/DS-AIR/
"""

import logging

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_SCAN_INTERVAL
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_SCAN_INTERVAL, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntry

from .hass_inst import GetHass
from .const import CONF_GW, DEFAULT_HOST, DEFAULT_PORT, DEFAULT_GW, DOMAIN
from .ds_air_service.config import Config
from .const import CONF_GW, DEFAULT_GW, DOMAIN
from .ds_air_service import Config, Service

_LOGGER = logging.getLogger(__name__)
PLATFORMS = ["climate", "sensor"]

PLATFORMS = [
Platform.CLIMATE,
Platform.SENSOR,
]

def _log(s: str):
s = str(s)
for i in s.split("\n"):
_LOGGER.debug(i)

def setup(hass, config):
hass.data[DOMAIN] = {}
GetHass.set_hass(hass)
return True


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry
):
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data.setdefault(DOMAIN, {})
host = entry.data[CONF_HOST]
port = entry.data[CONF_PORT]
gw = entry.data[CONF_GW]
scan_interval = entry.data[CONF_SCAN_INTERVAL]

_log(f"{host}:{port} {gw} {scan_interval}")

hass.data[DOMAIN][CONF_HOST] = host
hass.data[DOMAIN][CONF_PORT] = port
hass.data[DOMAIN][CONF_GW] = gw
hass.data[DOMAIN][CONF_SCAN_INTERVAL] = scan_interval
_LOGGER.debug("%s:%s %s %s", host, port, gw, scan_interval)

Config.is_c611 = gw == DEFAULT_GW
config = Config()
config.is_c611 = gw == DEFAULT_GW

from .ds_air_service.service import Service
await hass.async_add_executor_job(Service.init, host, port, scan_interval)
service = Service()
hass.data[DOMAIN][entry.entry_id] = service
await hass.async_add_executor_job(service.init, host, port, scan_interval, config)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(entry.add_update_listener(update_listener))

Expand All @@ -55,15 +44,26 @@ async def async_setup_entry(

async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
if hass.data[DOMAIN].get("listener") is not None:
hass.data[DOMAIN].get("listener")()
service: Service = hass.data[DOMAIN].pop(entry.entry_id)

if service.state_change_listener is not None:
service.state_change_listener()

unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
from .ds_air_service.service import Service
Service.destroy()
if not unload_ok:
return False

service.destroy()

return unload_ok
return True


async def update_listener(hass: HomeAssistant, entry: ConfigEntry):
async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
await hass.config_entries.async_reload(entry.entry_id)


async def async_remove_config_entry_device(
hass: HomeAssistant, config_entry: ConfigEntry, device_entry: DeviceEntry
) -> bool:
# reference: https://developers.home-assistant.io/docs/device_registry_index/#removing-devices
return True
Loading