feat: AC 三合一设备支持 + 新增 Plug/Gateway 驱动 + Cover 结构化重构
**AC 驱动增强** - 支持 thermostat + air-conditioner + air-fresh 多服务三合一设备,属性跨服务合并解析 - 新增 specResolver.findValListInService/findInService/hasService/serviceShortName 方法 - 移除 KindThermostat,heater/electric-blanket/thermostat 重新归类为 KindAirConditioner **Cover 窗帘重构** - CoverProps 结构化:Position/Status/Fault 分离 - SetPosition 优先写 target-position,回退 current-position - OnPropsChanged/FetchProps 支持多属性同步刷新 **新增设备类型** - Plug 驱动:插座/插头,支持开合、在线状态、属性变更订阅 - Gateway 驱动:网关设备类型识别 - miot/miot_errors.go:新增 MIoT 云端错误码定义 **MIoT 客户端健壮性** - SetProp 空结果视为失败;设备被移除或离线时自动刷新设备列表 - GetDevices 的 has_more 改用 parseBool 严格解析 - MIPS SubscribeMany 批量订阅减少往返 - matchURN 改为按段精确匹配,避免 tofan 误匹配 fan **Bridge** - 移除 Thermostat case,Cover 改用结构化 CoverProps - 新增设备创建后自动订阅属性变更 **其他** - 日志整体 Info → Debug 降噪 - .gitignore 忽略 /.codebuddy/memory - 更新 devices_test 与 08_adv_filter_classify 示例
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
package devices
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"xiaomihome/xiaomi"
|
||||
)
|
||||
|
||||
// Plug 插座/插头统一控制接口(电脑开机卡、智能插座等)。
|
||||
type Plug interface {
|
||||
Device
|
||||
TurnOn(ctx context.Context) error
|
||||
TurnOff(ctx context.Context) error
|
||||
IsOn(ctx context.Context) (bool, error)
|
||||
|
||||
OnPropsChanged(handler func(on bool)) (string, error)
|
||||
GetProps() PlugProps
|
||||
FetchProps(ctx context.Context) (PlugProps, error)
|
||||
}
|
||||
|
||||
// PlugProps 插座完整状态。
|
||||
type PlugProps struct {
|
||||
On bool `json:"on"`
|
||||
}
|
||||
|
||||
type plugDevice struct {
|
||||
BaseDevice
|
||||
propOn siidPiid
|
||||
propsCache PlugProps
|
||||
}
|
||||
|
||||
// NewPlug creates a Plug control for the given device.
|
||||
func NewPlug(client *xiaomi.Client, info *xiaomi.DeviceInfo) (Plug, error) {
|
||||
if info == nil {
|
||||
return nil, xiaomi.ErrDeviceNotFound
|
||||
}
|
||||
miotDev := client.GetMIoTDevice(info.DID)
|
||||
if miotDev == nil || miotDev.SpecInstance() == nil {
|
||||
return nil, fmt.Errorf("%w: device has no SPEC", xiaomi.ErrNotSupported)
|
||||
}
|
||||
|
||||
d := &plugDevice{
|
||||
BaseDevice: NewBaseDevice(client, info),
|
||||
}
|
||||
|
||||
for _, svc := range miotDev.SpecInstance().Services {
|
||||
for _, prop := range svc.Properties {
|
||||
if propertyShortName(prop.Type) == "on" && !d.propOn.valid {
|
||||
d.propOn = siidPiid{prop.SIID, prop.PIID, true}
|
||||
}
|
||||
}
|
||||
}
|
||||
if !d.propOn.valid {
|
||||
return nil, fmt.Errorf("%w: no on property", xiaomi.ErrNotSupported)
|
||||
}
|
||||
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func (d *plugDevice) TurnOn(ctx context.Context) error {
|
||||
return d.SetProp(ctx, d.propOn.siid, d.propOn.piid, true)
|
||||
}
|
||||
|
||||
func (d *plugDevice) TurnOff(ctx context.Context) error {
|
||||
return d.SetProp(ctx, d.propOn.siid, d.propOn.piid, false)
|
||||
}
|
||||
|
||||
func (d *plugDevice) IsOn(ctx context.Context) (bool, error) {
|
||||
prop, err := d.GetProp(ctx, d.propOn.siid, d.propOn.piid)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
b, _ := prop.Value.(bool)
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func (d *plugDevice) OnPropsChanged(handler func(on bool)) (string, error) {
|
||||
return d.SubProp(d.propOn.siid, d.propOn.piid, func(did string, prop *xiaomi.PropertyValue) {
|
||||
if b, ok := prop.Value.(bool); ok {
|
||||
d.propsCache.On = b
|
||||
handler(b)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (d *plugDevice) GetProps() PlugProps { return d.propsCache }
|
||||
|
||||
func (d *plugDevice) FetchProps(ctx context.Context) (PlugProps, error) {
|
||||
props, err := d.client.GetProps(ctx, []xiaomi.PropKey{
|
||||
{DID: d.info.DID, SIID: d.propOn.siid, PIID: d.propOn.piid},
|
||||
})
|
||||
if err != nil {
|
||||
return d.propsCache, err
|
||||
}
|
||||
for _, p := range props {
|
||||
if p.SIID == d.propOn.siid && p.PIID == d.propOn.piid {
|
||||
d.propsCache.On, _ = p.Value.(bool)
|
||||
}
|
||||
}
|
||||
return d.propsCache, nil
|
||||
}
|
||||
Reference in New Issue
Block a user