fix(xiaomi): 空调风速按数字档位映射并增加 MQTT 反向语义解析

klwdz1.airc.001 等设备 fan-level 枚举为 Auto + Level1~Level5,SDK 侧
low/medium/high 在枚举里查不到,导致只有 Auto 能设置成功。

- valMapper 增加 levelFallback 开关(仅 fan-level 启用):精确匹配失败时
  low/medium/high 按数字档位相对位置映射(排除 Auto=0)
- name() 增加对称的反向映射:设备上报 1/3/5 → low/medium/high,
  控制与 MQTT 通知使用同一套语义枚举
- newValMapper 默认关闭 fallback,mode/swing 行为不变
- 新增 TestValMapperLevelFallback 覆盖正反向映射与 mode 无副作用
This commit is contained in:
2026-09-12 19:29:32 +08:00
parent a0ef854726
commit c18c0adaae
2 changed files with 121 additions and 8 deletions
+41
View File
@@ -3,6 +3,7 @@ package devices
import (
"testing"
"xiaomihome/miot"
"xiaomihome/xiaomi"
)
@@ -200,3 +201,43 @@ func TestServiceShortName(t *testing.T) {
}
}
}
func TestValMapperLevelFallback(t *testing.T) {
// klwdz1.airc.001 场景:Auto + Level1~Level5
vl := miot.MIoTSpecValueList{
{Value: 0, Desc: "Auto"}, {Value: 1, Desc: "Level1"}, {Value: 2, Desc: "Level2"},
{Value: 3, Desc: "Level3"}, {Value: 4, Desc: "Level4"}, {Value: 5, Desc: "Level5"},
}
vm := newValMapperLevel(vl)
cases := []struct{ name string; want int; ok bool }{
{"auto", 0, true}, {"Auto", 0, true},
{"low", 1, true}, {"medium", 3, true}, {"high", 5, true},
{"level3", 3, true},
}
for _, c := range cases {
got, ok := vm.val(c.name)
if ok != c.ok || (ok && got != c.want) {
t.Errorf("val(%q) = (%d,%v), want (%d,%v)", c.name, got, ok, c.want, c.ok)
}
}
// 反向解析(MQTT 上报值 → 语义枚举),与 val() 正向对称
rname := []struct{ v int; want string }{
{0, "auto"}, {1, "low"}, {3, "medium"}, {5, "high"}, {2, "Level2"},
}
for _, c := range rname {
if got := vm.name(c.v); got != c.want {
t.Errorf("name(%d) = %q, want %q", c.v, got, c.want)
}
}
// mode 枚举不受影响(无 low/medium/high fallback 副作用)
mvl := miot.MIoTSpecValueList{
{Value: 0, Desc: "Auto"}, {Value: 1, Desc: "Cool"}, {Value: 2, Desc: "Heat"},
}
mvm := newValMapper(mvl)
if _, ok := mvm.val("low"); ok {
t.Error("mode mapper should not resolve 'low'")
}
if v, ok := mvm.val("cool"); !ok || v != 1 {
t.Errorf("mode val(cool) = (%d,%v), want (1,true)", v, ok)
}
}