def setup_platform(hass, config, add_devices, discovery_info=None):
    switches = []
    slave = config.get("slave", None)
    if modbus.TYPE == "serial" and not slave:
        _LOGGER.error("No slave number provided for serial Modbus")
        return False
    registers = config.get("registers")
    if registers:
        for regnum, register in registers.items():
            bits = register.get("bits")
            for bitnum, bit in bits.items():
                if bit.get("name"):
                    switches.append(ModbusSwitch(bit.get("name"),
                                                 slave,
                                                 regnum,
                                                 bitnum))
    coils = config.get("coils")
    if coils:
        for coilnum, coil in coils.items():
            switches.append(ModbusSwitch(coil.get("name"),
                                         slave,
                                         coilnum,
                                         0,
                                         coil=True))
    add_devices(switches)
Read configuration and create Modbus devices.
dataset/ETHPy150Open home-assistant/home-assistant/homeassistant/components/switch/modbus.py/setup_platform