# =============================================================================
# TiltForge — Home Assistant integration (stock firmware, no reflash)
# =============================================================================
# Turns a shipped TiltForge unit into a native Home Assistant cover entity
# using its built-in REST endpoints. Nothing changes on the device.
#
# Needs Home Assistant 2025.12 or newer (modern "template:" syntax). The old
# "cover: - platform: template" form was removed in HA 2026.6.
#
# This is POLLING-based: HA reads /state every 5 s, so a manual cord-pull shows
# up in HA within a few seconds (not instant). For instant/real-time HA, use
# the ESPHome path instead (tiltforge-esphome.yaml).
#
# SETUP:
#   1. Replace every "tiltforge-5110" / "tiltforge_5110" below with YOUR
#      device's name — the 4-character code shown during setup (also the setup
#      Wi-Fi network name). Keep the dash in URLs, the underscore in ids.
#   2. Paste into configuration.yaml (or a package/included file). If your
#      configuration.yaml already has a "rest:", "rest_command:" or
#      "template:" key, merge these entries into it instead of adding a
#      second one.
#   3. Restart Home Assistant.
#
# NOTE on .local: HA resolves tiltforge-XXXX.local on most systems. If your HA
# host can't do mDNS, replace the hostname with the device's IP address (find
# it in your router, on the device's own control page, or via the `i` serial
# command). A DHCP reservation for the unit keeps that IP from changing.
#
# NOTE on the scale: the device reports tilt as 0..100 with OPEN IN THE MIDDLE:
#   0 = closed, slats tilted up    50 = open (slats level)    100 = closed, down
# Home Assistant wants 0 = closed, 100 = open. The templates below fold the
# device scale onto HA's, so "open" reads 100% in HA and either closed end
# reads 0%. Setting a position keeps the slats on the side they are already
# on. Don't wire the device pct straight to HA's slider: 100% would close it.
# =============================================================================

# --- Poll /state once; one sensor carries everything the cover needs ---
rest:
  - resource: "http://tiltforge-5110.local/state"
    scan_interval: 5
    timeout: 4
    sensor:
      - name: "TiltForge 5110 pct"
        unique_id: tiltforge_5110_pct
        value_template: "{{ value_json.pct }}"
        json_attributes:
          - state
          - target
          - calibrated
          - fw
          - matter
          - rescue_ap

# --- Commands. The firmware takes GET or POST; POST is what we use ---
rest_command:
  tiltforge_5110_open:
    url: "http://tiltforge-5110.local/open"
    method: POST
  tiltforge_5110_close:
    url: "http://tiltforge-5110.local/close"
    method: POST
  tiltforge_5110_stop:
    url: "http://tiltforge-5110.local/stop"
    method: POST
  tiltforge_5110_set:
    url: "http://tiltforge-5110.local/tilt?pct={{ pct }}"
    method: POST

# =============================================================================
# VARIANT A — position slider (RECOMMENDED, active)
# Clean single slider + open/close/stop. Maps tilt onto HA position.
# =============================================================================
template:
  - cover:
      - name: "Living Room Blind"
        unique_id: tiltforge_5110_cover
        device_class: blind
        availability: "{{ has_value('sensor.tiltforge_5110_pct') }}"
        # Device 0..50..100 (closed-up, open, closed-down) -> HA 0..100..0
        position: >-
          {% set p = states('sensor.tiltforge_5110_pct') | int(0) %}
          {{ 100 - ((p - 50) | abs) * 2 }}
        state: >-
          {% set p = states('sensor.tiltforge_5110_pct') | int(0) %}
          {% set t = state_attr('sensor.tiltforge_5110_pct', 'target') | int(p) %}
          {% set s = state_attr('sensor.tiltforge_5110_pct', 'state') %}
          {% set hp = 100 - ((p - 50) | abs) * 2 %}
          {% set ht = 100 - ((t - 50) | abs) * 2 %}
          {% if s in ['OPENING', 'CLOSING'] and ht != hp %}
            {{ 'opening' if ht > hp else 'closing' }}
          {% else %}
            {{ 'closed' if hp == 0 else 'open' }}
          {% endif %}
        open_cover:
          - action: rest_command.tiltforge_5110_open
        close_cover:
          - action: rest_command.tiltforge_5110_close
        stop_cover:
          - action: rest_command.tiltforge_5110_stop
        set_cover_position:
          - action: rest_command.tiltforge_5110_set
            data:
              # Stay on the current side: up side (<= 50) or down side (> 50)
              pct: >-
                {% set p = states('sensor.tiltforge_5110_pct') | int(0) %}
                {% set half = (position / 2) | round(0) | int %}
                {{ (100 - half) if p > 50 else half }}

# =============================================================================
# VARIANT B — slat-tilt control (ALTERNATIVE)
# Represents the device as tilt: open/close/stop buttons + a tilt slider.
# To use this instead of Variant A, delete the "template:" block above and
# uncomment the one below. Same scale folding as Variant A.
# =============================================================================
# template:
#   - cover:
#       - name: "Living Room Blind"
#         unique_id: tiltforge_5110_cover
#         device_class: blind
#         availability: "{{ has_value('sensor.tiltforge_5110_pct') }}"
#         tilt: >-
#           {% set p = states('sensor.tiltforge_5110_pct') | int(0) %}
#           {{ 100 - ((p - 50) | abs) * 2 }}
#         state: >-
#           {% set p = states('sensor.tiltforge_5110_pct') | int(0) %}
#           {{ 'closed' if p in [0, 100] else 'open' }}
#         open_cover:
#           - action: rest_command.tiltforge_5110_open
#         close_cover:
#           - action: rest_command.tiltforge_5110_close
#         stop_cover:
#           - action: rest_command.tiltforge_5110_stop
#         set_cover_tilt_position:
#           - action: rest_command.tiltforge_5110_set
#             data:
#               pct: >-
#                 {% set p = states('sensor.tiltforge_5110_pct') | int(0) %}
#                 {% set half = (tilt / 2) | round(0) | int %}
#                 {{ (100 - half) if p > 50 else half }}
