# ═══════════════════════════════════════════════════════════════
#  Water Quality Box — HA Calibration Helpers + Calibrated Sensors
#  RACKSYNC CO., LTD. · 2026-11-18
# ═══════════════════════════════════════════════════════════════
#
#  WHAT THIS ADDS
#  - 14 adjustable input_number helpers (7 per box: slope+offset for pH/EC,
#    offset for TDS/Salinity/Temperature)
#  - 10 template sensors "*_cal" = raw × slope + offset  (raw sensors kept)
#
#  DEPLOY (path A — packages, preferred: copy 1 file, done)
#  1. Copy this file to:  /config/packages/wq_calibration.yaml
#  2. Ensure configuration.yaml has:
#       homeassistant:
#         packages: !include_dir_named packages
#     (skip if already present)
#  3. Restart HA once (new entities require it)
#  4. Developer Tools → States → search "cal" → set every slope = 1.000
#     and every offset = 0 once (first-run values; they persist after that)
#
#  DEPLOY (path B — no packages enabled: merge the two blocks below into
#  your existing includes/input_number.yaml and template include file)
#
#  ⚠️ DO NOT add `initial:` to any input_number here — YAML helpers with
#  initial: RESET to it on every HA restart (same lesson as the tank
#  FULL-level helpers, incident 2026-11-10). Templates default to
#  slope=1 / offset=0 while a helper is still unset, so until the first
#  manual set, *_cal simply mirrors the raw value.
# ═══════════════════════════════════════════════════════════════

input_number:

  # ── WQ1 (194.1.2.3) ─────────────────────────────────────────
  wq1_ph_slope:
    name: "WQ1 pH Slope (ตัวคูณ)"
    min: 0.800
    max: 1.200
    step: 0.001
    mode: box
    icon: mdi:tune-variant
  wq1_ph_offset:
    name: "WQ1 pH Offset (บวก/ลบ)"
    min: -2.0
    max: 2.0
    step: 0.01
    mode: box
    icon: mdi:tune
  wq1_ec_slope:
    name: "WQ1 EC Slope (ตัวคูณ)"
    min: 0.800
    max: 1.200
    step: 0.001
    mode: box
    icon: mdi:tune-variant
  wq1_ec_offset:
    name: "WQ1 EC Offset (µS/cm)"
    min: -500
    max: 500
    step: 1
    mode: box
    icon: mdi:tune
  wq1_tds_offset:
    name: "WQ1 TDS Offset (ppm)"
    min: -100
    max: 100
    step: 1
    mode: box
    icon: mdi:tune
  wq1_sal_offset:
    name: "WQ1 Salinity Offset (ppm)"
    min: -100
    max: 100
    step: 1
    mode: box
    icon: mdi:tune
  wq1_temp_offset:
    name: "WQ1 Temp Offset (°C)"
    min: -5.0
    max: 5.0
    step: 0.1
    mode: box
    icon: mdi:thermometer-lines

  # ── WQ2 (194.1.2.4) ─────────────────────────────────────────
  wq2_ph_slope:
    name: "WQ2 pH Slope (ตัวคูณ)"
    min: 0.800
    max: 1.200
    step: 0.001
    mode: box
    icon: mdi:tune-variant
  wq2_ph_offset:
    name: "WQ2 pH Offset (บวก/ลบ)"
    min: -2.0
    max: 2.0
    step: 0.01
    mode: box
    icon: mdi:tune
  wq2_ec_slope:
    name: "WQ2 EC Slope (ตัวคูณ)"
    min: 0.800
    max: 1.200
    step: 0.001
    mode: box
    icon: mdi:tune-variant
  wq2_ec_offset:
    name: "WQ2 EC Offset (µS/cm)"
    min: -500
    max: 500
    step: 1
    mode: box
    icon: mdi:tune
  wq2_tds_offset:
    name: "WQ2 TDS Offset (ppm)"
    min: -100
    max: 100
    step: 1
    mode: box
    icon: mdi:tune
  wq2_sal_offset:
    name: "WQ2 Salinity Offset (ppm)"
    min: -100
    max: 100
    step: 1
    mode: box
    icon: mdi:tune
  wq2_temp_offset:
    name: "WQ2 Temp Offset (°C)"
    min: -5.0
    max: 5.0
    step: 0.1
    mode: box
    icon: mdi:thermometer-lines

