# =============================================================================
# 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.
#
# 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" below with YOUR device's name — the
#      4-character code shown during setup (also the setup Wi-Fi network name).
#   2. Paste into configuration.yaml (or a package/included file).
#   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, or via the device's `i` serial command).
#
# NOTE on "position": TiltForge is physically a slat-TILT device. Variant A
# (below, active) maps that tilt onto HA's position slider — the cleanest
# single-control UX. Variant B represents it as slat-tilt instead. Use ONE.
# =============================================================================

# --- Shared: REST commands (used by both variants) ---
rest_command:
  tiltforge_5110_open:
    url: "http://tiltforge-5110.local/open"
    method: GET
  tiltforge_5110_close:
    url: "http://tiltforge-5110.local/close"
    method: GET
  tiltforge_5110_stop:
    url: "http://tiltforge-5110.local/stop"
    method: GET
  tiltforge_5110_set:
    url: "http://tiltforge-5110.local/tilt?pct={{ pct }}"
    method: POST

# --- Shared: poll /state for current position (0=closed .. 100=open) ---
sensor:
  - platform: rest
    name: tiltforge_5110_pct
    resource: "http://tiltforge-5110.local/state"
    value_template: "{{ value_json.pct }}"
    scan_interval: 5

# =============================================================================
# VARIANT A — position slider (RECOMMENDED, active)
# Clean single slider + open/close/stop. Maps tilt onto HA position.
# =============================================================================
cover:
  - platform: template
    covers:
      tiltforge_5110:
        friendly_name: "Living Room Blind"
        device_class: blind
        position_template: "{{ states('sensor.tiltforge_5110_pct') | int(0) }}"
        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:
            pct: "{{ position }}"

# =============================================================================
# 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 "cover:" block above and
# uncomment the one below.
# =============================================================================
# cover:
#   - platform: template
#     covers:
#       tiltforge_5110:
#         friendly_name: "Living Room Blind"
#         device_class: blind
#         tilt_template: "{{ states('sensor.tiltforge_5110_pct') | int(0) }}"
#         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: "{{ tilt }}"

# -----------------------------------------------------------------------------
# Older Home Assistant (pre-2024.8): replace every "action:" above with
# "service:" — the two are equivalent; newer HA prefers "action:".
# -----------------------------------------------------------------------------
