『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 1277|回复: 13

[基础教程] 小超人空调控制器蓝牙mesh版接入gateway3

[复制链接]

6

主题

54

帖子

694

积分

高级会员

Rank: 4

积分
694
金钱
640
HASS币
0
发表于 2024-6-4 20:59:50 | 显示全部楼层 |阅读模式
最近换了个蓝牙mesh的小超人,试着接入了一下也可以等官方更新


在config下新建xiaomi_gateway3.py
from custom_components.xiaomi_gateway3.core.devices import *

DEVICES = [{
    9507: ["Qdhkl", "AC Indoor Unit Controller(MESH)", "qdhkl.aircondition.b25"],
    "spec": [
        BaseConv("climate", "climate", mi="2.p.1"),
        MapConv("hvac_mode", mi="2.p.2", map={1: "cool", 2: "dry", 3: "fan_only", 4: "heat"}),
        MapConv("fan_mode", mi="3.p.1", map={0: "auto", 1: "low", 2: "medium", 3: "high"}),
        BaseConv("current_temp", mi="4.p.1"),
        BaseConv("target_temp", mi="2.p.3"),
    ],
}] + DEVICES
然后到/custom_components/xiaomi_gateway3/climate.py
自己添加或者复制过去
from homeassistant.components.climate import (
    ClimateEntity,
    ClimateEntityFeature,
    FAN_LOW,
    FAN_MEDIUM,
    FAN_HIGH,
    FAN_AUTO,
    HVACMode,
    HVACAction,
)
from homeassistant.const import PRECISION_WHOLE, UnitOfTemperature

from .hass.entity import XEntity


# noinspection PyUnusedLocal
async def async_setup_entry(hass, entry, async_add_entities) -> None:
    XEntity.ADD[entry.entry_id + "climate"] = async_add_entities


ACTIONS = {
    HVACMode.OFF: HVACAction.OFF,
    HVACMode.COOL: HVACAction.COOLING,
    HVACMode.HEAT: HVACAction.HEATING,
    HVACMode.DRY: HVACAction.DRYING,
    HVACMode.FAN_ONLY: HVACAction.FAN,
}


class XAqaraS2(XEntity, ClimateEntity):
    _attr_fan_mode = None
    _attr_fan_modes = [FAN_LOW, FAN_MEDIUM, FAN_HIGH, FAN_AUTO]
    _attr_hvac_mode = None
    _attr_hvac_modes = [HVACMode.OFF, HVACMode.COOL, HVACMode.HEAT]
    _attr_precision = PRECISION_WHOLE
    _attr_supported_features = (
        ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.FAN_MODE
    )
    _attr_target_temperature = 0
    _attr_target_temperature_step = 1
    _attr_temperature_unit = UnitOfTemperature.CELSIUS
    # support only KTWKQ03ES for now
    _attr_max_temp = 30
    _attr_min_temp = 17

    _enabled = None
    _mode = None

    def on_init(self):
        self.listen_attrs |= {"power", "current_temp", "hvac_mode", "target_temp"}

    def set_state(self, data: dict):
        if "power" in data:
            self._enabled = data["power"]
        if "current_temp" in data:
            self._attr_current_temperature = data["current_temp"]
        if "fan_mode" in data:
            self._attr_fan_mode = data["fan_mode"]
        if "hvac_mode" in data:
            self._attr_hvac_mode = data["hvac_mode"]
            self._mode = data["hvac_mode"]
            # better support HomeKit
            # https://github.com/AlexxIT/XiaomiGateway3/issues/707#issuecomment-1099109552
            self._attr_hvac_action = ACTIONS.get(self._attr_hvac_mode)
        if "target_temp" in data:
            # fix scenes with turned off climate
            # https://github.com/AlexxIT/XiaomiGateway3/issues/101#issuecomment-757781988
            self._attr_target_temperature = data["target_temp"]

        self._attr_hvac_mode = self._mode if self._enabled else HVACMode.OFF

    async def async_set_temperature(self, temperature: int, **kwargs) -> None:
        if temperature:
            self.device.write({self.attr: {"target_temp": temperature}})

    async def async_set_fan_mode(self, fan_mode: str) -> None:
        self.device.write({self.attr: {"fan_mode": fan_mode}})

    async def async_set_hvac_mode(self, hvac_mode: str) -> None:
        self.device.write({self.attr: {"hvac_mode": hvac_mode}})


