# ALM-173-R1 — ESPHome map (MAP v4: bit-packed Input Registers + stateful coils, uniform with DIO)
##
## Include per module with unique vars (alm_id, alm_address, alm_prefix):
##
##   packages:
##     alm1:
##       url: https://github.com/isystemsautomation/homemaster-dev
##       ref: main
##       files:
##         - path: ALM-173-R1/Firmware/v0.2.0/default_alm_173_r1_plc/default_alm_173_r1_plc.yaml
##           vars:
##             alm_id: alm1
##             alm_address: "4"
##             alm_prefix: "ALM#1"
##
## Input Registers (FC04, read) — contiguous @0..3 (one poll per cycle):
##   0 IN1..IN16 mask · 1 IN17/Any/G1-G3/Relay/LED/Armed/Entry/Exit/Tamper
##   2 Zone1..16 latched · 3 Zone17 latched
##
## Relay/Armed state is in IREG 1 (no FC01 coil read-back — STD-009 / DIO pattern):
##   bit5 0x0020 Relay 1 · bit6 0x0040 Relay 2 · bit7 0x0080 Relay 3 · bit12 0x1000 Armed
##   group=0 → Modbus only; group≠0 → alarm (+ Modbus override)
##
## Coils (FC05/15 write-only) @0..10 — assumed_state, never polled with FC01:
##   0..2 Relay 1..3 · 3 Armed · 4 Ack All · 5..7 Ack G1-G3 · 8..10 pulse G1-G3
##
## Identity (FC04) @200 — read once at startup; not in this package.

substitutions:
  alm_id: alm
  alm_address: "3"
  alm_prefix: "ALM"
  alm_update_interval: 1s
  alm_command_throttle: 0ms

modbus_controller:
  - id: ${alm_id}
    address: ${alm_address}
    modbus_id: modbus_bus
    command_throttle: ${alm_command_throttle}
    update_interval: ${alm_update_interval}

binary_sensor:
  # IN1..IN16 @ IREG 0
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} IN1"
    register_type: read
    address: 0
    bitmask: 0x0001
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} IN2"
    register_type: read
    address: 0
    bitmask: 0x0002
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} IN3"
    register_type: read
    address: 0
    bitmask: 0x0004
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} IN4"
    register_type: read
    address: 0
    bitmask: 0x0008
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} IN5"
    register_type: read
    address: 0
    bitmask: 0x0010
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} IN6"
    register_type: read
    address: 0
    bitmask: 0x0020
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} IN7"
    register_type: read
    address: 0
    bitmask: 0x0040
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} IN8"
    register_type: read
    address: 0
    bitmask: 0x0080
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} IN9"
    register_type: read
    address: 0
    bitmask: 0x0100
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} IN10"
    register_type: read
    address: 0
    bitmask: 0x0200
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} IN11"
    register_type: read
    address: 0
    bitmask: 0x0400
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} IN12"
    register_type: read
    address: 0
    bitmask: 0x0800
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} IN13"
    register_type: read
    address: 0
    bitmask: 0x1000
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} IN14"
    register_type: read
    address: 0
    bitmask: 0x2000
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} IN15"
    register_type: read
    address: 0
    bitmask: 0x4000
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} IN16"
    register_type: read
    address: 0
    bitmask: 0x8000

  # IN17, alarms, LEDs, arming status @ IREG 1
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} IN17"
    register_type: read
    address: 1
    bitmask: 0x0001
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} Any Alarm"
    register_type: read
    address: 1
    bitmask: 0x0002
    device_class: problem
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} Alarm Group 1"
    register_type: read
    address: 1
    bitmask: 0x0004
    device_class: problem
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} Alarm Group 2"
    register_type: read
    address: 1
    bitmask: 0x0008
    device_class: problem
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} Alarm Group 3"
    register_type: read
    address: 1
    bitmask: 0x0010
    device_class: problem
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} LED 1"
    register_type: read
    address: 1
    bitmask: 0x0100
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} LED 2"
    register_type: read
    address: 1
    bitmask: 0x0200
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} LED 3"
    register_type: read
    address: 1
    bitmask: 0x0400
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} LED 4"
    register_type: read
    address: 1
    bitmask: 0x0800
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} Entry pending"
    register_type: read
    address: 1
    bitmask: 0x2000
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} Exit pending"
    register_type: read
    address: 1
    bitmask: 0x4000
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} Tamper (any)"
    register_type: read
    address: 1
    bitmask: 0x8000
    device_class: problem

  # Relay/Armed state @ IREG 1 — same FC04 block; feeds template switches below
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    id: ${alm_id}_relay1_state
    name: "${alm_prefix} Relay 1 state"
    internal: true
    register_type: read
    address: 1
    bitmask: 0x0020
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    id: ${alm_id}_relay2_state
    name: "${alm_prefix} Relay 2 state"
    internal: true
    register_type: read
    address: 1
    bitmask: 0x0040
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    id: ${alm_id}_relay3_state
    name: "${alm_prefix} Relay 3 state"
    internal: true
    register_type: read
    address: 1
    bitmask: 0x0080
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    id: ${alm_id}_armed_state
    name: "${alm_prefix} Armed state"
    internal: true
    register_type: read
    address: 1
    bitmask: 0x1000

  # Zone latched @ IREG 2 (1..16) and 3 (17)
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} Zone 1 latched"
    register_type: read
    address: 2
    bitmask: 0x0001
    device_class: problem
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} Zone 2 latched"
    register_type: read
    address: 2
    bitmask: 0x0002
    device_class: problem
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} Zone 3 latched"
    register_type: read
    address: 2
    bitmask: 0x0004
    device_class: problem
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} Zone 4 latched"
    register_type: read
    address: 2
    bitmask: 0x0008
    device_class: problem
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} Zone 5 latched"
    register_type: read
    address: 2
    bitmask: 0x0010
    device_class: problem
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} Zone 6 latched"
    register_type: read
    address: 2
    bitmask: 0x0020
    device_class: problem
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} Zone 7 latched"
    register_type: read
    address: 2
    bitmask: 0x0040
    device_class: problem
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} Zone 8 latched"
    register_type: read
    address: 2
    bitmask: 0x0080
    device_class: problem
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} Zone 9 latched"
    register_type: read
    address: 2
    bitmask: 0x0100
    device_class: problem
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} Zone 10 latched"
    register_type: read
    address: 2
    bitmask: 0x0200
    device_class: problem
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} Zone 11 latched"
    register_type: read
    address: 2
    bitmask: 0x0400
    device_class: problem
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} Zone 12 latched"
    register_type: read
    address: 2
    bitmask: 0x0800
    device_class: problem
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} Zone 13 latched"
    register_type: read
    address: 2
    bitmask: 0x1000
    device_class: problem
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} Zone 14 latched"
    register_type: read
    address: 2
    bitmask: 0x2000
    device_class: problem
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} Zone 15 latched"
    register_type: read
    address: 2
    bitmask: 0x4000
    device_class: problem
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} Zone 16 latched"
    register_type: read
    address: 2
    bitmask: 0x8000
    device_class: problem
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    name: "${alm_prefix} Zone 17 latched"
    register_type: read
    address: 3
    bitmask: 0x0001
    device_class: problem

