matter-all-in-one-addon 2.3.13 → 2.3.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/README.md +18 -12
  2. package/dist/camera/browser-preview.js +12 -10
  3. package/dist/camera/browser-preview.js.map +1 -1
  4. package/dist/camera/homekit/homekit-camera-recording.delegate.d.ts +5 -0
  5. package/dist/camera/homekit/homekit-camera-recording.delegate.js +14 -0
  6. package/dist/camera/homekit/homekit-camera-recording.delegate.js.map +1 -1
  7. package/dist/camera/homekit/homekit-camera-stream.delegate.d.ts +8 -2
  8. package/dist/camera/homekit/homekit-camera-stream.delegate.js +113 -49
  9. package/dist/camera/homekit/homekit-camera-stream.delegate.js.map +1 -1
  10. package/dist/camera/homekit/homekit-camera.accessory.js +17 -3
  11. package/dist/camera/homekit/homekit-camera.accessory.js.map +1 -1
  12. package/dist/camera/homekit/tuya-hap-talkback.d.ts +1 -0
  13. package/dist/camera/homekit/tuya-hap-talkback.js +11 -8
  14. package/dist/camera/homekit/tuya-hap-talkback.js.map +1 -1
  15. package/dist/camera/matter/camera-endpoint.builder.js +31 -18
  16. package/dist/camera/matter/camera-endpoint.builder.js.map +1 -1
  17. package/dist/converters/binary_sensor.converter.d.ts +1 -1
  18. package/dist/converters/binary_sensor.converter.js +2 -2
  19. package/dist/converters/climate.converter.d.ts +14 -1
  20. package/dist/converters/climate.converter.js +44 -2
  21. package/dist/converters/climate.converter.js.map +1 -1
  22. package/dist/converters/sensor.converter.js +6 -6
  23. package/dist/converters/sensor.converter.js.map +1 -1
  24. package/dist/device-profiles.js +33 -2
  25. package/dist/device-profiles.js.map +1 -1
  26. package/dist/device-registry.js +12 -0
  27. package/dist/device-registry.js.map +1 -1
  28. package/dist/entities/air_quality.entity.js +24 -1
  29. package/dist/entities/air_quality.entity.js.map +1 -1
  30. package/dist/entities/base.entity.js +25 -5
  31. package/dist/entities/base.entity.js.map +1 -1
  32. package/dist/entities/climate.entity.d.ts +23 -0
  33. package/dist/entities/climate.entity.js +174 -0
  34. package/dist/entities/climate.entity.js.map +1 -0
  35. package/dist/entities/closure.entity.js +24 -1
  36. package/dist/entities/closure.entity.js.map +1 -1
  37. package/dist/entities/composite-device.entity.js +23 -7
  38. package/dist/entities/composite-device.entity.js.map +1 -1
  39. package/dist/entities/cooktop.entity.d.ts +1 -0
  40. package/dist/entities/cooktop.entity.js +18 -3
  41. package/dist/entities/cooktop.entity.js.map +1 -1
  42. package/dist/entities/energy_tariff.entity.js +16 -4
  43. package/dist/entities/energy_tariff.entity.js.map +1 -1
  44. package/dist/entities/humidifier.entity.d.ts +3 -0
  45. package/dist/entities/humidifier.entity.js +61 -45
  46. package/dist/entities/humidifier.entity.js.map +1 -1
  47. package/dist/entities/lock.entity.js +9 -5
  48. package/dist/entities/lock.entity.js.map +1 -1
  49. package/dist/entities/oven.entity.d.ts +2 -0
  50. package/dist/entities/oven.entity.js +50 -8
  51. package/dist/entities/oven.entity.js.map +1 -1
  52. package/dist/entities/soil_sensor.entity.js +2 -1
  53. package/dist/entities/soil_sensor.entity.js.map +1 -1
  54. package/dist/entities/vacuum.entity.js +4 -4
  55. package/dist/entities/vacuum.entity.js.map +1 -1
  56. package/dist/entities/valve.entity.js +29 -5
  57. package/dist/entities/valve.entity.js.map +1 -1
  58. package/dist/frontend/assets/CameraConfigModal-DnDZbkpj.js +6 -0
  59. package/dist/frontend/assets/CameraUiModal-DVCgRtyT.js +1 -0
  60. package/dist/frontend/assets/DeviceModal-DFgXDEzB.js +5 -0
  61. package/dist/frontend/assets/QRCodeDisplay-B3RFLtWz.js +8 -0
  62. package/dist/frontend/assets/ScryptedModal-DZ4B2NLt.js +1 -0
  63. package/dist/frontend/assets/SettingsModal-BBflJWuJ.js +2 -0
  64. package/dist/frontend/assets/clipboard-CnKYMNaq.js +1 -0
  65. package/dist/frontend/assets/index-CONl6ATp.js +10 -0
  66. package/dist/frontend/index.html +1 -1
  67. package/dist/hap/hap-generic-accessory.d.ts +6 -0
  68. package/dist/hap/hap-generic-accessory.js +641 -26
  69. package/dist/hap/hap-generic-accessory.js.map +1 -1
  70. package/dist/homeAssistant.d.ts +1 -0
  71. package/dist/homeAssistant.js +40 -1
  72. package/dist/homeAssistant.js.map +1 -1
  73. package/dist/mqtt/mqtt-client.d.ts +2 -0
  74. package/dist/mqtt/mqtt-client.js +44 -2
  75. package/dist/mqtt/mqtt-client.js.map +1 -1
  76. package/dist/mqtt/mqtt.entity.d.ts +12 -0
  77. package/dist/mqtt/mqtt.entity.js +231 -82
  78. package/dist/mqtt/mqtt.entity.js.map +1 -1
  79. package/dist/platform.d.ts +2 -0
  80. package/dist/platform.js +111 -37
  81. package/dist/platform.js.map +1 -1
  82. package/dist/proxy.js +13 -1
  83. package/dist/proxy.js.map +1 -1
  84. package/dist/utils/hap-firmware.js +1 -1
  85. package/dist/utils/hap-firmware.js.map +1 -1
  86. package/dist/utils/matter-command-response.js +64 -44
  87. package/dist/utils/matter-command-response.js.map +1 -1
  88. package/dist/utils/matter-reset-storage.d.ts +2 -0
  89. package/dist/utils/matter-reset-storage.js +74 -0
  90. package/dist/utils/matter-reset-storage.js.map +1 -0
  91. package/dist/utils/measurements.d.ts +8 -0
  92. package/dist/utils/measurements.js +42 -0
  93. package/dist/utils/measurements.js.map +1 -0
  94. package/package.json +17 -17
  95. package/dist/frontend/assets/index-1k-iTL-m.js +0 -26
