Introduce constructors for ExposeMapping

This should allow the ExposeMapping struct to be changed without
constantly impacting its callers.
This commit is contained in:
Darell Tan
2024-11-24 15:25:17 +08:00
parent b11ab07fbb
commit d4daef7bce
8 changed files with 22 additions and 12 deletions

View File

@@ -22,7 +22,7 @@ func createBatteryServices(dev *Device) (byte, []*service.S, []*ExposeMapping, e
batt.ChargingState.SetValue(characteristic.ChargingStateNotChargeable)
svcs = append(svcs, batt.S)
exposes = append(exposes, &ExposeMapping{&exp, batt.BatteryLevel.C, nil})
exposes = append(exposes, NewExposeMapping(&exp, batt.BatteryLevel.C))
}
}

View File

@@ -31,13 +31,13 @@ func createClimateServices(dev *Device) (byte, []*service.S, []*ExposeMapping, e
s.CurrentTemperature.SetStepValue(0.01)
svcs = append(svcs, s.S)
exposes = append(exposes, &ExposeMapping{&exp, s.CurrentTemperature.C, nil})
exposes = append(exposes, NewExposeMapping(&exp, s.CurrentTemperature.C))
case exp.Name == "humidity":
s := service.NewHumiditySensor()
s.CurrentRelativeHumidity.SetStepValue(0.01) // ditto, but 1% increments
svcs = append(svcs, s.S)
exposes = append(exposes, &ExposeMapping{&exp, s.CurrentRelativeHumidity.C, nil})
exposes = append(exposes, NewExposeMapping(&exp, s.CurrentRelativeHumidity.C))
case exp.Name == "pressure":
// TODO looks like pressure is not standard in HomeKit

View File

@@ -22,10 +22,10 @@ func createContactServices(dev *Device) (byte, []*service.S, []*ExposeMapping, e
s := service.NewContactSensor()
svcs = append(svcs, s.S)
exposes = append(exposes, &ExposeMapping{&exp, s.ContactSensorState.C,
exposes = append(exposes, NewTranslatedExposeMapping(&exp, s.ContactSensorState.C,
&BoolTranslator{
characteristic.ContactSensorStateContactNotDetected,
characteristic.ContactSensorStateContactDetected}})
characteristic.ContactSensorStateContactDetected}))
}
}

View File

@@ -31,12 +31,12 @@ func createLightServices(dev *Device) (byte, []*service.S, []*ExposeMapping, err
switch {
case feat.Name == "state" && feat.Type == "binary":
svcs = append(svcs, light.S)
exposes = append(exposes, &ExposeMapping{&feat, light.On.C, nil})
exposes = append(exposes, NewExposeMapping(&feat, light.On.C))
case feat.Name == "brightness" && feat.Type == "numeric":
brightness := characteristic.NewBrightness()
light.AddC(brightness.C)
exposes = append(exposes, &ExposeMapping{&feat, brightness.C, nil})
exposes = append(exposes, NewExposeMapping(&feat, brightness.C))
}
}
}

View File

@@ -24,7 +24,7 @@ func createMotionServices(dev *Device) (byte, []*service.S, []*ExposeMapping, er
s := service.NewMotionSensor()
svcs = append(svcs, s.S)
exposes = append(exposes, &ExposeMapping{&exp, s.MotionDetected.C, nil})
exposes = append(exposes, NewExposeMapping(&exp, s.MotionDetected.C))
}
}

View File

@@ -33,7 +33,7 @@ func createSwitchServices(dev *Device) (byte, []*service.S, []*ExposeMapping, er
sw.AddC(n.C)
svcs = append(svcs, sw.S)
exposes = append(exposes, &ExposeMapping{&f, sw.On.C, nil})
exposes = append(exposes, NewExposeMapping(&f, sw.On.C))
}
}
}

10
z2m.go
View File

@@ -242,6 +242,16 @@ type ExposeMapping struct {
Translator MappingTranslator
}
// Creates a new ExposeMapping
func NewExposeMapping(e *ExposesEntry, c *characteristic.C) *ExposeMapping {
return &ExposeMapping{ExposesEntry: e, Characteristic: c}
}
// Creates a new ExposeMapping with a MappingTranslator
func NewTranslatedExposeMapping(e *ExposesEntry, c *characteristic.C, t MappingTranslator) *ExposeMapping {
return &ExposeMapping{ExposesEntry: e, Characteristic: c, Translator: t}
}
func (m *ExposeMapping) String() string {
return fmt.Sprintf("{%q,%s -> ctyp %s}",
m.ExposesEntry.Name, m.ExposesEntry.Type, m.Characteristic.Type)

View File

@@ -148,10 +148,10 @@ func TestMappingTranslation(t *testing.T) {
}`)
s := service.NewContactSensor()
m := &ExposeMapping{exp, s.ContactSensorState.C,
m := NewTranslatedExposeMapping(exp, s.ContactSensorState.C,
&BoolTranslator{
characteristic.ContactSensorStateContactDetected,
characteristic.ContactSensorStateContactNotDetected}}
characteristic.ContactSensorStateContactNotDetected})
initExposeMappings(m)
for _, test := range []struct{ e, c any }{
@@ -184,7 +184,7 @@ func TestMappingNumeric(t *testing.T) {
}`)
s := service.NewTemperatureSensor()
m := &ExposeMapping{exp, s.CurrentTemperature.C, nil}
m := NewExposeMapping(exp, s.CurrentTemperature.C)
initExposeMappings(m)
for _, test := range []struct {