modbus:
  - id: mod_bus2
    uart_id: uart_bus
    role: server

modbus_controller:
  - id: modbus_controller1
    address: 0x2
    modbus_id: modbus_bus
    allow_duplicate_commands: false
    on_online:
      then:
        logger.log: "Module Online"
  - id: modbus_controller2
    address: 0x2
    modbus_id: mod_bus2
    server_registers:
      - address: 0x0000
        value_type: S_DWORD_R
        read_lambda: |-
          return 42.3;
    max_cmd_retries: 0
  - id: modbus_controller3
    address: 0x3
    modbus_id: mod_bus2
    server_registers:
      - address: 0x0009
        value_type: S_DWORD
        read_lambda: |-
          return 31;
        write_lambda: |-
          printf("address=%d, value=%d", x);
          return true;
    max_cmd_retries: 0
  - id: modbus_controller4
    modbus_id: mod_bus2
    address: 0x4
    server_courtesy_response:
      enabled: true
      register_last_address: 100
      register_value: 0
    server_registers:
      - address: 0x0001
        value_type: U_WORD
        read_lambda: |-
          return 0x8;
      - address: 0x0005
        value_type: U_WORD
        read_lambda: |-
          return (random_uint32() % 100);
binary_sensor:
  - platform: modbus_controller
    modbus_controller_id: modbus_controller1
    id: modbus_binary_sensor1
    name: Test Binary Sensor
    register_type: read
    address: 0x3200
    bitmask: 0x80
  - platform: modbus_controller
    modbus_controller_id: modbus_controller1
    id: modbus_binary_sensor2
    name: Test Binary Sensor with Lambda
    register_type: read
    address: 0x3201
    lambda: |-
      return x;

number:
  - platform: modbus_controller
    modbus_controller_id: modbus_controller1
    id: modbus_number1
    name: Test Number
    address: 0x9001
    value_type: U_WORD
    multiply: 1.0
  - platform: modbus_controller
    modbus_controller_id: modbus_controller1
    id: modbus_number2
    name: Test Number with Lambda
    address: 0x9002
    value_type: U_WORD
    lambda: |-
      return x * 2.0;
    write_lambda: |-
      return x / 2.0;

output:
  - platform: modbus_controller
    modbus_controller_id: modbus_controller1
    id: modbus_output1
    address: 2048
    register_type: holding
    value_type: U_WORD
    multiply: 1000
  - platform: modbus_controller
    modbus_controller_id: modbus_controller1
    id: modbus_output2
    address: 2049
    register_type: holding
    value_type: U_WORD
    write_lambda: |-
      return x * 100.0;

select:
  - platform: modbus_controller
    modbus_controller_id: modbus_controller1
    id: modbus_select1
    name: Test Select
    address: 1000
    value_type: U_WORD
    optionsmap:
      "Zero": 0
      "One": 1
      "Two": 2
      "Three": 3
  - platform: modbus_controller
    modbus_controller_id: modbus_controller1
    id: modbus_select2
    name: Test Select with Lambda
    address: 1001
    value_type: U_WORD
    optionsmap:
      "Off": 0
      "On": 1
      "Two": 2
    lambda: |-
      ESP_LOGD("Reg1001", "Received value %lld", x);
      if (x > 1) {
        return std::string("Two");
      } else if (x == 1) {
        return std::string("On");
      }
      return std::string("Off");
    write_lambda: |-
      ESP_LOGD("Reg1001", "Set option to %s (%lld)", x.c_str(), value);
      if (x == "On") {
        return 1;
      }
      if (x == "Two") {
        payload.push_back(0x0002);
        return 0;
      }
      return value;

sensor:
  - platform: modbus_controller
    modbus_controller_id: modbus_controller1
    id: modbus_sensor1
    name: Test Sensor
    register_type: holding
    address: 0x9001
    unit_of_measurement: "AH"
    value_type: U_WORD
  - platform: modbus_controller
    modbus_controller_id: modbus_controller1
    id: modbus_sensor2
    name: Test Sensor with Lambda
    register_type: holding
    address: 0x9002
    value_type: U_WORD
    lambda: |-
      return x / 10.0;

switch:
  - platform: modbus_controller
    modbus_controller_id: modbus_controller1
    id: modbus_switch1
    name: Test Switch
    register_type: coil
    address: 0x15
    bitmask: 1
  - platform: modbus_controller
    modbus_controller_id: modbus_controller1
    id: modbus_switch2
    name: Test Switch with Lambda
    register_type: coil
    address: 0x16
    lambda: |-
      return !x;
    write_lambda: |-
      return !x;

text_sensor:
  - platform: modbus_controller
    modbus_controller_id: modbus_controller1
    id: modbus_text_sensor1
    name: Test Text Sensor
    register_type: holding
    address: 0x9013
    register_count: 3
    raw_encode: HEXBYTES
    response_size: 6
  - platform: modbus_controller
    modbus_controller_id: modbus_controller1
    id: modbus_text_sensor2
    name: Test Text Sensor with Lambda
    register_type: holding
    address: 0x9014
    register_count: 2
    response_size: 4
    lambda: |-
      return "Modified: " + x;
