Reworked translation mapping between Characteristic and Exposed values.

- Broke the code out into its own file.

- Added chaining and flipped translators to assist with re-using
  existing translators.  Also simplified logic when translating between
  HomeKit & Z2M values by removing special cases like the "binary"
  exposes.  Since everything can be expressed with translators &
  translator chains now, the process is streamlined.

- Wired up the defaultTranslator during the setup phase, so when mapping
  is called, there's no nil checks necessary; just a direct call to the
  mapping.Translator.

- Also added more documentation for the translation part since I forgot
  most of it after a year.
This commit is contained in:
Darell Tan
2024-11-22 01:27:53 +08:00
parent 1fbc4d520a
commit 5e24287d8c
3 changed files with 170 additions and 143 deletions

View File

@@ -152,6 +152,7 @@ func TestMappingTranslation(t *testing.T) {
&BoolTranslator{
characteristic.ContactSensorStateContactDetected,
characteristic.ContactSensorStateContactNotDetected}}
initExposeMappings(m)
for _, test := range []struct{ e, c any }{
{"CONTACT", characteristic.ContactSensorStateContactDetected},
@@ -184,6 +185,7 @@ func TestMappingNumeric(t *testing.T) {
s := service.NewTemperatureSensor()
m := &ExposeMapping{exp, s.CurrentTemperature.C, nil}
initExposeMappings(m)
for _, test := range []struct {
v any