@@ -1,3 +1,6 @@
1
+ import { toCelsius, fromCelsius } from "../utils/measurements.js";
2
+ import { sensorConverter } from "../converters/sensor.converter.js";
3
+ import { climateConverter } from "../converters/climate.converter.js";
1
4
  /**
2
5
  * HAP Generic Accessory — expone cualquier entidad de Home Assistant como un
3
6
  * accesorio HAP nativo de hap-nodejs 2.2.3, igual que las cámaras pero sin
@@ -26,7 +29,7 @@ export const HAP_PROFILE_LABELS = {
26
29
  garage_door: "Puerta de Garaje",
27
30
  doorbell: "Videoportero / Timbre",
28
31
  fan_hap: "Ventilador (HAP)",
29
- heater_cooler: "Calefactor / Aire",
32
+ heater_cooler: "Aire Acondicionado (HeaterCooler)",
30
33
  thermostat_hap: "Termostato (HAP)",
31
34
  outlet_hap: "Enchufe (HAP)",
32
35
  switch_hap: "Interruptor (HAP)",
@@ -65,7 +68,7 @@ const HAP_PROFILE_CATEGORIES = {
65
68
  garage_door: 4 /* Categories.GARAGE_DOOR_OPENER */,
66
69
  doorbell: 18 /* Categories.VIDEO_DOORBELL */,
67
70
  fan_hap: 3 /* Categories.FAN */,
68
- heater_cooler: 20 /* Categories.AIR_HEATER */,
71
+ heater_cooler: 21 /* Categories.AIR_CONDITIONER */,
69
72
  thermostat_hap: 9 /* Categories.THERMOSTAT */,
70
73
  outlet_hap: 7 /* Categories.OUTLET */,
71
74
  switch_hap: 8 /* Categories.SWITCH */,
