#:########################################################################################:#
# Moonraker 3D Printer Package                                                           #
#:########################################################################################:#
#
# TITLE:
#   Moonraker 3D Printer
#
# FILE:
#   packages/moonraker_3dprinter.yaml
#
# VERSION:
#   V1.1 2026-07-20
#
# VERSION HISTORY:
#   V1.1 2026-07-20
#     - Added structured documentation for setup, polling, outputs, and safety.
#     - Recorded that the Creality printer is not currently connected.
#
#   V1.0
#     - Initial Moonraker REST sensors, commands, and customizations.
#
# PURPOSE:
#   Reads Klipper and Moonraker printer data and provides Home Assistant REST
#   commands for controlling a Creality K1C 3D printer.
#
# CURRENT STATUS:
#   - As of 2026-07-20, this configuration no longer connects to the printer.
#   - It is retained as a reference for future reconnection work.
#   - Before reconnecting, verify the printer address, Moonraker availability,
#     queried Klipper object names, and companion template entities.
#
# DEPENDENCIES:
#   - A reachable Moonraker server for the Klipper printer.
#   - Current configured endpoint: http://192.168.2.31:7125
#   - Companion templates: templates/moonraker_3dprinter.yaml
#   - The queried sensors and object names must match the Klipper configuration.
#
# POLLING:
#   - Full printer status, printer info, and server data poll every second.
#   - Current print file metadata polls every five seconds.
#
# RAW REST OUTPUTS:
#   - sensor.printer_3d_sensor provides printer object status attributes.
#   - sensor.printer_3d_info provides printer state and state message.
#   - sensor.printer_3d_server provides Mainsail server configuration data.
#   - sensor.printer_3d_file_metadata provides current-file metadata.
#   - Companion templates derive user-facing sensors from these raw entities.
#
# CONTROLS:
#   - Emergency stop immediately halts the printer.
#   - Firmware restart restarts the Klipper firmware process.
#   - Cancel stops the current print; pause and resume control print progress.
#
# SAFETY AND NETWORK:
#   - Emergency stop, firmware restart, and cancel are disruptive actions and
#     should only be exposed through dashboard controls with confirmation.
#   - The configured endpoint uses unencrypted HTTP without authentication and
#     assumes Moonraker is available only on a trusted local network.
#
# USER CONFIGURATION:
#   - If the printer address changes, update every Moonraker URL in this file.
#   - Confirm the filament and chamber sensor object names before reconnecting.
#
#:########################################################################################:#

#:########################################################################################:#
# Home Assistant Customizations                                                          #
#:########################################################################################:#

homeassistant:
  customize:
    rest_command.3d_printer_emergency_stop:
      friendly_name: "Emergency Stop (K1C)"
      area_id: Laundry
      icon: mdi:alert-octagon

    rest_command.3d_printer_firmware_restart:
      friendly_name: "Firmware Restart (K1C)"
      area_id: Laundry
      icon: mdi:restart

    rest_command.3d_printer_cancel:
      friendly_name: "Cancel Print (K1C)"
      area_id: Laundry
      icon: mdi:cancel

    rest_command.3d_printer_pause:
      friendly_name: "Pause Print (K1C)"
      area_id: Laundry
      icon: mdi:pause-circle

    rest_command.3d_printer_resume:
      friendly_name: "Resume Print (K1C)"
      area_id: Laundry
      icon: mdi:play-circle

    sensor.printer_3d_file_metadata:
      area_id: Laundry
      friendly_name: "3D Printer (K1C) Print file data"

    sensor.printer_3d_sensor:
      area_id: Laundry
      friendly_name: "3D Printer (K1C) Sensors"

    sensor.printer_3d_info:
      area_id: Laundry
      friendly_name: "3D Printer (K1C) State"

    sensor.printer_3d_server:
      area_id: Laundry
      friendly_name: "3D Printer (K1C) Print Server"

    sensor.3d_printer_chamber_temp:
      area_id: Laundry

    sensor.3d_printer_hotend_target:
      area_id: Laundry

    sensor.3d_printer_hotend_actual:
      area_id: Laundry

    sensor.3d_printer_hotend_power:
      area_id: Laundry

    sensor.3d_printer_bed_target:
      area_id: Laundry

    sensor.3d_printer_bed_actual:
      area_id: Laundry

    sensor.3d_printer_bed_power:
      area_id: Laundry

    sensor.3d_printer_state:
      area_id: Laundry

    sensor.3d_printer_current_print:
      area_id: Laundry

    sensor.3d_printer_current_progress:
      area_id: Laundry

    sensor.3d_printer_print_time:
      area_id: Laundry

    sensor.3d_printer_time_remaining:
      area_id: Laundry

    sensor.3d_printer_eta:
      area_id: Laundry

    sensor.3d_printer_message:
      area_id: Laundry

    sensor.3d_printer_layer_height:
      area_id: Laundry

    sensor.3d_printer_object_height:
      area_id: Laundry

    sensor.3d_printer_current_height:
      area_id: Laundry

    sensor.3d_printer_current_layer:
      area_id: Laundry

    sensor.3d_printer_total_layers:
      area_id: Laundry

    sensor.3d_printer_actual_layer:
      area_id: Laundry

    sensor.3d_printer_object_thumbnails:
      area_id: Laundry

    sensor.3d_printer_state_message:
      area_id: Laundry

    sensor.3d_printer_heaters_power:
      area_id: Laundry

    sensor.3d_printer_printername:
      area_id: Laundry

    binary_sensor.3d_printer_filament:
      area_id: Laundry

      # === REST sensor: File metadata for current 3D print ===