template:

  - sensor:
      # ── WQ1 calibrated ──────────────────────────────────────
      - name: "WQ1 pH (cal)"
        unique_id: wq1_ph_cal
        device_class: ph
        state_class: measurement
        unit_of_measurement: pH
        availability: "{{ states('sensor.wq1_ph') not in ['unknown','unavailable'] }}"
        state: >
          {{ ((states('sensor.wq1_ph') | float(0))
              * (states('input_number.wq1_ph_slope') | float(1))
              + (states('input_number.wq1_ph_offset') | float(0))) | round(2) }}
      - name: "WQ1 EC (cal)"
        unique_id: wq1_ec_cal
        device_class: conductivity
        state_class: measurement
        unit_of_measurement: "µS/cm"
        availability: "{{ states('sensor.wq1_ec') not in ['unknown','unavailable'] }}"
        state: >
          {{ ((states('sensor.wq1_ec') | float(0))
              * (states('input_number.wq1_ec_slope') | float(1))
              + (states('input_number.wq1_ec_offset') | float(0))) | round(0) }}
      - name: "WQ1 Temperature (cal)"
        unique_id: wq1_temp_cal
        device_class: temperature
        state_class: measurement
        unit_of_measurement: "°C"
        availability: "{{ states('sensor.wq1_temperature') not in ['unknown','unavailable'] }}"
        state: >
          {{ ((states('sensor.wq1_temperature') | float(0))
              + (states('input_number.wq1_temp_offset') | float(0))) | round(1) }}
      - name: "WQ1 TDS (cal)"
        unique_id: wq1_tds_cal
        state_class: measurement
        unit_of_measurement: ppm
        availability: "{{ states('sensor.wq1_tds') not in ['unknown','unavailable'] }}"
        state: >
          {{ ((states('sensor.wq1_tds') | float(0))
              + (states('input_number.wq1_tds_offset') | float(0))) | round(0) }}
      - name: "WQ1 Salinity (cal)"
        unique_id: wq1_sal_cal
        state_class: measurement
        unit_of_measurement: ppm
        availability: "{{ states('sensor.wq1_salinity') not in ['unknown','unavailable'] }}"
        state: >
          {{ ((states('sensor.wq1_salinity') | float(0))
              + (states('input_number.wq1_sal_offset') | float(0))) | round(0) }}

      # ── WQ2 calibrated ──────────────────────────────────────
      - name: "WQ2 pH (cal)"
        unique_id: wq2_ph_cal
        device_class: ph
        state_class: measurement
        unit_of_measurement: pH
        availability: "{{ states('sensor.wq2_ph') not in ['unknown','unavailable'] }}"
        state: >
          {{ ((states('sensor.wq2_ph') | float(0))
              * (states('input_number.wq2_ph_slope') | float(1))
              + (states('input_number.wq2_ph_offset') | float(0))) | round(2) }}
      - name: "WQ2 EC (cal)"
        unique_id: wq2_ec_cal
        device_class: conductivity
        state_class: measurement
        unit_of_measurement: "µS/cm"
        availability: "{{ states('sensor.wq2_ec') not in ['unknown','unavailable'] }}"
        state: >
          {{ ((states('sensor.wq2_ec') | float(0))
              * (states('input_number.wq2_ec_slope') | float(1))
              + (states('input_number.wq2_ec_offset') | float(0))) | round(0) }}
      - name: "WQ2 Temperature (cal)"
        unique_id: wq2_temp_cal
        device_class: temperature
        state_class: measurement
        unit_of_measurement: "°C"
        availability: "{{ states('sensor.wq2_temperature') not in ['unknown','unavailable'] }}"
        state: >
          {{ ((states('sensor.wq2_temperature') | float(0))
              + (states('input_number.wq2_temp_offset') | float(0))) | round(1) }}
      - name: "WQ2 TDS (cal)"
        unique_id: wq2_tds_cal
        state_class: measurement
        unit_of_measurement: ppm
        availability: "{{ states('sensor.wq2_tds') not in ['unknown','unavailable'] }}"
        state: >
          {{ ((states('sensor.wq2_tds') | float(0))
              + (states('input_number.wq2_tds_offset') | float(0))) | round(0) }}
      - name: "WQ2 Salinity (cal)"
        unique_id: wq2_sal_cal
        state_class: measurement
        unit_of_measurement: ppm
        availability: "{{ states('sensor.wq2_salinity') not in ['unknown','unavailable'] }}"
        state: >
          {{ ((states('sensor.wq2_salinity') | float(0))
              + (states('input_number.wq2_sal_offset') | float(0))) | round(0) }}