class XAqaraE1(XEntity, ClimateEntity):
    _attr_hvac_mode = None
    _attr_hvac_modes = [HVACMode.OFF, HVACMode.HEAT, HVACMode.AUTO]
    _attr_supported_features = ClimateEntityFeature.TARGET_TEMPERATURE
    _attr_temperature_unit = UnitOfTemperature.CELSIUS
    _attr_max_temp = 30
    _attr_min_temp = 5
    _attr_target_temperature_step = 0.5

    _enabled = None
    _mode = None

    def on_init(self):
        self.listen_attrs = {"climate", "mode", "current_temp", "target_temp"}

    def set_state(self, data: dict):
        if "climate" in data:
            self._enabled = data["climate"]
        if "mode" in data:
            self._mode = data["mode"]
        if "current_temp" in data:
            self._attr_current_temperature = data["current_temp"]
        if "target_temp" in data:
            self._attr_target_temperature = data["target_temp"]

        if self._enabled is None or self._mode is None:
            return

        self._attr_hvac_mode = self._mode if self._enabled else HVACMode.OFF

    async def async_set_temperature(self, temperature: int, **kwargs) -> None:
        self.device.write({"target_temp": temperature})

    async def async_set_hvac_mode(self, hvac_mode: str) -> None:
        if hvac_mode in (HVACMode.HEAT, HVACMode.AUTO):
            payload = {"mode": hvac_mode}
        elif hvac_mode == HVACMode.OFF:
            payload = {"climate": False}
        else:
            return
        self.device.write(payload)


class XScdvbHAVC(XEntity, ClimateEntity):
    _attr_fan_mode = None
    _attr_fan_modes = [FAN_LOW, FAN_MEDIUM, FAN_HIGH, FAN_AUTO]
    _attr_hvac_mode = None
    _attr_hvac_modes = [
        HVACMode.OFF,
        HVACMode.COOL,
        HVACMode.HEAT,
        HVACMode.AUTO,
        HVACMode.DRY,
        HVACMode.FAN_ONLY,
    ]
    _attr_precision = PRECISION_WHOLE
    _attr_supported_features = (
        ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.FAN_MODE
    )
    _attr_temperature_unit = UnitOfTemperature.CELSIUS
    _attr_max_temp = 32
    _attr_min_temp = 16
    _attr_target_temperature_step = 1

    _enabled = None
    _mode = None

    def on_init(self):
        self.listen_attrs |= {"current_temp", "fan_mode", "hvac_mode", "target_temp"}

    def set_state(self, data: dict):
        if "climate" in data:
            self._enabled = data["climate"]
        if "hvac_mode" in data:
            self._mode = data["hvac_mode"]
        if "fan_mode" in data:
            self._attr_fan_mode = data["fan_mode"]
        if "current_temp" in data:
            self._attr_current_temperature = data["current_temp"]
        if "target_temp" in data:
            self._attr_target_temperature = data["target_temp"]

        if self._enabled is None or self._mode is None:
            return

        self._attr_hvac_mode = self._mode if self._enabled else HVACMode.OFF

    async def async_set_temperature(self, temperature: int, **kwargs) -> None:
        if temperature:
            self.device.write({"target_temp": temperature})

    async def async_set_fan_mode(self, fan_mode: str) -> None:
        if not self._enabled:
            self.device.write({"climate": True})
            self._attr_hvac_mode = self._mode
        self.device.write({"fan_mode": fan_mode})

    async def async_set_hvac_mode(self, hvac_mode: str) -> None:
        if hvac_mode == HVACMode.OFF:
            self.device.write({"climate": False})
        else:
            if not self._enabled:
                self.device.write({"climate": True})
            # better support HomeKit
            if hvac_mode == HVACMode.AUTO:
                hvac_mode = self._mode
            self.device.write({"hvac_mode": hvac_mode})


class XQdhklHAVC(XEntity, ClimateEntity):
    _attr_fan_mode = None
    _attr_fan_modes = [FAN_LOW, FAN_MEDIUM, FAN_HIGH, FAN_AUTO]
    _attr_hvac_mode = None
    _attr_hvac_modes = [
        HVACMode.OFF,
        HVACMode.COOL,
        HVACMode.HEAT,
        HVACMode.AUTO,
        HVACMode.DRY,
        HVACMode.FAN_ONLY,
    ]
    _attr_precision = PRECISION_WHOLE
    _attr_supported_features = (
        ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.FAN_MODE
    )
    _attr_temperature_unit = UnitOfTemperature.CELSIUS
    _attr_max_temp = 32
    _attr_min_temp = 16
    _attr_target_temperature_step = 1

    _enabled = None
    _mode = None

    def on_init(self):
        self.listen_attrs |= {"current_temp", "fan_mode", "hvac_mode", "target_temp"}

    def set_state(self, data: dict):
        if "climate" in data:
            self._enabled = data["climate"]
        if "hvac_mode" in data:
            self._mode = data["hvac_mode"]
        if "fan_mode" in data:
            self._attr_fan_mode = data["fan_mode"]
        if "current_temp" in data:
            self._attr_current_temperature = data["current_temp"]
        if "target_temp" in data:
            self._attr_target_temperature = data["target_temp"]

        if self._enabled is None or self._mode is None:
            return

        self._attr_hvac_mode = self._mode if self._enabled else HVACMode.OFF

    async def async_set_temperature(self, temperature: int, **kwargs) -> None:
        if temperature:
            self.device.write({"target_temp": temperature})

    async def async_set_fan_mode(self, fan_mode: str) -> None:
        if not self._enabled:
            self.device.write({"climate": True})
            self._attr_hvac_mode = self._mode
        self.device.write({"fan_mode": fan_mode})

    async def async_set_hvac_mode(self, hvac_mode: str) -> None:
        if hvac_mode == HVACMode.OFF:
            self.device.write({"climate": False})
        else:
            if not self._enabled:
                self.device.write({"climate": True})
            # better support HomeKit
            if hvac_mode == HVACMode.AUTO:
                hvac_mode = self._mode
            self.device.write({"hvac_mode": hvac_mode})


