diff --git a/s_battery.go b/s_battery.go index ed21b92..ce6a081 100644 --- a/s_battery.go +++ b/s_battery.go @@ -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)) } } diff --git a/s_climate_sensors.go b/s_climate_sensors.go index fdb4f59..fd6c595 100644 --- a/s_climate_sensors.go +++ b/s_climate_sensors.go @@ -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 diff --git a/s_contact_sensor.go b/s_contact_sensor.go index bec083b..87c83dc 100644 --- a/s_contact_sensor.go +++ b/s_contact_sensor.go @@ -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})) } } diff --git a/s_light.go b/s_light.go index 7809c5f..82b46f8 100644 --- a/s_light.go +++ b/s_light.go @@ -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)) } } } diff --git a/s_motion_sensor.go b/s_motion_sensor.go index 47ad4cf..e028537 100644 --- a/s_motion_sensor.go +++ b/s_motion_sensor.go @@ -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)) } } diff --git a/s_switch.go b/s_switch.go index 75434b6..1eb5ae6 100644 --- a/s_switch.go +++ b/s_switch.go @@ -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)) } } } diff --git a/z2m.go b/z2m.go index 45f6bfa..c2edc62 100644 --- a/z2m.go +++ b/z2m.go @@ -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) diff --git a/z2m_test.go b/z2m_test.go index 79eafa0..eaeb2a0 100644 --- a/z2m_test.go +++ b/z2m_test.go @@ -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 {