switch:
  # Write-only coils — no FC01 read-back (state from IREG 1 via template switches)
  # IDs must be prefixed with ${alm_id} so two packages do not collide (same as DIO/RGB).
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    id: ${alm_id}_relay1_coil
    register_type: coil
    address: 0
    internal: true
    assumed_state: true
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    id: ${alm_id}_relay2_coil
    register_type: coil
    address: 1
    internal: true
    assumed_state: true
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    id: ${alm_id}_relay3_coil
    register_type: coil
    address: 2
    internal: true
    assumed_state: true
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    id: ${alm_id}_armed_coil
    register_type: coil
    address: 3
    internal: true
    assumed_state: true

  # Public switches — state from FC04, command via write-only coils above
  - platform: template
    name: "${alm_prefix} Relay 1"
    id: ${alm_id}_relay1
    lambda: |-
      return id(${alm_id}_relay1_state).state;
    turn_on_action:
      - switch.turn_on: ${alm_id}_relay1_coil
    turn_off_action:
      - switch.turn_off: ${alm_id}_relay1_coil
  - platform: template
    name: "${alm_prefix} Relay 2"
    id: ${alm_id}_relay2
    lambda: |-
      return id(${alm_id}_relay2_state).state;
    turn_on_action:
      - switch.turn_on: ${alm_id}_relay2_coil
    turn_off_action:
      - switch.turn_off: ${alm_id}_relay2_coil
  - platform: template
    name: "${alm_prefix} Relay 3"
    id: ${alm_id}_relay3
    lambda: |-
      return id(${alm_id}_relay3_state).state;
    turn_on_action:
      - switch.turn_on: ${alm_id}_relay3_coil
    turn_off_action:
      - switch.turn_off: ${alm_id}_relay3_coil
  - platform: template
    name: "${alm_prefix} Armed"
    id: ${alm_id}_armed
    lambda: |-
      return id(${alm_id}_armed_state).state;
    turn_on_action:
      - switch.turn_on: ${alm_id}_armed_coil
    turn_off_action:
      - switch.turn_off: ${alm_id}_armed_coil

  # Momentary command coils (internal — driven by template buttons)
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    id: ${alm_id}_ack_all
    register_type: coil
    address: 4
    internal: true
    assumed_state: true
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    id: ${alm_id}_ack_g1
    register_type: coil
    address: 5
    internal: true
    assumed_state: true
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    id: ${alm_id}_ack_g2
    register_type: coil
    address: 6
    internal: true
    assumed_state: true
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    id: ${alm_id}_ack_g3
    register_type: coil
    address: 7
    internal: true
    assumed_state: true
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    id: ${alm_id}_pulse_g1
    register_type: coil
    address: 8
    internal: true
    assumed_state: true
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    id: ${alm_id}_pulse_g2
    register_type: coil
    address: 9
    internal: true
    assumed_state: true
  - platform: modbus_controller
    modbus_controller_id: ${alm_id}
    id: ${alm_id}_pulse_g3
    register_type: coil
    address: 10
    internal: true
    assumed_state: true

button:
  - platform: template
    name: "${alm_prefix} Ack All"
    on_press:
      - switch.turn_on: ${alm_id}_ack_all
  - platform: template
    name: "${alm_prefix} Ack Group 1"
    on_press:
      - switch.turn_on: ${alm_id}_ack_g1
  - platform: template
    name: "${alm_prefix} Ack Group 2"
    on_press:
      - switch.turn_on: ${alm_id}_ack_g2
  - platform: template
    name: "${alm_prefix} Ack Group 3"
    on_press:
      - switch.turn_on: ${alm_id}_ack_g3
  - platform: template
    name: "${alm_prefix} Pulse Group 1"
    on_press:
      - switch.turn_on: ${alm_id}_pulse_g1
  - platform: template
    name: "${alm_prefix} Pulse Group 2"
    on_press:
      - switch.turn_on: ${alm_id}_pulse_g2
  - platform: template
    name: "${alm_prefix} Pulse Group 3"
    on_press:
      - switch.turn_on: ${alm_id}_pulse_g3