XEntity.NEW["climate.model.lumi.airrtc.tcpecn02"] = XAqaraS2
XEntity.NEW["climate.model.lumi.airrtc.agl001"] = XAqaraE1
XEntity.NEW["climate.model.14050"] = XScdvbHAVC
XEntity.NEW["climate.model.9507"] = XQdhklHAVC
QQ图片20240604205852.png

评分

参与人数 1金钱 +8 收起 理由
penguinmm + 8 高手,这是高手!

查看全部评分

回复

使用道具 举报

7

主题

244

帖子

1711

积分

金牌会员

Rank: 6Rank: 6

积分
1711
金钱
1467
HASS币
10
发表于 2024-6-5 09:25:12 | 显示全部楼层
不错不错
回复

使用道具 举报

2

主题

30

帖子

228

积分

中级会员

Rank: 3Rank: 3

积分
228
金钱
198
HASS币
0
发表于 2024-6-5 16:34:51 | 显示全部楼层
接入getway3就可以本地化了吗?接入auto,延迟有点厉害
回复

使用道具 举报

2

主题

30

帖子

228

积分

中级会员

Rank: 3Rank: 3

积分
228
金钱
198
HASS币
0
发表于 2024-6-5 16:50:01 | 显示全部楼层
复制代码进去后,提示错误,大佬,这是哪里不对?
1717577356482.jpg
回复

使用道具 举报

6

主题

54

帖子

694

积分

高级会员

Rank: 4

积分
694
金钱
640
HASS币
0
 楼主| 发表于 2024-6-5 20:50:15 | 显示全部楼层
xyxhome 发表于 2024-6-5 16:50
复制代码进去后,提示错误,大佬,这是哪里不对?

在config下新建xiaomi_gateway3.py没看这句吗
是写在这里头
回复

使用道具 举报

2

主题

30

帖子

228

积分

中级会员

Rank: 3Rank: 3

积分
228
金钱
198
HASS币
0
发表于 2024-6-6 08:34:13 | 显示全部楼层
没内涵 发表于 2024-6-5 20:50
在config下新建xiaomi_gateway3.py没看这句吗
是写在这里头

照着流程弄了一次后,重启ha,集成getway3加载不了
回复

使用道具 举报

2

主题

30

帖子

228

积分

中级会员

Rank: 3Rank: 3

积分
228
金钱
198
HASS币
0
发表于 2024-6-6 09:07:12 | 显示全部楼层
getway3加载不了,显示没有设备
1.png
2.png
回复

使用道具 举报

2

主题

30

帖子

228

积分

中级会员

Rank: 3Rank: 3

积分
228
金钱
198
HASS币
0
发表于 2024-6-6 09:08:38 | 显示全部楼层
/custom_components/xiaomi_gateway3/climate.py这里复制代码进去就不行了
1.png
2.png
回复

使用道具 举报

6

主题

54

帖子

694

积分

高级会员

Rank: 4

积分
694
金钱
640
HASS币
0
 楼主| 发表于 2024-6-6 10:31:41 | 显示全部楼层
xyxhome 发表于 2024-6-6 09:08
/custom_components/xiaomi_gateway3/climate.py这里复制代码进去就不行了

你的gateway3是什么版本的
还有你的小超人在ha里显示的实体截图看下
就算你没有自定义
也能认到啊
回复

使用道具 举报

2

主题

30

帖子

228

积分

中级会员

Rank: 3Rank: 3

积分
228
金钱
198
HASS币
0
发表于 2024-6-6 10:44:25 | 显示全部楼层
没内涵 发表于 2024-6-6 10:31
你的gateway3是什么版本的
还有你的小超人在ha里显示的实体截图看下
就算你没有自定义

getway3版本3.3.2,小超人接在Xiaomi Miot Auto
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-24 21:50 , Processed in 0.059543 second(s), 35 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表