@@ -118,10 +121,13 @@ export class HapGenericAccessory {
118
121
  ? record.valveSetDurationSeconds
119
122
  : 0;
120
123
  this.accessory = new Accessory(record.name || entityId, accUuid);
124
+ this.accessory.category =
125
+ HAP_PROFILE_CATEGORIES[record.hapProfile] ?? 1 /* Categories.OTHER */;
121
126
  this.configureAccessoryInformation();
122
127
  this.addServiceForProfile(record.hapProfile);
123
128
  this.bindPrimaryHomeAssistantEntity();
124
129
  this.addDiscoveredSensorServices();
130
+ this.bindAdditionalHomeAssistantProfiles();
125
131
  this.accessory.on("characteristic-warning", (warning) => {
126
132
  this.platform.log?.warn?.(`[HAPCommandTrace][${this.entityId}] warning=${warning.type} characteristic=${warning.characteristic.displayName}`);
127
133
  });
@@ -192,10 +198,18 @@ export class HapGenericAccessory {
192
198
  : [own];
193
199
  }
194
200
  bindDiscoveredSensor(entityId, serviceType, characteristic, suffix, read) {
201
+ const primary = this.accessory.getService(serviceType);
195
202
  const service = this.accessory.addService(serviceType, `${this.record.name} ${suffix}`, `sensor:${entityId}`);
196
- service
197
- .getCharacteristic(characteristic)
198
- .onGet(() => read(this.getHomeAssistantState(entityId)));
203
+ // Keep the previously allocated service/subtype for existing pairings while
204
+ // presenting the now-functional primary sensor instead of two tiles.
205
+ if (primary && entityId === this.entityId)
206
+ service.setHiddenService(true);
207
+ service.getCharacteristic(characteristic).onGet(() => {
208
+ const state = this.getHomeAssistantState(entityId);
209
+ if (!state || ["unknown", "unavailable"].includes(state.state))
210
+ throw -70402;
211
+ return read(state);
212
+ });
199
213
  this.discoveredSensorBindings.set(entityId, {
200
214
  service,
201
215
  characteristic,
@@ -209,13 +223,18 @@ export class HapGenericAccessory {
209
223
  return (item.entityId.startsWith(`${domain}.`) &&
210
224
  state?.attributes?.device_class === deviceClass);
211
225
  });
212
- const numeric = (deviceClass, serviceType, characteristic, suffix, minimum = 0) => {
226
+ const numeric = (deviceClass, serviceType, characteristic, suffix, minimum = deviceClass === "temperature" ? -273.15 : 0) => {
213
227
  const member = find("sensor", deviceClass);
214
228
  if (!member)
215
229
  return;
216
230
  this.bindDiscoveredSensor(member.entityId, serviceType, characteristic, suffix, (state) => {
217
- const value = Number(state?.state);
218
- return Number.isFinite(value) ? Math.max(minimum, value) : minimum;
231
+ const raw = Number(state?.state);
232
+ const value = deviceClass === "temperature"
233
+ ? toCelsius(raw, state?.attributes?.unit_of_measurement)
234
+ : raw;
235
+ if (!Number.isFinite(value))
236
+ throw -70402;
237
+ return Math.max(minimum, value);
219
238
  });
220
239
  };
221
240
  // Only device members reported by HA are exported. No guessed sensor names,
@@ -417,6 +436,187 @@ export class HapGenericAccessory {
417
436
  if (outcome.failed)
418
437
  throw -70402;
419
438
  }
439
+ /** Attach the existing HAP service identities to HA without republishing. */
440
+ bindAdditionalHomeAssistantProfiles() {
441
+ const S = Service, C = Characteristic;
442
+ const bind = (type, characteristic, write) => {
443
+ const service = this.accessory.getService(type);
444
+ service?.getCharacteristic(characteristic).onSet(write);
445
+ };
446
+ const call = (service, data) => {
447
+ const domain = this.entityId.split(".")[0];
448
+ const targetService = domain === "valve"
449
+ ? service === "turn_on"
450
+ ? "open_valve"
451
+ : service === "turn_off"
452
+ ? "close_valve"
453
+ : service
454
+ : service;
455
+ return this.callHaWithResponseBudget(domain, targetService, data);
456
+ };
457
+ switch (this.record.hapProfile) {
458
+ case "lock_hap":
459
+ bind(S.LockMechanism, C.LockTargetState, (v) => call(v === 1 ? "lock" : "unlock"));
460
+ break;
461
+ case "window_covering_hap":
462
+ case "door_hap":
463
+ case "window_hap": {
464
+ const type = this.record.hapProfile === "door_hap"
465
+ ? S.Door
466
+ : this.record.hapProfile === "window_hap"
467
+ ? S.Window
468
+ : S.WindowCovering;
469
+ bind(type, C.TargetPosition, (v) => call("set_cover_position", { position: Number(v) }));
470
+ bind(type, C.HoldPosition, async (v) => {
471
+ if (v)
472
+ await call("stop_cover");
473
+ });
474
+ break;
475
+ }
476
+ case "garage_door":
477
+ bind(S.GarageDoorOpener, C.TargetDoorState, (v) => call(v === 0 ? "open_cover" : "close_cover"));
478
+ break;
479
+ case "air_purifier":
480
+ bind(S.AirPurifier, C.Active, (v) => call(v ? "turn_on" : "turn_off"));
481
+ break;
482
+ case "speaker_hap":
483
+ case "television_speaker": {
484
+ const type = this.record.hapProfile === "speaker_hap"
485
+ ? S.Speaker
486
+ : S.TelevisionSpeaker;
487
+ bind(type, C.Mute, (v) => call("volume_mute", { is_volume_muted: Boolean(v) }));
488
+ bind(type, C.Volume, (v) => call("volume_set", { volume_level: Number(v) / 100 }));
489
+ break;
490
+ }
491
+ case "irrigation_system":
492
+ bind(S.IrrigationSystem, C.Active, (v) => call(v ? "turn_on" : "turn_off"));
493
+ break;
494
+ case "dehumidifier": {
495
+ const svc = this.accessory.getService(S.HumidifierDehumidifier);
496
+ svc
497
+ ?.getCharacteristic(C.RelativeHumidityDehumidifierThreshold)
498
+ .onGet(() => Number(this.getHomeAssistantState(this.entityId)?.attributes?.humidity) || 0)
499
+ .onSet((v) => call("set_humidity", { humidity: Number(v) }));
500
+ break;
501
+ }
502
+ }
503
+ this.syncAdditionalHomeAssistantState(this.getHomeAssistantState(this.entityId));
504
+ }
505
+ syncAdditionalHomeAssistantState(state) {
506
+ if (!state || ["unknown", "unavailable"].includes(state.state))
507
+ return;
508
+ const S = Service, C = Characteristic;
509
+ const set = (type, characteristic, value) => this.accessory
510
+ .getService(type)
511
+ ?.updateCharacteristic(characteristic, value);
512
+ const on = state.state === "on", n = Number(state.state), attrs = state.attributes ?? {};
513
+ switch (this.record.hapProfile) {
514
+ case "lock_hap":
515
+ set(S.LockMechanism, C.LockCurrentState, state.state === "locked"
516
+ ? 1
517
+ : state.state === "unlocked" || state.state === "open"
518
+ ? 0
519
+ : state.state === "jammed"
520
+ ? 2
521
+ : 3);
522
+ if (state.state === "locked" || state.state === "unlocked")
523
+ set(S.LockMechanism, C.LockTargetState, state.state === "locked" ? 1 : 0);
524
+ break;
525
+ case "window_covering_hap":
526
+ case "door_hap":
527
+ case "window_hap": {
528
+ const type = this.record.hapProfile === "door_hap"
529
+ ? S.Door
530
+ : this.record.hapProfile === "window_hap"
531
+ ? S.Window
532
+ : S.WindowCovering;
533
+ const pos = Number(attrs.current_position);
534
+ if (Number.isFinite(pos)) {
535
+ set(type, C.CurrentPosition, pos);
536
+ if (!["opening", "closing"].includes(state.state))
537
+ set(type, C.TargetPosition, pos);
538
+ }
539
+ set(type, C.PositionState, state.state === "opening" ? 1 : state.state === "closing" ? 0 : 2);
540
+ break;
541
+ }
542
+ case "garage_door":
543
+ set(S.GarageDoorOpener, C.CurrentDoorState, { open: 0, closed: 1, opening: 2, closing: 3 }[state.state] ?? 4);
544
+ if (["open", "closed"].includes(state.state))
545
+ set(S.GarageDoorOpener, C.TargetDoorState, state.state === "open" ? 0 : 1);
546
+ set(S.GarageDoorOpener, C.ObstructionDetected, Boolean(attrs.obstruction_detected));
547
+ break;
548
+ case "air_purifier":
549
+ set(S.AirPurifier, C.Active, on ? 1 : 0);
550
+ set(S.AirPurifier, C.CurrentAirPurifierState, on ? 2 : 0);
551
+ break;
552
+ case "irrigation_system":
553
+ set(S.IrrigationSystem, C.Active, on || ["open", "opening"].includes(state.state) ? 1 : 0);
554
+ set(S.IrrigationSystem, C.InUse, on || state.state === "open" ? 1 : 0);
555
+ break;
556
+ case "speaker_hap":
557
+ case "television_speaker": {
558
+ const type = this.record.hapProfile === "speaker_hap"
559
+ ? S.Speaker
560
+ : S.TelevisionSpeaker;
561
+ set(type, C.Mute, Boolean(attrs.is_volume_muted));
562
+ if (Number.isFinite(attrs.volume_level))
563
+ set(type, C.Volume, Math.round(attrs.volume_level * 100));
564
+ break;
565
+ }
566
+ case "temperature_sensor_hap":
567
+ if (Number.isFinite(n))
568
+ set(S.TemperatureSensor, C.CurrentTemperature, toCelsius(n, attrs.unit_of_measurement));
569
+ break;
570
+ case "humidity_sensor_hap":
571
+ if (Number.isFinite(n))
572
+ set(S.HumiditySensor, C.CurrentRelativeHumidity, n);
573
+ break;
574
+ case "light_sensor_hap":
575
+ if (Number.isFinite(n))
576
+ set(S.LightSensor, C.CurrentAmbientLightLevel, Math.max(0.0001, n));
577
+ break;
578
+ case "motion_sensor_hap":
579
+ set(S.MotionSensor, C.MotionDetected, on);
580
+ break;
581
+ case "contact_sensor_hap":
582
+ set(S.ContactSensor, C.ContactSensorState, on ? 1 : 0);
583
+ break;
584
+ case "occupancy_sensor_hap":
585
+ set(S.OccupancySensor, C.OccupancyDetected, on ? 1 : 0);
586
+ break;
587
+ case "smoke_sensor_hap":
588
+ set(S.SmokeSensor, C.SmokeDetected, on ? 1 : 0);
589
+ break;
590
+ case "carbon_monoxide_sensor_hap":
591
+ set(S.CarbonMonoxideSensor, C.CarbonMonoxideDetected, on ? 1 : 0);
592
+ break;
593
+ case "carbon_dioxide_sensor_hap":
594
+ if (Number.isFinite(n)) {
595
+ set(S.CarbonDioxideSensor, C.CarbonDioxideLevel, n);
596
+ set(S.CarbonDioxideSensor, C.CarbonDioxideDetected, n > 1000 ? 1 : 0);
597
+ }
598
+ break;
599
+ case "leak_sensor_hap":
600
+ set(S.LeakSensor, C.LeakDetected, on ? 1 : 0);
601
+ break;
602
+ case "air_quality_sensor_hap":
603
+ set(S.AirQualitySensor, C.AirQuality, sensorConverter.toAirQuality(state));
604
+ break;
605
+ case "battery_hap":
606
+ if (Number.isFinite(n)) {
607
+ set(S.Battery, C.BatteryLevel, Math.max(0, Math.min(100, n)));
608
+ set(S.Battery, C.StatusLowBattery, n <= 20 ? 1 : 0);
609
+ }
610
+ break;
611
+ case "doorbell":
612
+ if (on)
613
+ set(S.Doorbell, C.ProgrammableSwitchEvent, 0);
614
+ break;
615
+ }
616
+ if (this.record.hapProfile === "dehumidifier" &&
617
+ Number.isFinite(attrs.humidity))
618
+ set(S.HumidifierDehumidifier, C.RelativeHumidityDehumidifierThreshold, attrs.humidity);
619
+ }
420
620
  bindPrimaryHomeAssistantEntity() {
421
621
  const [domain] = this.entityId.split(".");
422
622
  const state = () => this.platform.entities.get(this.entityId)?.state;
@@ -445,6 +645,31 @@ export class HapGenericAccessory {
445
645
  .onSet(async (value) => this.callHaWithResponseBudget("light", "turn_on", {
446
646
  brightness_pct: Number(value),
447
647
  }));
648
+ const attrs = state()?.attributes ?? {};
649
+ if (attrs.supported_color_modes?.includes("color_temp") ||
650
+ attrs.color_temp_kelvin !== undefined) {
651
+ const temperature = service.getCharacteristic(Characteristic.ColorTemperature);
652
+ const minimum = Math.ceil(1_000_000 / Number(attrs.max_color_temp_kelvin ?? 6500));
653
+ const maximum = Math.floor(1_000_000 / Number(attrs.min_color_temp_kelvin ?? 2000));
654
+ temperature.setProps({
655
+ minValue: minimum,
656
+ maxValue: maximum,
657
+ minStep: 1,
658
+ });
659
+ temperature
660
+ .onGet(() => {
661
+ const a = state()?.attributes ?? {};
662
+ const mireds = a.color_temp_kelvin
663
+ ? Math.round(1_000_000 / a.color_temp_kelvin)
664
+ : a.color_temp;
665
+ if (!Number.isFinite(mireds))
666
+ throw -70402;
667
+ return mireds;
668
+ })
669
+ .onSet(async (value) => this.callHaWithResponseBudget("light", "turn_on", {
670
+ color_temp_kelvin: Math.round(1_000_000 / Number(value)),
671
+ }));
672
+ }
448
673
  const color = () => state()?.attributes?.hs_color;
449
674
  if (Array.isArray(color())) {
450
675
  service
@@ -453,17 +678,20 @@ export class HapGenericAccessory {
453
678
  service
454
679
  .getCharacteristic(Characteristic.Saturation)
455
680
  .onGet(() => Number(color()?.[1]) || 0);
456
- const setColor = async () => this.callHaWithResponseBudget("light", "turn_on", {
457
- hs_color: [
458
- Number(service.getCharacteristic(Characteristic.Hue).value || 0),
459
- Number(service.getCharacteristic(Characteristic.Saturation)
460
- .value || 0),
461
- ],
462
- });
463
- service.getCharacteristic(Characteristic.Hue).onSet(setColor);
681
+ let lastHs = [Number(color()?.[0]) || 0, Number(color()?.[1]) || 0];
682
+ const writeHs = async (index, value) => {
683
+ lastHs = [...lastHs];
684
+ lastHs[index] = Number(value);
685
+ await this.callHaWithResponseBudget("light", "turn_on", {
686
+ hs_color: [...lastHs],
687
+ });
688
+ };
689
+ service
690
+ .getCharacteristic(Characteristic.Hue)
691
+ .onSet((value) => writeHs(0, value));
464
692
  service
465
693
  .getCharacteristic(Characteristic.Saturation)
466
- .onSet(setColor);
694
+ .onSet((value) => writeHs(1, value));
467
695
  }
468
696
  }
469
697
  break;
@@ -506,6 +734,227 @@ export class HapGenericAccessory {
506
734
  });
507
735
  break;
508
736
  }
737
+ case "heater_cooler": {
738
+ const service = this.accessory.getService(Service.HeaterCooler);
739
+ if (!service)
740
+ break;
741
+ service
742
+ .getCharacteristic(Characteristic.Active)
743
+ .onGet(() => {
744
+ const s = state();
745
+ return s?.state &&
746
+ s.state !== "off" &&
747
+ s.state !== "unavailable" &&
748
+ s.state !== "unknown"
749
+ ? 1
750
+ : 0;
751
+ })
752
+ .onSet(async (value) => {
753
+ if (value === 1) {
754
+ await this.callHaWithResponseBudget("climate", "turn_on").catch(async () => {
755
+ await this.callHaWithResponseBudget("climate", "set_hvac_mode", {
756
+ hvac_mode: climateConverter.supportedMode(state()?.attributes?.hvac_modes?.find((mode) => mode !== "off") ?? "cool", state()?.attributes?.hvac_modes),
757
+ });
758
+ });
759
+ }
760
+ else {
761
+ await this.callHaWithResponseBudget("climate", "set_hvac_mode", {
762
+ hvac_mode: "off",
763
+ });
764
+ }
765
+ });
766
+ service
767
+ .getCharacteristic(Characteristic.CurrentHeaterCoolerState)
768
+ .onGet(() => {
769
+ const s = state();
770
+ if (!s ||
771
+ s.state === "off" ||
772
+ s.state === "unavailable" ||
773
+ s.state === "unknown")
774
+ return 0;
775
+ const action = s.attributes?.hvac_action;
776
+ if (action === "cooling")
777
+ return 3;
778
+ if (action === "heating")
779
+ return 2;
780
+ if (action === "idle")
781
+ return 1;
782
+ if (s.state === "cool")
783
+ return 3;
784
+ if (s.state === "heat")
785
+ return 2;
786
+ return 1;
787
+ });
788
+ const target = service.getCharacteristic(Characteristic.TargetHeaterCoolerState);
789
+ const modes = state()?.attributes?.hvac_modes;
790
+ if (Array.isArray(modes)) {
791
+ const supported = [
792
+ ...(modes.includes("auto") || modes.includes("heat_cool")
793
+ ? [0]
794
+ : []),
795
+ ...(modes.includes("heat") ? [1] : []),
796
+ ...(modes.includes("cool") ? [2] : []),
797
+ ];
798
+ if (supported.length)
799
+ target.setProps({ validValues: supported });
800
+ }
801
+ target
802
+ .onGet(() => {
803
+ const s = state();
804
+ const mode = s?.state || s?.attributes?.hvac_mode;
805
+ if (mode === "heat")
806
+ return 1;
807
+ if (mode === "cool")
808
+ return 2;
809
+ return target.props.validValues?.[0] ?? 0;
810
+ })
811
+ .onSet(async (value) => {
812
+ let hvac_mode = "auto";
813
+ if (value === 1)
814
+ hvac_mode = "heat";
815
+ else if (value === 2)
816
+ hvac_mode = "cool";
817
+ await this.callHaWithResponseBudget("climate", "set_hvac_mode", {
818
+ hvac_mode: climateConverter.supportedMode(hvac_mode, state()?.attributes?.hvac_modes, state()?.state),
819
+ });
820
+ });
821
+ service
822
+ .getCharacteristic(Characteristic.CurrentTemperature)
823
+ .onGet(() => {
824
+ const current = Number(state()?.attributes?.current_temperature);
825
+ return Number.isFinite(current)
826
+ ? toCelsius(current, state()?.attributes?.temperature_unit)
827
+ : 21;
828
+ });
829
+ service
830
+ .getCharacteristic(Characteristic.CoolingThresholdTemperature)
831
+ .onGet(() => {
832
+ const current = state();
833
+ const rangeTarget = current?.attributes?.target_temp_high;
834
+ const temp = Number(current?.state === "heat_cool"
835
+ ? rangeTarget
836
+ : (current?.attributes?.temperature ?? rangeTarget));
837
+ return Number.isFinite(temp)
838
+ ? toCelsius(temp, state()?.attributes?.temperature_unit)
839
+ : 22;
840
+ })
841
+ .onSet(async (value) => {
842
+ await this.callHaWithResponseBudget("climate", "set_temperature", climateConverter.targetTemperature(state() ?? { state: "unknown" }, "cool", Number(value)));
843
+ });
844
+ service
845
+ .getCharacteristic(Characteristic.HeatingThresholdTemperature)
846
+ .onGet(() => {
847
+ const current = state();
848
+ const rangeTarget = current?.attributes?.target_temp_low;
849
+ const temp = Number(current?.state === "heat_cool"
850
+ ? rangeTarget
851
+ : (current?.attributes?.temperature ?? rangeTarget));
852
+ return Number.isFinite(temp)
853
+ ? toCelsius(temp, state()?.attributes?.temperature_unit)
854
+ : 20;
855
+ })
856
+ .onSet(async (value) => {
857
+ await this.callHaWithResponseBudget("climate", "set_temperature", climateConverter.targetTemperature(state() ?? { state: "unknown" }, "heat", Number(value)));
858
+ });
859
+ service
860
+ .getCharacteristic(Characteristic.TemperatureDisplayUnits)
861
+ .onGet(() => state()?.attributes?.temperature_unit === "°F" ? 1 : 0);
862
+ service
863
+ .getCharacteristic(Characteristic.RotationSpeed)
864
+ .onGet(() => this.getClimateFanSpeedPercentage(state()))
865
+ .onSet(async (value) => {
866
+ const fanMode = this.climateFanPercentageToMode(Number(value), state());
867
+ if (fanMode) {
868
+ await this.callHaWithResponseBudget("climate", "set_fan_mode", {
869
+ fan_mode: fanMode,
870
+ });
871
+ }
872
+ });
873
+ service
874
+ .getCharacteristic(Characteristic.SwingMode)
875
+ .onGet(() => {
876
+ const swing = state()?.attributes?.swing_mode;
877
+ return swing && swing !== "off" && swing !== "stop" ? 1 : 0;
878
+ })
879
+ .onSet(async (value) => {
880
+ const swingMode = this.resolveClimateSwingMode(value === 1, state());
881
+ if (swingMode) {
882
+ await this.callHaWithResponseBudget("climate", "set_swing_mode", {
883
+ swing_mode: swingMode,
884
+ });
885
+ }
886
+ });
887
+ break;
888
+ }
889
+ case "thermostat_hap": {
890
+ const service = this.accessory.getService(Service.Thermostat);
891
+ if (!service)
892
+ break;
893
+ service
894
+ .getCharacteristic(Characteristic.CurrentTemperature)
895
+ .onGet(() => {
896
+ const current = Number(state()?.attributes?.current_temperature);
897
+ return Number.isFinite(current)
898
+ ? toCelsius(current, state()?.attributes?.temperature_unit)
899
+ : 21;
900
+ });
901
+ service
902
+ .getCharacteristic(Characteristic.TargetTemperature)
903
+ .onGet(() => {
904
+ const temp = Number(state()?.attributes?.temperature);
905
+ return Number.isFinite(temp)
906
+ ? toCelsius(temp, state()?.attributes?.temperature_unit)
907
+ : 22;
908
+ })
909
+ .onSet(async (value) => {
910
+ await this.callHaWithResponseBudget("climate", "set_temperature", {
911
+ temperature: fromCelsius(Number(value), state()?.attributes?.temperature_unit),
912
+ });
913
+ });
914
+ service
915
+ .getCharacteristic(Characteristic.TargetHeatingCoolingState)
916
+ .onGet(() => {
917
+ const mode = state()?.state;
918
+ if (mode === "off")
919
+ return 0;
920
+ if (mode === "heat")
921
+ return 1;
922
+ if (mode === "cool")
923
+ return 2;
924
+ return 3; // AUTO
925
+ })
926
+ .onSet(async (value) => {
927
+ let hvac_mode = "off";
928
+ if (value === 1)
929
+ hvac_mode = "heat";
930
+ else if (value === 2)
931
+ hvac_mode = "cool";
932
+ else if (value === 3)
933
+ hvac_mode = "auto";
934
+ await this.callHaWithResponseBudget("climate", "set_hvac_mode", {
935
+ hvac_mode: climateConverter.supportedMode(hvac_mode, state()?.attributes?.hvac_modes, state()?.state),
936
+ });
937
+ });
938
+ service
939
+ .getCharacteristic(Characteristic.CurrentHeatingCoolingState)
940
+ .onGet(() => {
941
+ const action = state()?.attributes?.hvac_action;
942
+ if (action === "heating")
943
+ return 1;
944
+ if (action === "cooling")
945
+ return 2;
946
+ const mode = state()?.state;
947
+ if (mode === "heat")
948
+ return 1;
949
+ if (mode === "cool")
950
+ return 2;
951
+ return 0; // OFF
952
+ });
953
+ service
954
+ .getCharacteristic(Characteristic.TemperatureDisplayUnits)
955
+ .onGet(() => state()?.attributes?.temperature_unit === "°F" ? 1 : 0);
956
+ break;
957
+ }
509
958
  case "fan_hap": {
510
959
  const service = this.accessory.getService(Service.Fanv2);
511
960
  if (service) {
@@ -603,13 +1052,12 @@ export class HapGenericAccessory {
603
1052
  const current = this.getCurrentHumidity();
604
1053
  if (current !== undefined)
605
1054
  return current;
606
- const target = Number(this.getHomeAssistantState(this.entityId)?.attributes?.humidity);
607
- return Number.isFinite(target) && target >= 0 && target <= 100
608
- ? target
609
- : 0;
1055
+ throw -70402; // A target is not a measured humidity value.
610
1056
  });
611
1057
  svc
612
- .getCharacteristic(Characteristic.RelativeHumidityHumidifierThreshold)
1058
+ .getCharacteristic(profile === "dehumidifier"
1059
+ ? Characteristic.RelativeHumidityDehumidifierThreshold
1060
+ : Characteristic.RelativeHumidityHumidifierThreshold)
613
1061
  .onGet(() => {
614
1062
  const value = Number(this.platform.entities.get(this.entityId)?.state?.attributes
615
1063
  ?.humidity);
@@ -619,7 +1067,9 @@ export class HapGenericAccessory {
619
1067
  })
620
1068
  .onSet(async (value) => {
621
1069
  const [domain] = this.entityId.split(".");
622
- svc.updateCharacteristic(Characteristic.RelativeHumidityHumidifierThreshold, Number(value));
1070
+ svc.updateCharacteristic(profile === "dehumidifier"
1071
+ ? Characteristic.RelativeHumidityDehumidifierThreshold
1072
+ : Characteristic.RelativeHumidityHumidifierThreshold, Number(value));
623
1073
  await this.callHaWithResponseBudget(domain, "set_humidity", {
624
1074
  humidity: Number(value),
625
1075
  });
@@ -877,7 +1327,7 @@ export class HapGenericAccessory {
877
1327
  svc.getCharacteristic(Characteristic.Active).setValue(0);
878
1328
  break;
879
1329
  }
880
- // ── Calefactor / Aire ─────────────────────────────────────────────────
1330
+ // ── Aire Acondicionado / Calefactor (HeaterCooler) ───────────────────
881
1331
  case "heater_cooler": {
882
1332
  const svc = this.accessory.addService(Service.HeaterCooler, this.record.name);
883
1333
  svc.getCharacteristic(Characteristic.Active).setValue(0);
@@ -888,6 +1338,25 @@ export class HapGenericAccessory {
888
1338
  .getCharacteristic(Characteristic.TargetHeaterCoolerState)
889
1339
  .setValue(0);
890
1340
  svc.getCharacteristic(Characteristic.CurrentTemperature).setValue(20);
1341
+ const cooling = svc.getCharacteristic(Characteristic.CoolingThresholdTemperature) ||
1342
+ svc.addCharacteristic(Characteristic.CoolingThresholdTemperature);
1343
+ cooling
1344
+ .setValue(22)
1345
+ .setProps({ minValue: 10, maxValue: 35, minStep: 0.5 });
1346
+ const heating = svc.getCharacteristic(Characteristic.HeatingThresholdTemperature) ||
1347
+ svc.addCharacteristic(Characteristic.HeatingThresholdTemperature);
1348
+ heating
1349
+ .setValue(20)
1350
+ .setProps({ minValue: 0, maxValue: 35, minStep: 0.5 });
1351
+ const units = svc.getCharacteristic(Characteristic.TemperatureDisplayUnits) ||
1352
+ svc.addCharacteristic(Characteristic.TemperatureDisplayUnits);
1353
+ units.setValue(Characteristic.TemperatureDisplayUnits.CELSIUS);
1354
+ const speed = svc.getCharacteristic(Characteristic.RotationSpeed) ||
1355
+ svc.addCharacteristic(Characteristic.RotationSpeed);
1356
+ speed.setProps({ minValue: 0, maxValue: 100, minStep: 1 }).setValue(0);
1357
+ const swing = svc.getCharacteristic(Characteristic.SwingMode) ||
1358
+ svc.addCharacteristic(Characteristic.SwingMode);
1359
+ swing.setValue(Characteristic.SwingMode.SWING_DISABLED);
891
1360
  break;
892
1361
  }
893
1362
  // ── Termostato HAP ────────────────────────────────────────────────────
@@ -1211,12 +1680,14 @@ export class HapGenericAccessory {
1211
1680
  }
1212
1681
  }
1213
1682
  const binding = this.discoveredSensorBindings.get(sourceEntityId);
1214
- if (binding) {
1683
+ if (binding && !["unknown", "unavailable"].includes(state?.state)) {
1215
1684
  binding.service.updateCharacteristic(binding.characteristic, binding.read(state));
1216
1685
  }
1217
1686
  return;
1218
1687
  }
1219
1688
  this.setReachability(state?.state !== "unavailable" && state?.state !== "unknown");
1689
+ if (!state || ["unknown", "unavailable"].includes(state.state))
1690
+ return;
1220
1691
  if (this.record.hapProfile === "security_system") {
1221
1692
  this.syncAlarmState(state);
1222
1693
  return;
@@ -1225,6 +1696,7 @@ export class HapGenericAccessory {
1225
1696
  this.syncValveState(state);
1226
1697
  return;
1227
1698
  }
1699
+ this.syncAdditionalHomeAssistantState(state);
1228
1700
  const on = state?.state === "on";
1229
1701
  switch (this.record.hapProfile) {
1230
1702
  case "switch_hap":
@@ -1242,6 +1714,9 @@ export class HapGenericAccessory {
1242
1714
  light?.updateCharacteristic(Characteristic.On, on);
1243
1715
  if (state?.attributes?.brightness !== undefined)
1244
1716
  light?.updateCharacteristic(Characteristic.Brightness, Math.round((Number(state.attributes.brightness) * 100) / 255));
1717
+ if (state?.attributes?.color_temp_kelvin &&
1718
+ light?.testCharacteristic(Characteristic.ColorTemperature))
1719
+ light.updateCharacteristic(Characteristic.ColorTemperature, Math.round(1_000_000 / state.attributes.color_temp_kelvin));
1245
1720
  if (Array.isArray(state?.attributes?.hs_color)) {
1246
1721
  light?.updateCharacteristic(Characteristic.Hue, Number(state.attributes.hs_color[0]) || 0);
1247
1722
  light?.updateCharacteristic(Characteristic.Saturation, Number(state.attributes.hs_color[1]) || 0);
@@ -1292,6 +1767,105 @@ export class HapGenericAccessory {
1292
1767
  }
1293
1768
  break;
1294
1769
  }
1770
+ case "heater_cooler": {
1771
+ const svc = this.accessory.getService(Service.HeaterCooler);
1772
+ if (!svc)
1773
+ break;
1774
+ const isActive = state?.state &&
1775
+ state.state !== "off" &&
1776
+ state.state !== "unavailable" &&
1777
+ state.state !== "unknown";
1778
+ svc.updateCharacteristic(Characteristic.Active, isActive ? 1 : 0);
1779
+ const action = state?.attributes?.hvac_action;
1780
+ let currentState = 0; // INACTIVE
1781
+ if (isActive) {
1782
+ if (action === "cooling")
1783
+ currentState = 3;
1784
+ else if (action === "heating")
1785
+ currentState = 2;
1786
+ else if (action === "idle")
1787
+ currentState = 1;
1788
+ else if (state.state === "cool")
1789
+ currentState = 3;
1790
+ else if (state.state === "heat")
1791
+ currentState = 2;
1792
+ else
1793
+ currentState = 1;
1794
+ }
1795
+ svc.updateCharacteristic(Characteristic.CurrentHeaterCoolerState, currentState);
1796
+ const mode = state?.state || state?.attributes?.hvac_mode;
1797
+ const target = svc.getCharacteristic(Characteristic.TargetHeaterCoolerState);
1798
+ let targetState = target.props.validValues?.[0] ?? 0;
1799
+ if (mode === "heat")
1800
+ targetState = 1;
1801
+ else if (mode === "cool")
1802
+ targetState = 2;
1803
+ svc.updateCharacteristic(Characteristic.TargetHeaterCoolerState, targetState);
1804
+ const currentTemp = toCelsius(Number(state?.attributes?.current_temperature), state?.attributes?.temperature_unit);
1805
+ if (Number.isFinite(currentTemp)) {
1806
+ svc.updateCharacteristic(Characteristic.CurrentTemperature, currentTemp);
1807
+ }
1808
+ const targetTemp = state?.state === "heat_cool"
1809
+ ? NaN
1810
+ : Number(state?.attributes?.temperature);
1811
+ if (Number.isFinite(targetTemp)) {
1812
+ svc.updateCharacteristic(Characteristic.CoolingThresholdTemperature, toCelsius(targetTemp, state?.attributes?.temperature_unit));
1813
+ svc.updateCharacteristic(Characteristic.HeatingThresholdTemperature, toCelsius(targetTemp, state?.attributes?.temperature_unit));
1814
+ }
1815
+ else {
1816
+ if (Number.isFinite(Number(state?.attributes?.target_temp_low))) {
1817
+ svc.updateCharacteristic(Characteristic.HeatingThresholdTemperature, toCelsius(Number(state.attributes.target_temp_low), state.attributes.temperature_unit));
1818
+ }
1819
+ if (Number.isFinite(Number(state?.attributes?.target_temp_high))) {
1820
+ svc.updateCharacteristic(Characteristic.CoolingThresholdTemperature, toCelsius(Number(state.attributes.target_temp_high), state.attributes.temperature_unit));
1821
+ }
1822
+ }
1823
+ svc.updateCharacteristic(Characteristic.RotationSpeed, this.getClimateFanSpeedPercentage(state));
1824
+ const swing = state?.attributes?.swing_mode;
1825
+ svc.updateCharacteristic(Characteristic.SwingMode, swing && swing !== "off" && swing !== "stop" ? 1 : 0);
1826
+ break;
1827
+ }
1828
+ case "thermostat_hap": {
1829
+ const svc = this.accessory.getService(Service.Thermostat);
1830
+ if (!svc)
1831
+ break;
1832
+ const isOff = state?.state === "off" ||
1833
+ state?.state === "unavailable" ||
1834
+ state?.state === "unknown";
1835
+ const mode = state?.state || state?.attributes?.hvac_mode;
1836
+ let targetHeatingCooling = 0; // OFF
1837
+ if (!isOff) {
1838
+ if (mode === "heat")
1839
+ targetHeatingCooling = 1;
1840
+ else if (mode === "cool")
1841
+ targetHeatingCooling = 2;
1842
+ else if (mode === "auto" || mode === "heat_cool")
1843
+ targetHeatingCooling = 3;
1844
+ }
1845
+ svc.updateCharacteristic(Characteristic.TargetHeatingCoolingState, targetHeatingCooling);
1846
+ const action = state?.attributes?.hvac_action;
1847
+ let currentHeatingCooling = 0;
1848
+ if (!isOff) {
1849
+ if (action === "heating")
1850
+ currentHeatingCooling = 1;
1851
+ else if (action === "cooling")
1852
+ currentHeatingCooling = 2;
1853
+ else if (mode === "heat")
1854
+ currentHeatingCooling = 1;
1855
+ else if (mode === "cool")
1856
+ currentHeatingCooling = 2;
1857
+ }
1858
+ svc.updateCharacteristic(Characteristic.CurrentHeatingCoolingState, currentHeatingCooling);
1859
+ const currentTemp = toCelsius(Number(state?.attributes?.current_temperature), state?.attributes?.temperature_unit);
1860
+ if (Number.isFinite(currentTemp)) {
1861
+ svc.updateCharacteristic(Characteristic.CurrentTemperature, currentTemp);
1862
+ }
1863
+ const targetTemp = Number(state?.attributes?.temperature);
1864
+ if (Number.isFinite(targetTemp)) {
1865
+ svc.updateCharacteristic(Characteristic.TargetTemperature, toCelsius(targetTemp, state?.attributes?.temperature_unit));
1866
+ }
1867
+ break;
1868
+ }
1295
1869
  }
1296
1870
  if (this.record.hapProfile === "humidifier" ||
1297
1871
  this.record.hapProfile === "dehumidifier") {
@@ -1306,7 +1880,9 @@ export class HapGenericAccessory {
1306
1880
  }
1307
1881
  const targetHum = Number(state?.attributes?.humidity);
1308
1882
  if (!isNaN(targetHum) && targetHum >= 0 && targetHum <= 100) {
1309
- svc.updateCharacteristic(Characteristic.RelativeHumidityHumidifierThreshold, targetHum);
1883
+ svc.updateCharacteristic(this.record.hapProfile === "dehumidifier"
1884
+ ? Characteristic.RelativeHumidityDehumidifierThreshold
1885
+ : Characteristic.RelativeHumidityHumidifierThreshold, targetHum);
1310
1886
  }
1311
1887
  }
1312
1888
  }
@@ -1685,6 +2261,45 @@ export class HapGenericAccessory {
1685
2261
  throw error;
1686
2262
  }
1687
2263
  }
2264
+ getClimateFanSpeedPercentage(s) {
2265
+ if (!s ||
2266
+ s.state === "off" ||
2267
+ s.state === "unavailable" ||
2268
+ s.state === "unknown")
2269
+ return 0;
2270
+ const currentMode = String(s.attributes?.fan_mode || "").toLowerCase();
2271
+ const modes = s.attributes?.fan_modes || [];
2272
+ if (modes.length === 0) {
2273
+ if (s.attributes?.percentage !== undefined)
2274
+ return Number(s.attributes.percentage) || 0;
2275
+ return currentMode === "off" ? 0 : 100;
2276
+ }
2277
+ const idx = modes.findIndex((m) => m.toLowerCase() === currentMode);
2278
+ if (idx === -1)
2279
+ return 50;
2280
+ return Math.round(((idx + 1) / modes.length) * 100);
2281
+ }
2282
+ climateFanPercentageToMode(percentage, s) {
2283
+ const modes = s?.attributes?.fan_modes || [];
2284
+ if (modes.length === 0)
2285
+ return undefined;
2286
+ if (percentage <= 0) {
2287
+ const offMode = modes.find((m) => /off/i.test(m));
2288
+ if (offMode)
2289
+ return offMode;
2290
+ }
2291
+ const index = Math.min(modes.length - 1, Math.max(0, Math.floor((percentage / 100) * modes.length)));
2292
+ return modes[index];
2293
+ }
2294
+ resolveClimateSwingMode(enable, s) {
2295
+ const modes = s?.attributes?.swing_modes || [];
2296
+ if (modes.length === 0)
2297
+ return enable ? "on" : "off";
2298
+ if (!enable) {
2299
+ return modes.find((m) => /off|stop/i.test(m)) || "off";
2300
+ }
2301
+ return modes.find((m) => !/off|stop/i.test(m)) || modes[0];
2302
+ }
1688
2303
  /**
1689
2304
  * HomeKit accessories are independently reachable from the Matter bridge.
1690
2305
  * Mirror Home Assistant availability here so Casa does not leave a paired