1 contributor
134 lines | 5.976kb
<script type="text/x-red" data-template-name="zg-204zv-homekit-adapter">
  <div class="form-row">
    <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
    <input type="text" id="node-input-name" placeholder="Name">
  </div>
  <div class="form-row">
    <label for="node-input-site"><i class="fa fa-globe"></i> Site</label>
    <input type="text" id="node-input-site" placeholder="vad">
  </div>
  <div class="form-row">
    <label for="node-input-location"><i class="fa fa-home"></i> Location</label>
    <input type="text" id="node-input-location" placeholder="balcon">
  </div>
  <div class="form-row">
    <label for="node-input-accessory"><i class="fa fa-dot-circle-o"></i> Accessory</label>
    <input type="text" id="node-input-accessory" placeholder="south">
  </div>
  <div class="form-row">
    <label for="node-input-batteryLowThreshold">Battery low threshold (%)</label>
    <input type="number" id="node-input-batteryLowThreshold" min="0" max="100" placeholder="20">
  </div>
  <div class="form-row">
    <label for="node-input-occupancyFadingTimeSec">Occupancy fading time (s)</label>
    <input type="number" id="node-input-occupancyFadingTimeSec" min="0" step="1" placeholder="300">
  </div>
</script>

<script type="text/x-red" data-help-name="zg-204zv-homekit-adapter">
  <p>
    Consumes HomeBus telemetry for a single <code>ZG-204ZV</code> endpoint and projects it to HomeKit services.
  </p>
  <p>
    Output 7 is not a publish stream anymore. It controls a Node-RED <code>mqtt in</code> node with dynamic subscriptions.
  </p>
  <p>
    On start, the node emits:
    <code>subscribe &lt;site&gt;/home/&lt;location&gt;/+/&lt;accessory&gt;/last</code> and
    <code>subscribe &lt;site&gt;/home/&lt;location&gt;/+/&lt;accessory&gt;/value</code>.
  </p>
  <p>
    The node keeps <code>.../last</code> subscribed until the cold-start bootstrap is complete for all required values.
    A value is considered satisfied when it arrived either from <code>last</code> or from live <code>value</code>.
    Only then does the node emit <code>unsubscribe .../last</code>.
  </p>
  <p>
    Supported input topic shape:
    <code>&lt;site&gt;/home/&lt;location&gt;/&lt;capability&gt;/&lt;accessory&gt;/value</code> and
    <code>&lt;site&gt;/home/&lt;location&gt;/&lt;capability&gt;/&lt;accessory&gt;/last</code>.
  </p>
  <p>
    The node accepts scalar <code>value</code> payloads and timestamped <code>last</code> envelopes of the form
    <code>{ value, observed_at, quality }</code>. For <code>last</code>, only <code>payload.value</code> is used.
  </p>
  <h3>Capabilities</h3>
  <p>
    Mappings:
    <code>motion -&gt; Motion + Occupancy</code>,
    <code>temperature -&gt; Temperature</code>,
    <code>humidity -&gt; Humidity</code>,
    <code>illuminance -&gt; Light Level</code>,
    <code>battery + battery_low -&gt; Battery</code>.
  </p>
  <p>
    Occupancy is synthesized locally from <code>motion</code> and the configured <code>Occupancy fading time</code>.
    A <code>motion=true</code> sample sets occupancy, while <code>motion=false</code> only clears the Motion service immediately;
    Occupancy clears when the local timer expires.
  </p>
  <p>
    Subscription topics are generated from the configured <code>site</code>, <code>location</code>, and
    <code>accessory</code>. The bus segment remains fixed to <code>home</code>.
  </p>
  <p>
    For compatibility with existing flows, legacy accessory ids such as <code>radar-south</code> are normalized to
    <code>south</code>.
  </p>
  <h3>Outputs</h3>
  <ol>
    <li>Motion Sensor: <code>{ MotionDetected, StatusActive, StatusFault, StatusLowBattery, StatusTampered }</code></li>
    <li>Occupancy Sensor: <code>{ OccupancyDetected, StatusActive, StatusFault, StatusLowBattery, StatusTampered }</code></li>
    <li>Temperature Sensor: <code>{ CurrentTemperature }</code></li>
    <li>Humidity Sensor: <code>{ CurrentRelativeHumidity }</code></li>
    <li>Light Sensor: <code>{ CurrentAmbientLightLevel }</code></li>
    <li>Battery Service: <code>{ StatusLowBattery, BatteryLevel, ChargingState }</code></li>
    <li><code>mqtt in</code> dynamic control messages: <code>{ action, topic }</code></li>
  </ol>
  <h3>HomeKit Service Setup</h3>
  <p>
    The linked <code>homekit-service</code> nodes should explicitly materialize the optional characteristics used by this adapter.
  </p>
  <p>
    Recommended <code>characteristicProperties</code> values:
  </p>
  <p><code>Battery</code></p>
  <pre><code>{"BatteryLevel":{},"ChargingState":{}}</code></pre>
  <p><code>Motion</code></p>
  <pre><code>{"StatusActive":{},"StatusFault":{},"StatusLowBattery":{},"StatusTampered":{}}</code></pre>
  <p><code>Occupancy</code></p>
  <pre><code>{"StatusActive":{},"StatusFault":{},"StatusLowBattery":{},"StatusTampered":{}}</code></pre>
  <p>
    Temperature, Humidity, and Light Level currently publish only their main reading, so no extra optional characteristics are required for those services.
  </p>
</script>

<script>
  function requiredText(v) {
    return !!(v && String(v).trim());
  }

  RED.nodes.registerType("zg-204zv-homekit-adapter", {
    category: "myNodes",
    color: "#d7f0d1",
    defaults: {
      name: { value: "" },
      outputTopic: { value: "" },
      mqttBus: { value: "" },
      site: { value: "", validate: requiredText },
      location: { value: "", validate: requiredText },
      accessory: { value: "", validate: requiredText },
      mqttSite: { value: "" },
      mqttRoom: { value: "" },
      mqttSensor: { value: "" },
      batteryLowThreshold: { value: 20, validate: RED.validators.number() },
      occupancyFadingTimeSec: { value: 300, validate: RED.validators.number() },
      publishCacheWindowSec: { value: "" }
    },
    inputs: 1,
    outputs: 7,
    icon: "font-awesome/fa-eye",
    label: function() {
      return this.name || "zg-204zv-homekit-adapter";
    },
    paletteLabel: "zg-204zv-homekit-adapter"
  });
</script>