rest:
  scan_interval: 5
  resource_template: "http://192.168.2.31:7125/server/files/metadata?filename={{ states('sensor.3d_printer_current_print') | urlencode }}"
  sensor:
    - name: printer_3d_file_metadata
      unique_id: "192.168.2.315ec44d90-419c-419c-802d-d34071639c08"
      icon: mdi:file-document-outline
      json_attributes_path: "$.result"
      json_attributes:
        - layer_height
        - object_height
        - thumbnails
      value_template: "OK"

rest_command:
  3d_printer_emergency_stop:
    url: "http://192.168.2.31:7125/printer/emergency_stop"
    method: post
  3d_printer_firmware_restart:
    url: "http://192.168.2.31:7125/printer/firmware_restart"
    method: post
  3d_printer_cancel:
    url: "http://192.168.2.31:7125/printer/print/cancel"
    method: post
  3d_printer_pause:
    url: "http://192.168.2.31:7125/printer/print/pause"
    method: post
  3d_printer_resume:
    url: "http://192.168.2.31:7125/printer/print/resume"
    method: post

sensor:
  # === REST sensor: Full 3D printer status (Klipper printer/objects/query) ===
  - platform: rest
    name: printer_3d_sensor
    unique_id: "192.168.2.313a0c25fa-297d-4c19-b03c-ddf04840682b"
    icon: mdi:printer-3d-nozzle
    resource: "http://192.168.2.31:7125/printer/objects/query?heater_bed&extruder&print_stats&toolhead&display_status&virtual_sdcard&gcode_move&filament_motion_sensor%20btt_smart_filament&temperature_sensor%20Chamber_Temp"
    json_attributes_path: "$.result.status"
    json_attributes:
      - heater_bed
      - extruder
      - print_stats
      - toolhead
      - display_status
      - virtual_sdcard
      - gcode_move
      - "filament_motion_sensor btt_smart_filament_sensor"
      - "temperature_sensor Chamber_Temp"
    value_template: "OK"
    force_update: true
    scan_interval: 1

  # === REST sensor: Basic printer state and message ===
  - platform: rest
    name: printer_3d_info
    unique_id: "192.168.2.311cba6677-02bd-4273-9083-b8301bf6943b"
    icon: mdi:information-outline
    scan_interval: 1
    resource_template: "http://192.168.2.31:7125/printer/info"
    json_attributes_path: "$.result"
    json_attributes:
      - state_message
      - state
    value_template: "OK"

  # === REST sensor: Server configuration details (e.g., printer name) ===
  - platform: rest
    name: printer_3d_server
    unique_id: "192.168.2.311cba6677-02bd-4273-9083-b8301bf6949"
    icon: mdi:server
    scan_interval: 1
    resource_template: "http://192.168.2.31:7125/server/database/item?namespace=mainsail&key=general"
    json_attributes_path: "$.result.value"
    json_attributes:
      - printername
    value_template: "OK"
