# One BL0910 chip: 10 channels plus chip-level readings. vars: prefix, chip, base
packages:
  ch1: !include
    file: kincony-n30-channel.yaml
    vars:
      prefix: ${prefix}
      chip: ${chip}
      ch: 1
      base: ${base}
  ch2: !include
    file: kincony-n30-channel.yaml
    vars:
      prefix: ${prefix}
      chip: ${chip}
      ch: 2
      base: ${base}
  ch3: !include
    file: kincony-n30-channel.yaml
    vars:
      prefix: ${prefix}
      chip: ${chip}
      ch: 3
      base: ${base}
  ch4: !include
    file: kincony-n30-channel.yaml
    vars:
      prefix: ${prefix}
      chip: ${chip}
      ch: 4
      base: ${base}
  ch5: !include
    file: kincony-n30-channel.yaml
    vars:
      prefix: ${prefix}
      chip: ${chip}
      ch: 5
      base: ${base}
  ch6: !include
    file: kincony-n30-channel.yaml
    vars:
      prefix: ${prefix}
      chip: ${chip}
      ch: 6
      base: ${base}
  ch7: !include
    file: kincony-n30-channel.yaml
    vars:
      prefix: ${prefix}
      chip: ${chip}
      ch: 7
      base: ${base}
  ch8: !include
    file: kincony-n30-channel.yaml
    vars:
      prefix: ${prefix}
      chip: ${chip}
      ch: 8
      base: ${base}
  ch9: !include
    file: kincony-n30-channel.yaml
    vars:
      prefix: ${prefix}
      chip: ${chip}
      ch: 9
      base: ${base}
  ch10: !include
    file: kincony-n30-channel.yaml
    vars:
      prefix: ${prefix}
      chip: ${chip}
      ch: 10
      base: ${base}

sensor:
  - platform: modbus_controller
    address: ${ base + 60 }
    register_type: holding
    name: ${prefix|upper}_${chip}_11_energy_d
    id: ${prefix}_${chip}_11_energy_d
    unit_of_measurement: kWh
    device_class: energy
    state_class: total_increasing
    accuracy_decimals: 3
    value_type: FP32_R

  - platform: modbus_controller
    address: ${ base + 62 }
    register_type: holding
    name: bl0910_${chip}_voltage
    id: ${prefix}_${chip}_voltage
    unit_of_measurement: V
    device_class: voltage
    accuracy_decimals: 1
    value_type: U_WORD
    filters:
      - multiply: 0.01

  - platform: modbus_controller
    address: ${ base + 63 }
    register_type: holding
    name: bl0910_${chip}_frequency
    id: ${prefix}_${chip}_period
    unit_of_measurement: Hz
    device_class: frequency
    accuracy_decimals: 2
    value_type: U_WORD
    filters:
      - multiply: 0.01

  - platform: modbus_controller
    address: ${ base + 64 }
    register_type: holding
    name: bl0910_${chip}_tps1
    id: ${prefix}_${chip}_tps_1
    unit_of_measurement: "°C"
    device_class: temperature
    accuracy_decimals: 1
    value_type: FP32_R

  - platform: modbus_controller
    address: ${ base + 66 }
    register_type: holding
    name: bl0910_${chip}_power_factor
    id: ${prefix}_${chip}_power_factor
    device_class: power_factor
    accuracy_decimals: 2
    value_type: FP32_R

button:
  - platform: template
    name: ${prefix|upper}_${chip}_clear_energy
    entity_category: config
    on_press:
      - lambda: |-
          id(${prefix}_modbus)->queue_command(modbus_controller::ModbusCommandItem::create_write_single_coil(id(${prefix}_modbus), ${ 519 + chip }, true));
